Skip to content
Open
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
28 changes: 18 additions & 10 deletions Bugzilla.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
2 changes: 1 addition & 1 deletion buglist.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion extensions/BMO/bin/export_bmo_etl.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
69 changes: 69 additions & 0 deletions t/app-log-user-request.t
Original file line number Diff line number Diff line change
@@ -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;
Loading