Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Bugzilla/Constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,11 @@ sub DEFAULT_CSP {
'self',

# used in template/en/default/search/search-google.html.tmpl
'https://www.google.com/search'
'https://www.google.com/search',

# post_bug.cgi redirects to enter_bug.cgi's creation_callback, which
# is always a loopback URL (see loopback_callback_url in Bugzilla::Util)
'http://127.0.0.1:*', 'http://localhost:*', 'http://[::1]:*',
],
frame_ancestors => ['self'],

Expand Down
17 changes: 16 additions & 1 deletion Bugzilla/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use base qw(Exporter);
trim wrap_hard wrap_comment find_wrap_point
format_time validate_date validate_time datetime_from time_ago
file_mod_time is_7bit_clean
is_safe_url
is_safe_url loopback_callback_url
bz_crypt generate_random_password
validate_email_syntax clean_text
get_text template_var disable_utf8
Expand Down Expand Up @@ -239,6 +239,21 @@ sub is_safe_url {
return 0;
}

# Returns $url if it is an http URL on the local machine with an explicit port
# and a plain path, such as http://127.0.0.1:8765/done, and undef otherwise.
# Used for enter_bug.cgi's creation_callback, so a local tool that opened the
# form learns the new bug's ID. Anything else would be an open redirect, so the
# whole string is matched strictly rather than parsed: no userinfo, query,
# fragment or characters that browsers and URI.pm might read differently.
sub loopback_callback_url {
my ($url) = @_;
return undef if !defined $url || ref $url;
return undef
unless $url
=~ m{\Ahttp://(?:127\.0\.0\.1|localhost|\[::1\]):[1-9][0-9]{0,4}(?:/[A-Za-z0-9._~/-]*)?\z};
return $url;
}

sub email_filter {
my ($toencode) = @_;
if (!Bugzilla->user->id) {
Expand Down
4 changes: 4 additions & 0 deletions enter_bug.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ $vars->{'cloned_bug_id'} = $cloned_bug_id;

$vars->{'token'} = issue_session_token('create_bug');

# A local tool that opened this form can ask to be told the new bug's ID.
my $callback = loopback_callback_url(scalar $cgi->param('creation_callback'));
$vars->{'creation_callback'} = $callback if $callback;
Comment on lines +245 to +247


my @enter_bug_fields = grep { $_->enter_bug } Bugzilla->active_custom_fields;
foreach my $field (@enter_bug_fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
<input type="hidden" name="product" value="[% product.name FILTER html %]">
<input type="hidden" name="token" value="[% token FILTER html %]">
<input type="hidden" name="bug_ignored" value="[% bug_ignored ? "1" : "0" %]">
[% IF creation_callback %]
<input type="hidden" name="creation_callback" value="[% creation_callback FILTER html %]">
[% END %]
Comment on lines +111 to +113

<div role="toolbar" id="page-toolbar">
<h2>File a New [% terms.Bug %]</h2>
Expand Down
9 changes: 9 additions & 0 deletions post_bug.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ foreach my $ref_bug_id (uniq @{$bug->{see_also_changes} || []}) {
$C->flash(last_sent_changes => \@all_mail_results);

my $redirect_url = Bugzilla->localconfig->urlbase . "show_bug.cgi?id=$id";

# If a local tool opened the form with a creation_callback, send the browser
# there with the bug ID instead. The tool is expected to redirect on to the
# bug. Only loopback URLs are accepted, so this is not an open redirect.
if (my $callback
= loopback_callback_url(scalar $cgi->param('creation_callback')))
{
$redirect_url = "$callback?bug_id=$id";
}
$C->redirect_to($redirect_url);

1;
65 changes: 65 additions & 0 deletions t/loopback-callback-url.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env perl
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
use strict;
use warnings;
use 5.10.1;
use lib qw( . lib local/lib/perl5 );

use Bugzilla::Util qw(loopback_callback_url);
use Test2::V0;

# post_bug.cgi redirects to enter_bug.cgi's creation_callback after filing a
# bug, so anything accepted here is a redirect target. Only loopback http URLs
# with an explicit port and a plain path may pass.

my @good = (
'http://127.0.0.1:8765',
'http://127.0.0.1:8765/',
'http://127.0.0.1:8765/done',
'http://localhost:1/bug-filed/cb_1.x~y',
'http://[::1]:65535/done',
);
is(loopback_callback_url($_), $_, "accepts $_") for @good;

my @bad = (
undef,
'',
['http://127.0.0.1:8765/'],
'https://127.0.0.1:8765/',
'http://127.0.0.1/',
'http://127.0.0.1:/',
'http://127.0.0.1:0/',
'http://127.0.0.1:080/',
'http://localhost/done',
'http://evil.example:8765/',
'http://127.0.0.1.evil.example:8765/',
'http://localhost.evil.example:8765/',
'http://127.0.0.1:8765@evil.example/',
'http://evil.example@127.0.0.1:8765/',
'http://127.0.0.1:8765\\@evil.example/',
'http://127.0.0.1:8765\\evil.example',
'http://127.0.0.1:8765/done?next=https://evil.example/',
'http://127.0.0.1:8765/done#frag',
'http://127.0.0.1:8765/done%2F%2Fevil.example',
"http://127.0.0.1:8765/done\n",
"http://127.0.0.1:8765/\nLocation: https://evil.example/",
' http://127.0.0.1:8765/',
'HTTP://127.0.0.1:8765/',
'http://0.0.0.0:8765/',
'http://2130706433:8765/',
'http://[::ffff:127.0.0.1]:8765/',
'//127.0.0.1:8765/',
'javascript:alert(1)//http://127.0.0.1:8765/',
);
foreach my $url (@bad) {
my $name = defined $url ? (ref $url ? 'an array ref' : "'$url'") : 'undef';
$name =~ s/\n/\\n/g;
is(loopback_callback_url($url), undef, "rejects $name");
}

done_testing;