diff --git a/Bugzilla/Constants.pm b/Bugzilla/Constants.pm
index d27bfd790a..008acef0f8 100644
--- a/Bugzilla/Constants.pm
+++ b/Bugzilla/Constants.pm
@@ -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'],
diff --git a/Bugzilla/Util.pm b/Bugzilla/Util.pm
index 20fe79254c..f430178f86 100644
--- a/Bugzilla/Util.pm
+++ b/Bugzilla/Util.pm
@@ -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
@@ -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) {
diff --git a/enter_bug.cgi b/enter_bug.cgi
index 68894f965f..87d8623b68 100755
--- a/enter_bug.cgi
+++ b/enter_bug.cgi
@@ -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;
+
my @enter_bug_fields = grep { $_->enter_bug } Bugzilla->active_custom_fields;
foreach my $field (@enter_bug_fields) {
diff --git a/extensions/BugModal/template/en/default/bug_modal/create.html.tmpl b/extensions/BugModal/template/en/default/bug_modal/create.html.tmpl
index c0d691c36d..3a8236c012 100644
--- a/extensions/BugModal/template/en/default/bug_modal/create.html.tmpl
+++ b/extensions/BugModal/template/en/default/bug_modal/create.html.tmpl
@@ -108,6 +108,9 @@
+ [% IF creation_callback %]
+
+ [% END %]
File a New [% terms.Bug %]
diff --git a/post_bug.cgi b/post_bug.cgi
index 1642a9a5ca..c407030b92 100755
--- a/post_bug.cgi
+++ b/post_bug.cgi
@@ -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;
diff --git a/t/loopback-callback-url.t b/t/loopback-callback-url.t
new file mode 100644
index 0000000000..2bf39878e4
--- /dev/null
+++ b/t/loopback-callback-url.t
@@ -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;