From 6df1ae87fde63a13ed3499cd8e2006d1734e352b Mon Sep 17 00:00:00 2001 From: Andrew Halberstadt Date: Tue, 1 Dec 2026 10:03:16 -0500 Subject: [PATCH] Bug 2099955 - Add a creation_callback param to enter_bug.cgi that redirects to a loopback URL with the new bug ID A local tool, such as a coding agent, that opens a prefilled enter_bug.cgi form in the user's browser can pass creation_callback=http://127.0.0.1:PORT/path. After the bug is filed, post_bug.cgi redirects the browser there with ?bug_id=N instead of to show_bug.cgi, so the tool learns which bug was filed. Only http URLs on 127.0.0.1, localhost or [::1] with an explicit port and a plain path are accepted, so this is not an open redirect. The loopback origins are added to the CSP form-action list, which browsers apply to the redirect after a form POST. --- Bugzilla/Constants.pm | 6 +- Bugzilla/Util.pm | 17 ++++- enter_bug.cgi | 4 ++ .../en/default/bug_modal/create.html.tmpl | 3 + post_bug.cgi | 9 +++ t/loopback-callback-url.t | 65 +++++++++++++++++++ 6 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 t/loopback-callback-url.t 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 %]