Skip to content

Commit a0fcc8f

Browse files
committed
Bug 1196626 - log all authenticated requests
1 parent b21167f commit a0fcc8f

File tree

9 files changed

+90
-1
lines changed

9 files changed

+90
-1
lines changed

Bugzilla.pm

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,45 @@ sub switch_to_main_db {
594594
return $class->dbh_main;
595595
}
596596

597+
sub log_user_request {
598+
my ($class, $bug_id, $attach_id, $action) = @_;
599+
600+
return unless Bugzilla->params->{log_user_requests};
601+
602+
my $cgi = $class->cgi;
603+
my $user_id = $class->user->id;
604+
my $request_url = $cgi->request_uri // '';
605+
my $method = $cgi->request_method;
606+
my $user_agent = $cgi->user_agent // '';
607+
my $script_name = $cgi->script_name;
608+
my $server = "web";
609+
610+
if ($script_name =~ /rest\.cgi/) {
611+
$server = $script_name =~ /BzAPI/ ? "bzapi" : "rest";
612+
}
613+
elsif ($script_name =~ /xmlrpc\.cgi/) {
614+
$server = "xmlrpc";
615+
}
616+
elsif ($script_name =~ /jsonrpc\.cgi/) {
617+
$server = "jsonrpc";
618+
}
619+
620+
my @params = ($user_id, remote_ip(), $user_agent, $request_url, $method, $bug_id, $attach_id, $action, $server);
621+
foreach my $param (@params) {
622+
trick_taint($param) if defined $param;
623+
}
624+
625+
eval {
626+
local $class->request_cache->{dbh};
627+
$class->switch_to_main_db();
628+
$class->dbh->do("INSERT INTO user_request_log
629+
(user_id, ip_address, user_agent, request_url,
630+
method, timestamp, bug_id, attach_id, action, server)
631+
VALUES (?, ?, ?, ?, ?, NOW(), ?, ?, ?, ?)", undef, @params);
632+
};
633+
warn $@ if $@;
634+
}
635+
597636
sub is_shadow_db {
598637
my $class = shift;
599638
return $class->request_cache->{dbh} != $class->dbh_main;

Bugzilla/Attachment/PatchReader.pm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ sub process_diff {
3838
if ($format eq 'raw') {
3939
require Bugzilla::PatchReader::DiffPrinter::raw;
4040
$last_reader->sends_data_to(new Bugzilla::PatchReader::DiffPrinter::raw());
41+
42+
Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get")
43+
if Bugzilla->user->id;
4144
# Actually print out the patch.
4245
print $cgi->header(-type => 'text/plain',
4346
-expires => '+3M');
@@ -93,6 +96,12 @@ sub process_interdiff {
9396
my $lc = Bugzilla->localconfig;
9497
my $vars = {};
9598

99+
if (Bugzilla->user->id) {
100+
foreach my $attachment ($old_attachment, $new_attachment) {
101+
Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get");
102+
}
103+
}
104+
96105
# Encode attachment data as utf8 if it's going to be displayed in a HTML
97106
# page using the UTF-8 encoding.
98107
if ($format ne 'raw' && Bugzilla->params->{'utf8'}) {

Bugzilla/Config/Admin.pm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ sub get_param_list {
6363
type => 't',
6464
default => 10,
6565
checker => \&check_numeric
66+
},
67+
68+
{
69+
name => 'log_user_requests',
70+
type => 'b',
71+
default => 0,
6672
});
6773
return @param_list;
6874
}

Bugzilla/Search.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ sub data {
787787
return $self->{data} if $self->{data};
788788
my $dbh = Bugzilla->dbh;
789789

790+
Bugzilla->log_user_request(undef, undef, "search") if Bugzilla->user->id;
790791
# If all fields belong to the 'bugs' table, there is no need to split
791792
# the original query into two pieces. Else we override the 'fields'
792793
# argument to first get bug IDs based on the search criteria defined

Bugzilla/WebService/Bug.pm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,11 @@ sub get {
458458

459459
$self->_add_update_tokens($params, \@bugs, \@hashes);
460460

461+
if (Bugzilla->user->id) {
462+
foreach my $bug (@bugs) {
463+
Bugzilla->log_user_request($bug->id, undef, 'bug-get');
464+
}
465+
}
461466
return { bugs => \@hashes, faults => \@faults };
462467
}
463468

@@ -1196,17 +1201,26 @@ sub attachments {
11961201
}
11971202

11981203
my %attachments;
1204+
my @log_attachments;
11991205
foreach my $attach (@{Bugzilla::Attachment->new_from_list($attach_ids)}) {
12001206
Bugzilla::Bug->check($attach->bug_id);
12011207
if ($attach->isprivate && !Bugzilla->user->is_insider) {
12021208
ThrowUserError('auth_failure', {action => 'access',
12031209
object => 'attachment',
12041210
attach_id => $attach->id});
12051211
}
1212+
push @log_attachments, $attach;
1213+
12061214
$attachments{$attach->id} =
12071215
$self->_attachment_to_hash($attach, $params);
12081216
}
12091217

1218+
if (Bugzilla->user->id) {
1219+
foreach my $attachment (@log_attachments) {
1220+
Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get");
1221+
}
1222+
}
1223+
12101224
return { bugs => \%bugs, attachments => \%attachments };
12111225
}
12121226

attachment.cgi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ sub view {
435435
}
436436
}
437437
}
438+
Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get")
439+
if Bugzilla->user->id;
438440
print $cgi->header(-type=>"$contenttype; name=\"$filename\"",
439441
-content_disposition=> "$disposition; filename=\"$filename\"",
440442
-content_length => $attachment->datasize);
@@ -669,6 +671,8 @@ sub edit {
669671
$vars->{'attachment'} = $attachment;
670672
$vars->{'attachments'} = $bugattachments;
671673

674+
Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get")
675+
if Bugzilla->user->id;
672676
print $cgi->header();
673677

674678
# Generate and return the UI (HTML page) from the appropriate template.

show_bug.cgi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ foreach ($cgi->param("excludefield")) {
133133

134134
$vars->{'displayfields'} = \%displayfields;
135135

136+
if ($user->id) {
137+
foreach my $bug_id (@bugids) {
138+
Bugzilla->log_user_request($bug_id, undef, 'bug-get');
139+
}
140+
}
136141
print $cgi->header($format->{'ctype'});
137142

138143
$template->process($format->{'template'}, $vars)

template/en/default/admin/params/admin.html.tmpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@
4040
"will ever happen."
4141

4242
last_visit_keep_days => "This option controls how many days $terms.Bugzilla will " _
43-
"remember when users visit specific ${terms.bugs}."}
43+
"remember when users visit specific ${terms.bugs}.",
44+
45+
log_user_requests => "This option controls logging of authenticated requests in the user_request_log table"}
4446
%]

userprefs.cgi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,12 @@ sub SaveApiKey {
853853
revoked => $revoked,
854854
});
855855
$api_key->update();
856+
if ($revoked) {
857+
Bugzilla->log_user_request(undef, undef, 'api-key-revoke')
858+
}
859+
else {
860+
Bugzilla->log_user_request(undef, undef, 'api-key-unrevoke')
861+
}
856862
}
857863
}
858864
}
@@ -912,6 +918,7 @@ sub MfaApiKey {
912918
revoked => 0,
913919
});
914920
$api_key->update();
921+
Bugzilla->log_user_request(undef, undef, 'api-key-unrevoke');
915922
$dbh->bz_commit_transaction;
916923
}
917924
}
@@ -926,6 +933,8 @@ sub _create_api_key {
926933
description => $description,
927934
});
928935

936+
Bugzilla->log_user_request(undef, undef, 'api-key-create');
937+
929938
# As a security precaution, we always sent out an e-mail when
930939
# an API key is created
931940
my $template = Bugzilla->template_inner($user->setting('lang'));

0 commit comments

Comments
 (0)