From b261d86e184d609286251f1f678bf5b2d244da56 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 22 Oct 2026 19:14:49 +0200 Subject: [PATCH 1/3] Bug 2085028 - log_user_request logs empty request_url/user_agent and server 'web' for native Mojo API routes The CGI environment is only emulated for the legacy .cgi routes, so on native Bugzilla::API::V1 routes log_user_request logged an empty URL and user agent, a default GET method and server 'web'. Read them from the Mojo request under USAGE_MODE_MOJO_REST instead. --- Bugzilla.pm | 28 ++++++++++------ t/app-log-user-request.t | 69 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 t/app-log-user-request.t diff --git a/Bugzilla.pm b/Bugzilla.pm index afbd732702..5ac3a44517 100644 --- a/Bugzilla.pm +++ b/Bugzilla.pm @@ -593,16 +593,24 @@ sub log_user_request { unless (i_am_cgi() || i_am_webservice()) && Bugzilla->params->{log_user_requests}; - my $cgi = $class->cgi; - my $user_id = $class->user->id; - my $request_url = $cgi->request_uri // ''; - my $method = $cgi->request_method // 'GET'; - my $user_agent = $cgi->user_agent // ''; - my $script_name = $cgi->script_name; - my $server = "web"; - - if ($script_name =~ /rest\.cgi/) { - $server = "rest"; + my $user_id = $class->user->id; + my ($request_url, $method, $user_agent, $server); + + # The CGI environment is only emulated for the legacy .cgi routes, so read + # the request of a native Mojo API route from the Mojo controller. + if ($class->usage_mode == USAGE_MODE_MOJO_REST) { + my $req = request_cache->{mojo_controller}->req; + $request_url = $req->url->path->to_string; + $method = $req->method; + $user_agent = $req->headers->user_agent // ''; + $server = 'rest'; + } + else { + my $cgi = $class->cgi; + $request_url = $cgi->request_uri // ''; + $method = $cgi->request_method // 'GET'; + $user_agent = $cgi->user_agent // ''; + $server = $cgi->script_name =~ /rest\.cgi/ ? 'rest' : 'web'; } my @params = ( diff --git a/t/app-log-user-request.t b/t/app-log-user-request.t new file mode 100644 index 0000000000..b98d53497c --- /dev/null +++ b/t/app-log-user-request.t @@ -0,0 +1,69 @@ +#!/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 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::MockParams (log_user_requests => 1); +use Bugzilla::Test::Util qw(create_user issue_api_key); + +use Bugzilla::Constants; +use Test2::V0; +use Test::Mojo; + +# Native Mojo API routes have no emulated CGI environment, so +# Bugzilla->log_user_request must read the request from the Mojo controller. + +my $user = create_user('logged@mozilla.org', '*'); +my $key = issue_api_key('logged@mozilla.org'); + +my $t = Test::Mojo->new('Bugzilla::App'); +$t->app->routes->get( + '/test/log_user_request' => sub { + my $c = shift; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + $c->bugzilla->login; + Bugzilla->log_user_request(42, 7, 'attachment-get'); + $c->render(json => {}); + } +); + +# Like the legacy REQUEST_URI, the logged URL is the path without the query. +$t->get_ok('/test/log_user_request?ids=1' => + {'X-Bugzilla-API-Key' => $key->api_key, 'User-Agent' => 'log-test/1.0',}) + ->status_is(200); + +my $rows = Bugzilla->dbh->selectall_arrayref( + 'SELECT user_id, user_agent, request_url, method, bug_id, attach_id, + action, server + FROM user_request_log', {Slice => {}} +); +is( + $rows, + [{ + user_id => $user->id, + user_agent => 'log-test/1.0', + request_url => '/test/log_user_request', + method => 'GET', + bug_id => 42, + attach_id => 7, + action => 'attachment-get', + server => 'rest', + }], + 'The request is logged from the Mojo request' +); + +done_testing; From 7e9af99b60b9391bd5fc690fffe2f7445a25a87a Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 23 Oct 2026 15:01:07 +0200 Subject: [PATCH 2/3] Bug 2085028 - Fall back to 'bugs' when runseries has no namedcmd --- buglist.cgi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buglist.cgi b/buglist.cgi index 848c0ca08c..0c892d9737 100755 --- a/buglist.cgi +++ b/buglist.cgi @@ -323,7 +323,7 @@ my $disp_prefix = "bugs"; if ( ($cmdtype eq "dorem" && $remaction =~ /^run/) || ($format->{extension} ne 'html' && defined $cgi->param('namedcmd'))) { - $disp_prefix = $cgi->param('namedcmd'); + $disp_prefix = $cgi->param('namedcmd') // 'bugs'; } # Take appropriate action based on user's request. From b317e5eccd0f1d3aea9a7414c4f4e56c3dda65ea Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 23 Oct 2026 15:03:19 +0200 Subject: [PATCH 3/3] Bug 2085028 - Use $PROCESS_ID instead of $$ in export_bmo_etl.pl to satisfy critic --- extensions/BMO/bin/export_bmo_etl.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extensions/BMO/bin/export_bmo_etl.pl b/extensions/BMO/bin/export_bmo_etl.pl index 385e57c59e..c54aa80515 100644 --- a/extensions/BMO/bin/export_bmo_etl.pl +++ b/extensions/BMO/bin/export_bmo_etl.pl @@ -21,6 +21,7 @@ use Bugzilla::User; use Bugzilla::FlagActivity; +use English qw(-no_match_vars $PROCESS_ID); use HTTP::Headers; use HTTP::Request; use IO::Compress::Gzip qw(gzip $GzipError); @@ -82,7 +83,7 @@ $lock_wait //= LOCK_MAX_WAIT; # Identifies this run in the lock table and in staging table names -my $run_id = time() . "_$$"; +my $run_id = time() . "_$PROCESS_ID"; # Create a hash for faster checking later my %no_cache = map { $_ => 1 } @no_cache;