]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1196626 - log all authenticated requests
authorDylan William Hardison <dylan@hardison.net>
Thu, 15 Oct 2015 22:58:09 +0000 (18:58 -0400)
committerDylan William Hardison <dylan@hardison.net>
Thu, 15 Oct 2015 22:58:09 +0000 (18:58 -0400)
Bugzilla.pm
Bugzilla/Attachment/PatchReader.pm
Bugzilla/Config/Admin.pm
Bugzilla/Search.pm
Bugzilla/WebService/Bug.pm
attachment.cgi
show_bug.cgi
template/en/default/admin/params/admin.html.tmpl
userprefs.cgi

index fa95128d1a6d130489f2c1850a54ed7e3f8a2f32..b14b92e0d5c58cd2946482a682264c55718da5b0 100644 (file)
@@ -594,6 +594,45 @@ sub switch_to_main_db {
     return $class->dbh_main;
 }
 
+sub log_user_request {
+    my ($class, $bug_id, $attach_id, $action) = @_;
+
+    return unless 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;
+    my $user_agent  = $cgi->user_agent // '';
+    my $script_name = $cgi->script_name;
+    my $server      = "web";
+
+    if ($script_name =~ /rest\.cgi/) {
+        $server = $script_name =~ /BzAPI/ ? "bzapi" : "rest";
+    }
+    elsif ($script_name =~ /xmlrpc\.cgi/) {
+        $server = "xmlrpc";
+    }
+    elsif ($script_name =~ /jsonrpc\.cgi/) {
+        $server = "jsonrpc";
+    }
+
+    my @params = ($user_id, remote_ip(), $user_agent, $request_url, $method, $bug_id, $attach_id, $action, $server);
+    foreach my $param (@params) {
+        trick_taint($param) if defined $param;
+    }
+
+    eval {
+        local $class->request_cache->{dbh};
+        $class->switch_to_main_db();
+        $class->dbh->do("INSERT INTO user_request_log
+                         (user_id, ip_address, user_agent, request_url,
+                         method, timestamp, bug_id, attach_id, action, server)
+                         VALUES (?, ?, ?, ?, ?, NOW(), ?, ?, ?, ?)", undef, @params);
+    };
+    warn $@ if $@;
+}
+
 sub is_shadow_db {
     my $class = shift;
     return $class->request_cache->{dbh} != $class->dbh_main;
index 1ab14f386e858691a184cd7dbb084c2f183af29e..2c164773668b2f197ae1bfb7ef6ad91ec952e9ce 100644 (file)
@@ -38,6 +38,9 @@ sub process_diff {
     if ($format eq 'raw') {
         require Bugzilla::PatchReader::DiffPrinter::raw;
         $last_reader->sends_data_to(new Bugzilla::PatchReader::DiffPrinter::raw());
+
+        Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get")
+          if Bugzilla->user->id;
         # Actually print out the patch.
         print $cgi->header(-type => 'text/plain',
                            -expires => '+3M');
@@ -93,6 +96,12 @@ sub process_interdiff {
     my $lc  = Bugzilla->localconfig;
     my $vars = {};
 
+    if (Bugzilla->user->id) {
+        foreach my $attachment ($old_attachment, $new_attachment) {
+            Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get");
+        }
+    }
+
     # Encode attachment data as utf8 if it's going to be displayed in a HTML
     # page using the UTF-8 encoding.
     if ($format ne 'raw' && Bugzilla->params->{'utf8'}) {
index 769e3170bef86f22a0b3f7c82071a14ea25be7b0..b0c0bad9a7ed23aa553c1450d92142bfa6d1886c 100644 (file)
@@ -63,6 +63,12 @@ sub get_param_list {
    type => 't',
    default => 10,
    checker => \&check_numeric
+  },
+
+  {
+    name => 'log_user_requests',
+    type => 'b',
+    default => 0,
   });
   return @param_list;
 }
index 46d959c3c35f0d15346775ca0addacfb823e45bc..ff0db1baae651a5bc8a8ebfa4ad81e4eacc10aaf 100644 (file)
@@ -787,6 +787,7 @@ sub data {
     return $self->{data} if $self->{data};
     my $dbh = Bugzilla->dbh;
 
+    Bugzilla->log_user_request(undef, undef, "search") if Bugzilla->user->id;
     # If all fields belong to the 'bugs' table, there is no need to split
     # the original query into two pieces. Else we override the 'fields'
     # argument to first get bug IDs based on the search criteria defined
index d0fe8465f8f8fb1cbf01f9fe46f510627376065c..d7a1d8f9bb5fdee524f2bb342a4573a63d890f2e 100644 (file)
@@ -458,6 +458,11 @@ sub get {
 
     $self->_add_update_tokens($params, \@bugs, \@hashes);
 
+    if (Bugzilla->user->id) {
+        foreach my $bug (@bugs) {
+            Bugzilla->log_user_request($bug->id, undef, 'bug-get');
+        }
+    }
     return { bugs => \@hashes, faults => \@faults };
 }
 
@@ -1196,6 +1201,7 @@ sub attachments {
     }
 
     my %attachments;
+    my @log_attachments;
     foreach my $attach (@{Bugzilla::Attachment->new_from_list($attach_ids)}) {
         Bugzilla::Bug->check($attach->bug_id);
         if ($attach->isprivate && !Bugzilla->user->is_insider) {
@@ -1203,10 +1209,18 @@ sub attachments {
                                             object    => 'attachment',
                                             attach_id => $attach->id});
         }
+        push @log_attachments, $attach;
+
         $attachments{$attach->id} =
             $self->_attachment_to_hash($attach, $params);
     }
 
+    if (Bugzilla->user->id) {
+        foreach my $attachment (@log_attachments) {
+            Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get");
+        }
+    }
+
     return { bugs => \%bugs, attachments => \%attachments };
 }
 
index 78023560df3c4dbfb22e9e255bafd229e1c950bc..104ee0ca8cf8c67ca1afb8a83e3cf86ff2ab6b21 100755 (executable)
@@ -435,6 +435,8 @@ sub view {
             }
         }
     }
+    Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get")
+      if Bugzilla->user->id;
     print $cgi->header(-type=>"$contenttype; name=\"$filename\"",
                        -content_disposition=> "$disposition; filename=\"$filename\"",
                        -content_length => $attachment->datasize);
@@ -669,6 +671,8 @@ sub edit {
   $vars->{'attachment'} = $attachment;
   $vars->{'attachments'} = $bugattachments;
 
+  Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get")
+    if Bugzilla->user->id;
   print $cgi->header();
 
   # Generate and return the UI (HTML page) from the appropriate template.
index 06d17e3529609e459b6a8c856019e273932a6396..fbcf4e82807b955a54722f425a068d30c9960368 100755 (executable)
@@ -133,6 +133,11 @@ foreach ($cgi->param("excludefield")) {
 
 $vars->{'displayfields'} = \%displayfields;
 
+if ($user->id) {
+    foreach my $bug_id (@bugids) {
+        Bugzilla->log_user_request($bug_id, undef, 'bug-get');
+    }
+}
 print $cgi->header($format->{'ctype'});
 
 $template->process($format->{'template'}, $vars)
index f84dbc7011714997ee928c20a67419750e7f4784..df0580783c90ccfa00f86e05f27fa6d5ed70eedb 100644 (file)
@@ -40,5 +40,7 @@
                        "will ever happen."
 
   last_visit_keep_days => "This option controls how many days $terms.Bugzilla will " _
-                          "remember when users visit specific ${terms.bugs}."}
+                          "remember when users visit specific ${terms.bugs}.",
+
+   log_user_requests => "This option controls logging of authenticated requests in the user_request_log table"}
 %]
index 6c6a246ff72dbd13231411bcb13b9fed1b1ef828..bd1bb8ab7694f8401a3c53b350200b6c7eb5490e 100755 (executable)
@@ -853,6 +853,12 @@ sub SaveApiKey {
                     revoked     => $revoked,
                 });
                 $api_key->update();
+                if ($revoked) {
+                    Bugzilla->log_user_request(undef, undef, 'api-key-revoke')
+                }
+                else {
+                    Bugzilla->log_user_request(undef, undef, 'api-key-unrevoke')
+                }
             }
         }
     }
@@ -912,6 +918,7 @@ sub MfaApiKey {
                 revoked     => 0,
             });
             $api_key->update();
+            Bugzilla->log_user_request(undef, undef, 'api-key-unrevoke');
             $dbh->bz_commit_transaction;
         }
     }
@@ -926,6 +933,8 @@ sub _create_api_key {
         description => $description,
     });
 
+    Bugzilla->log_user_request(undef, undef, 'api-key-create');
+
     # As a security precaution, we always sent out an e-mail when
     # an API key is created
     my $template = Bugzilla->template_inner($user->setting('lang'));