-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Bugzilla.pm
1185 lines (918 loc) · 34 KB
/
Bugzilla.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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.
package Bugzilla;
use 5.10.1;
use strict;
use warnings;
use Bugzilla::Logging;
our $VERSION = '20200805.1';
use Bugzilla::Auth;
use Bugzilla::Auth::Persist::Cookie;
use Bugzilla::CGI;
use Bugzilla::Config;
use Bugzilla::Constants;
use Bugzilla::DB;
use Bugzilla::Error;
use Bugzilla::Extension;
use Bugzilla::Field;
use Bugzilla::Flag;
use Bugzilla::Hook;
use Bugzilla::Install::Localconfig qw(read_localconfig);
use Bugzilla::Localconfig;
use Bugzilla::Install::Util qw(init_console include_languages);
use Bugzilla::Memcached;
use Bugzilla::Template;
use Bugzilla::Token;
use Bugzilla::User;
use Bugzilla::Util;
use Bugzilla::CPAN;
use Bugzilla::Bloomfilter;
use Date::Parse;
use DateTime::TimeZone;
use Encode;
use File::Basename;
use File::Spec::Functions;
use Safe;
use JSON::XS qw(decode_json);
use Scope::Guard;
use parent qw(Bugzilla::CPAN);
#####################################################################
# Constants
#####################################################################
# This is identical to Install::Util::_cache so that things loaded
# into Install::Util::_cache during installation can be read out
# of request_cache later in installation.
use constant request_cache => Bugzilla::Install::Util::_cache();
#####################################################################
# Global Code
#####################################################################
# Note that this is a raw subroutine, not a method, so $class isn't available.
# Called prior to every Bugzilla CGI request (not called for native Mojo
# routes).
sub init_page {
if (Bugzilla->usage_mode == USAGE_MODE_CMDLINE) {
init_console();
}
elsif (Bugzilla->params->{'utf8'}) {
binmode STDOUT, ':utf8';
}
if (i_am_cgi()) {
Bugzilla::Logging->fields->{remote_ip} = remote_ip();
}
# Because this function is run live from Perl "use" commands of
# other scripts, we're skipping the rest of this function if we get here
# during a Perl syntax check (perl -c, like we do during the
# 001compile.t test).
return if $^C;
my $script = basename($0);
# Because of attachment_base, attachment.cgi handles this itself.
if ($script ne 'attachment.cgi') {
do_ssl_redirect_if_required();
}
}
#####################################################################
# Subroutines and Methods
#####################################################################
my $preload_templates = 0;
sub preload_templates {
$preload_templates = 1;
delete request_cache->{template};
template();
return 1;
}
sub template {
request_cache->{template}
||= Bugzilla::Template->create(preload => $preload_templates);
request_cache->{template}->{_is_main} = 1;
return request_cache->{template};
}
sub template_inner {
my (undef, $lang) = @_;
my $cache = request_cache;
my $current_lang = $cache->{template_current_lang}->[0];
$lang ||= $current_lang || '';
my %options = (language => $lang, preload => $preload_templates);
return $cache->{"template_inner_$lang"}
||= Bugzilla::Template->create(%options);
}
sub extensions {
state $extensions;
return $extensions if $extensions;
my $extension_packages = Bugzilla::Extension->load_all();
$extensions = [];
foreach my $package (@$extension_packages) {
if ($package->enabled) {
push @$extensions, $package;
}
}
return $extensions;
}
sub cgi {
return request_cache->{cgi} ||= Bugzilla::CGI->new;
}
sub input_params {
my ($class, $params) = @_;
my $cache = request_cache;
# This is how the WebService and other places set input_params.
if (defined $params) {
$cache->{input_params} = $params;
}
return $cache->{input_params} if defined $cache->{input_params};
# Making this scalar makes it a tied hash to the internals of $cgi,
# so if a variable is changed, then it actually changes the $cgi object
# as well.
$cache->{input_params} = $class->cgi->Vars;
return $cache->{input_params};
}
sub localconfig {
return $_[0]->process_cache->{localconfig}
||= Bugzilla::Localconfig->new(read_localconfig());
}
sub params {
return request_cache->{params} ||= Bugzilla::Config::read_param_file();
}
sub get_param_with_override {
my ($class, $name) = @_;
return $class->localconfig->param_override->{$name} // $class->params->{$name};
}
sub user {
return request_cache->{user} ||= new Bugzilla::User;
}
sub set_user {
my (undef, $new_user, %option) = @_;
if ($option{scope_guard}) {
my $old_user = request_cache->{user};
request_cache->{user} = $new_user;
return Scope::Guard->new(sub {
request_cache->{user} = $old_user;
});
}
else {
request_cache->{user} = $new_user;
}
}
sub sudoer {
return request_cache->{sudoer};
}
sub sudo_request {
my (undef, $new_user, $new_sudoer) = @_;
request_cache->{user} = $new_user;
request_cache->{sudoer} = $new_sudoer;
# NOTE: If you want to log the start of an sudo session, do it here.
}
sub page_requires_login {
return request_cache->{page_requires_login};
}
sub github_secret {
my ($class) = @_;
my $cache = request_cache;
my $cgi = $class->cgi;
$cache->{github_secret} //= $cgi->cookie('github_secret')
// generate_random_password(256);
return $cache->{github_secret};
}
sub assert_password_is_secure {
my ($class, $password1) = @_;
}
sub assert_passwords_match {
my ($class, $password1, $password2) = @_;
ThrowUserError('password_mismatch') if $password1 ne $password2;
}
sub login {
my ($class, $type) = @_;
return $class->user if $class->user->id;
my $authorizer = new Bugzilla::Auth();
$type = LOGIN_REQUIRED if $class->cgi->param('GoAheadAndLogIn');
if (!defined $type || $type == LOGIN_NORMAL) {
$type = $class->params->{'requirelogin'} ? LOGIN_REQUIRED : LOGIN_NORMAL;
}
# Allow templates to know that we're in a page that always requires
# login.
if ($type == LOGIN_REQUIRED) {
request_cache->{page_requires_login} = 1;
}
my $authenticated_user = $authorizer->login($type);
if (i_am_cgi() && $authenticated_user->id) {
Bugzilla::Logging->fields->{user_id} = $authenticated_user->id;
}
# At this point, we now know if a real person is logged in.
# Check if a password reset is required
my $cgi = Bugzilla->cgi;
my $script_name = $cgi->script_name;
my $do_logout = $cgi->param('logout');
my $on_token_page = $script_name eq '/token.cgi';
if ($authenticated_user->password_change_required) {
# We cannot show the password reset UI for API calls, so treat those as
# a disabled account.
if (i_am_webservice()) {
ThrowUserError("account_disabled",
{disabled_reason => $authenticated_user->password_change_reason});
}
# only allow the reset-password and token pages to handle requests
# (tokens handles the 'forgot password' process)
# otherwise redirect user to the reset-password page.
if ($script_name !~ m#/(?:reset_password|token)\.cgi$# && !$do_logout) {
my $self_url = trim($cgi->self_url);
my $sig_type = 'prev_url:' . $authenticated_user->id;
my $self_url_sig = issue_hash_sig($sig_type, $self_url);
my $redir_url = URI->new('reset_password.cgi');
$redir_url->query_form(prev_url => $self_url, prev_url_sig => $self_url_sig);
$cgi->base_redirect($redir_url->as_string);
}
}
elsif (!i_am_webservice()
&& $authenticated_user->in_mfa_group
&& !$authenticated_user->mfa)
{
# decide if the user needs a warning or to be blocked.
my $date = $authenticated_user->mfa_required_date('UTC');
my $grace_period = Bugzilla->params->{mfa_group_grace_period};
my $expired = defined $date && $date < DateTime->now;
my $on_mfa_page
= $script_name eq '/userprefs.cgi' && $cgi->param('tab') eq 'mfa';
Bugzilla->request_cache->{mfa_warning} = 1;
Bugzilla->request_cache->{mfa_grace_period_expired} = $expired;
Bugzilla->request_cache->{on_mfa_page} = $on_mfa_page;
if ($grace_period == 0 || $expired) {
if (!($on_mfa_page || $on_token_page || $do_logout)) {
$cgi->base_redirect('userprefs.cgi?tab=mfa');
}
}
else {
my $dbh = Bugzilla->dbh_main;
my $date = $dbh->sql_date_math('NOW()', '+', '?', 'DAY');
my ($mfa_required_date)
= $dbh->selectrow_array("SELECT $date", undef, $grace_period);
$authenticated_user->set_mfa_required_date($mfa_required_date);
$authenticated_user->update();
}
}
# We must now check to see if an sudo session is in progress.
# For a session to be in progress, the following must be true:
# 1: There must be a logged in user
# 2: That user must be in the 'bz_sudoer' group
# 3: There must be a valid value in the 'sudo' cookie
# 4: A Bugzilla::User object must exist for the given cookie value
# 5: That user must NOT be in the 'bz_sudo_protect' group
my $token = $class->cgi->cookie('sudo');
if (defined $authenticated_user && $token) {
my ($user_id, $date, $sudo_target_id) = Bugzilla::Token::GetTokenData($token);
if (!$user_id
|| $user_id != $authenticated_user->id
|| !detaint_natural($sudo_target_id)
|| (time() - str2time($date) > MAX_SUDO_TOKEN_AGE))
{
$class->cgi->remove_cookie('sudo');
ThrowUserError('sudo_invalid_cookie');
}
my $sudo_target = new Bugzilla::User($sudo_target_id);
if ( $authenticated_user->in_group('bz_sudoers')
&& defined $sudo_target
&& !$sudo_target->in_group('bz_sudo_protect'))
{
$class->set_user($sudo_target);
request_cache->{sudoer} = $authenticated_user;
# And make sure that both users have the same Auth object,
# since we never call Auth::login for the sudo target.
$sudo_target->set_authorizer($authenticated_user->authorizer);
# NOTE: If you want to do any special logging, do it here.
}
else {
delete_token($token);
$class->cgi->remove_cookie('sudo');
ThrowUserError('sudo_illegal_action',
{sudoer => $authenticated_user, target_user => $sudo_target});
}
}
else {
$class->set_user($authenticated_user);
}
if (Bugzilla->sudoer) {
Bugzilla->sudoer->update_last_seen_date();
}
else {
$class->user->update_last_seen_date();
}
# If Mojo native app is requesting login, we need to possibly redirect
my $C = $Bugzilla::App::CGI::C;
my $session = $C->session;
if (!$on_token_page && $session->{override_login_target}) {
my $override_login_target = delete $session->{override_login_target};
my $cgi_params = delete $session->{cgi_params};
my $mojo_url = Mojo::URL->new($override_login_target);
$mojo_url->query($cgi_params);
$C->redirect_to($mojo_url);
}
return $class->user;
}
sub logout {
my ($class, $option) = @_;
# If we're not logged in, go away
return unless $class->user->id;
$option = LOGOUT_CURRENT unless defined $option;
Bugzilla::Auth::Persist::Cookie->logout({type => $option});
$class->logout_request() unless $option eq LOGOUT_KEEP_CURRENT;
}
sub logout_user {
my ($class, $user) = @_;
# When we're logging out another user we leave cookies alone, and
# therefore avoid calling Bugzilla->logout() directly.
Bugzilla::Auth::Persist::Cookie->logout({user => $user});
}
# just a compatibility front-end to logout_user that gets a user by id
sub logout_user_by_id {
my ($class, $id) = @_;
my $user = new Bugzilla::User($id);
$class->logout_user($user);
}
# hack that invalidates credentials for a single request
sub logout_request {
my $class = shift;
delete request_cache->{user};
delete request_cache->{sudoer};
# We can't delete from $cgi->cookie, so logincookie data will remain
# there. Don't rely on it: use Bugzilla->user->login instead!
}
sub job_queue {
require Bugzilla::JobQueue;
return request_cache->{job_queue} ||= Bugzilla::JobQueue->new();
}
sub jwt {
my ($class, @args) = @_;
require Mojo::JWT;
return Mojo::JWT->new(@args, secret => $class->localconfig->jwt_secret);
}
sub dbh {
my ($class) = @_;
# If we're not connected, then we must want the main db
return request_cache->{dbh} ||= $class->dbh_main;
}
sub dbh_main {
return request_cache->{dbh_main} ||= Bugzilla::DB::connect_main();
}
sub languages {
return Bugzilla::Install::Util::supported_languages();
}
sub current_language {
return request_cache->{current_language} ||= (include_languages())[0];
}
sub error_mode {
my (undef, $newval) = @_;
if (defined $newval) {
request_cache->{error_mode} = $newval;
}
return request_cache->{error_mode}
|| (i_am_cgi() ? ERROR_MODE_WEBPAGE : ERROR_MODE_DIE);
}
# This is used only by Bugzilla::Error to throw errors.
sub _json_server {
my (undef, $newval) = @_;
if (defined $newval) {
request_cache->{_json_server} = $newval;
}
return request_cache->{_json_server};
}
sub usage_mode {
my ($class, $newval) = @_;
if (defined $newval) {
if ($newval == USAGE_MODE_BROWSER) {
$class->error_mode(ERROR_MODE_WEBPAGE);
}
elsif ($newval == USAGE_MODE_CMDLINE) {
$class->error_mode(ERROR_MODE_DIE);
}
elsif ($newval == USAGE_MODE_XMLRPC) {
$class->error_mode(ERROR_MODE_DIE_SOAP_FAULT);
}
elsif ($newval == USAGE_MODE_JSON) {
$class->error_mode(ERROR_MODE_JSON_RPC);
}
elsif ($newval == USAGE_MODE_EMAIL) {
$class->error_mode(ERROR_MODE_DIE);
}
elsif ($newval == USAGE_MODE_TEST) {
$class->error_mode(ERROR_MODE_TEST);
}
elsif ($newval == USAGE_MODE_REST) {
$class->error_mode(ERROR_MODE_REST);
}
elsif ($newval == USAGE_MODE_MOJO) {
$class->error_mode(ERROR_MODE_MOJO);
}
else {
ThrowCodeError('usage_mode_invalid', {'invalid_usage_mode', $newval});
}
request_cache->{usage_mode} = $newval;
}
return request_cache->{usage_mode}
|| (i_am_cgi() ? USAGE_MODE_BROWSER : USAGE_MODE_CMDLINE);
}
sub installation_mode {
my (undef, $newval) = @_;
(request_cache->{installation_mode} = $newval) if defined $newval;
return request_cache->{installation_mode} || INSTALLATION_MODE_INTERACTIVE;
}
sub installation_answers {
my (undef, $filename) = @_;
if ($filename) {
my $s = new Safe;
$s->rdo($filename);
die "Error reading $filename: $!" if $!;
die "Error evaluating $filename: $@" if $@;
# Now read the param back out from the sandbox
request_cache->{installation_answers} = $s->varglob('answer');
}
return request_cache->{installation_answers} || {};
}
sub switch_to_shadow_db {
my $class = shift;
if (!request_cache->{dbh_shadow}) {
if ($class->get_param_with_override('shadowdb')) {
request_cache->{dbh_shadow} = Bugzilla::DB::connect_shadow();
}
else {
request_cache->{dbh_shadow} = $class->dbh_main;
}
}
request_cache->{dbh} = request_cache->{dbh_shadow};
# we have to return $class->dbh instead of {dbh} as
# {dbh_shadow} may be undefined if no shadow DB is used
# and no connection to the main DB has been established yet.
return $class->dbh;
}
sub switch_to_main_db {
my $class = shift;
request_cache->{dbh} = $class->dbh_main;
return $class->dbh_main;
}
sub log_user_request {
my ($class, $bug_id, $attach_id, $action) = @_;
return
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;
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
);
eval {
local 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 request_cache->{dbh} != $class->dbh_main;
}
sub fields {
my (undef, $criteria) = @_;
$criteria ||= {};
my $cache = request_cache;
# We create an advanced cache for fields by type, so that we
# can avoid going back to the database for every fields() call.
# (And most of our fields() calls are for getting fields by type.)
#
# We also cache fields by name, because calling $field->name a few
# million times can be slow in calling code, but if we just do it
# once here, that makes things a lot faster for callers.
if (!defined $cache->{fields}) {
my @all_fields = Bugzilla::Field->get_all;
my (%by_name, %by_type);
foreach my $field (@all_fields) {
my $name = $field->name;
$by_type{$field->type}->{$name} = $field;
$by_name{$name} = $field;
}
$cache->{fields} = {by_type => \%by_type, by_name => \%by_name};
}
my $fields = $cache->{fields};
my %requested;
if (my $types = delete $criteria->{type}) {
$types = ref($types) ? $types : [$types];
%requested = map { %{$fields->{by_type}->{$_} || {}} } @$types;
}
else {
%requested = %{$fields->{by_name}};
}
my $do_by_name = delete $criteria->{by_name};
# Filtering before returning the fields based on
# the criterias.
foreach my $filter (keys %$criteria) {
foreach my $field (keys %requested) {
if ($requested{$field}->$filter != $criteria->{$filter}) {
delete $requested{$field};
}
}
}
return $do_by_name
? \%requested
: [sort { $a->sortkey <=> $b->sortkey || $a->name cmp $b->name }
values %requested];
}
sub active_custom_fields {
my (undef, $params, $wants) = @_;
my $cache_id = 'active_custom_fields';
my $can_cache = !exists $params->{bug_id} && !$wants;
if ($can_cache && $params) {
$cache_id .= ($params->{product} ? '_p' . $params->{product}->id : '')
. ($params->{component} ? '_c' . $params->{component}->id : '');
$cache_id .= ':noext' if $params->{skip_extensions};
}
if ($can_cache && exists request_cache->{$cache_id}) {
return @{request_cache->{$cache_id}};
}
else {
my $match_params = {custom => 1, obsolete => 0, skip_extensions => 1};
if ($wants) {
if ($wants->exclude_type->{custom}) {
return ();
}
elsif ($wants->is_specific) {
my @names = (grep {/^cf_/} $wants->includes);
return () unless @names;
$match_params->{name} = \@names;
}
}
my $fields = Bugzilla::Field->match($match_params);
Bugzilla::Hook::process('active_custom_fields',
{fields => \$fields, params => $params, wants => $wants});
request_cache->{$cache_id} = $fields if $can_cache;
return @$fields;
}
}
sub has_flags {
if (!defined request_cache->{has_flags}) {
request_cache->{has_flags} = Bugzilla::Flag->any_exist;
}
return request_cache->{has_flags};
}
sub local_timezone {
return $_[0]->process_cache->{local_timezone}
||= DateTime::TimeZone->new(name => 'local');
}
# Send messages to syslog for the auditing systems (e.g. mozdef) to pick up.
sub audit {
my (undef, $message) = @_;
state $logger = Log::Log4perl->get_logger("audit");
$logger->notice(encode_utf8($message));
}
sub clear_request_cache {
my (undef, %option) = @_;
my $request_cache = request_cache();
my @except = $option{except} ? @{$option{except}} : ();
%{$request_cache} = map { $_ => $request_cache->{$_} } @except;
}
# This is a per-process cache. Under mod_cgi it's identical to the
# request_cache. When using mod_perl, items in this cache live until the
# worker process is terminated.
our $_process_cache = {};
sub process_cache {
return $_process_cache;
}
# This is a memcached wrapper, which provides cross-process and cross-system
# caching.
sub memcached {
return request_cache->{memcached} ||= Bugzilla::Memcached->_new();
}
# Connector to the Datadog metrics collection daemon.
sub datadog {
my ($class, $namespace) = @_;
my $host = $class->localconfig->datadog_host;
my $port = $class->localconfig->datadog_port;
$namespace //= '';
if ($class->has_feature('datadog') && $host) {
require DataDog::DogStatsd;
return request_cache->{datadog}{$namespace} //= DataDog::DogStatsd->new(
host => $host,
port => $port,
namespace => $namespace ? "$namespace." : '',
);
}
else {
return undef;
}
}
sub check_rate_limit {
my ($class, $name, $ip, $throw_error) = @_;
$throw_error //= sub { ThrowUserError("rate_limit") };
my $params = Bugzilla->params;
if ($params->{rate_limit_active}) {
my $rules = decode_json($params->{rate_limit_rules});
my $limit = $rules->{$name};
unless ($limit) {
warn "no rules for $name!";
return 0;
}
if (Bugzilla->memcached->should_rate_limit("$name:$ip", @$limit)) {
my $action = 'block';
my $filter = Bugzilla::Bloomfilter->lookup("rate_limit_whitelist");
if ($filter && $filter->test($ip)) {
$action = 'ignore';
}
my $limit = join("/", @$limit);
Bugzilla->audit(
"[rate_limit] action=$action, ip=$ip, limit=$limit, name=$name");
if ($action eq 'block') {
$Bugzilla::App::CGI::C->block_ip($ip);
$throw_error->();
}
}
}
}
sub markdown {
require Bugzilla::Markdown;
state $markdown = Bugzilla::Markdown->new;
return $markdown;
}
# Private methods
# Per-process cleanup. Note that this is a plain subroutine, not a method,
# so we don't have $class available.
*cleanup = \&_cleanup;
sub _cleanup {
return if $^C;
# BMO - allow "end of request" processing
Bugzilla::Hook::process('request_cleanup');
Bugzilla::Bug->CLEANUP;
my $main = Bugzilla->request_cache->{dbh_main};
my $shadow = Bugzilla->request_cache->{dbh_shadow};
foreach my $dbh ($main, $shadow) {
next if !$dbh;
$dbh->bz_rollback_transaction() if $dbh->bz_in_transaction;
}
clear_request_cache();
Log::Log4perl::MDC->remove();
}
our ($caller_package, $caller_file) = caller;
init_page()
if $caller_package eq 'main'
&& $caller_package !~ /^Test/
&& $caller_file =~ /\.t$/;
END {
cleanup() if $caller_package eq 'main';
}
1;
__END__
=head1 NAME
Bugzilla - Semi-persistent collection of various objects used by scripts
and modules
=head1 SYNOPSIS
use Bugzilla;
sub someModulesSub {
Bugzilla->dbh->prepare(...);
Bugzilla->template->process(...);
}
=head1 DESCRIPTION
Several Bugzilla 'things' are used by a variety of modules and scripts. This
includes database handles, template objects, and so on.
This module is a singleton intended as a central place to store these objects.
This approach has several advantages:
=over 4
=item *
They're not global variables, so we don't have issues with them staying around
with mod_perl
=item *
Everything is in one central place, so it's easy to access, modify, and maintain
=item *
Code in modules can get access to these objects without having to have them
all passed from the caller, and the caller's caller, and....
=item *
We can reuse objects across requests using mod_perl where appropriate (e.g.
templates), whilst destroying those which are only valid for a single request
(such as the current user)
=back
Note that items accessible via this object are demand-loaded when requested.
For something to be added to this object, it should either be able to benefit
from persistence when run under mod_perl (such as the a C<template> object),
or should be something which is globally required by a large amount of code
(such as the current C<user> object).
=head1 METHODS
Note that all C<Bugzilla> functionality is method based; use C<Bugzilla-E<gt>dbh>
rather than C<Bugzilla::dbh>. Nothing cares about this now, but don't rely on
that.
=over 4
=item C<template>
The current C<Template> object, to be used for output
=item C<template_inner>
If you ever need a L<Bugzilla::Template> object while you're already
processing a template, use this. Also use it if you want to specify
the language to use. If no argument is passed, it uses the last
language set. If the argument is "" (empty string), the language is
reset to the current one (the one used by Bugzilla->template).
=item C<cgi>
The current C<cgi> object. Note that modules should B<not> be using this in
general. Not all Bugzilla actions are cgi requests. Its useful as a convenience
method for those scripts/templates which are only use via CGI, though.
=item C<input_params>
When running under the WebService, this is a hashref containing the arguments
passed to the WebService method that was called. When running in a normal
script, this is a hashref containing the contents of the CGI parameters.
Modifying this hashref will modify the CGI parameters or the WebService
arguments (depending on what C<input_params> currently represents).
This should be used instead of L</cgi> in situations where your code
could be being called by either a normal CGI script or a WebService method,
such as during a code hook.
B<Note:> When C<input_params> represents the CGI parameters, any
parameter specified more than once (like C<foo=bar&foo=baz>) will appear
as an arrayref in the hash, but any value specified only once will appear
as a scalar. This means that even if a value I<can> appear multiple times,
if it only I<does> appear once, then it will be a scalar in C<input_params>,
not an arrayref.
=item C<user>
Default C<Bugzilla::User> object if there is no currently logged in user or
if the login code has not yet been run. If an sudo session is in progress,
the C<Bugzilla::User> corresponding to the person who is being impersonated.
If no session is in progress, the current C<Bugzilla::User>.
=item C<set_user>
Allows you to directly set what L</user> will return. You can use this
if you want to bypass L</login> for some reason and directly "log in"
a specific L<Bugzilla::User>. Be careful with it, though!
=item C<sudoer>
C<undef> if there is no currently logged in user, the currently logged in user
is not in the I<sudoer> group, or there is no session in progress. If an sudo
session is in progress, returns the C<Bugzilla::User> object corresponding to
the person who logged in and initiated the session. If no session is in
progress, returns the C<Bugzilla::User> object corresponding to the currently
logged in user.
=item C<sudo_request>
This begins an sudo session for the current request. It is meant to be
used when a session has just started. For normal use, sudo access should
normally be set at login time.
=item C<login>
Logs in a user, returning a C<Bugzilla::User> object, or C<undef> if there is
no logged in user. See L<Bugzilla::Auth|Bugzilla::Auth>, and
L<Bugzilla::User|Bugzilla::User>.
=item C<page_requires_login>
If the current page always requires the user to log in (for example,
C<enter_bug.cgi> or any page called with C<?GoAheadAndLogIn=1>) then
this will return something true. Otherwise it will return false. (This is
set when you call L</login>.)
=item C<logout($option)>
Logs out the current user, which involves invalidating user sessions and
cookies. Three options are available from
L<Bugzilla::Constants|Bugzilla::Constants>: LOGOUT_CURRENT (the
default), LOGOUT_ALL or LOGOUT_KEEP_CURRENT.
=item C<logout_user($user)>
Logs out the specified user (invalidating all their sessions), taking a
Bugzilla::User instance.
=item C<logout_by_id($id)>
Logs out the user with the id specified. This is a compatibility
function to be used in callsites where there is only a userid and no
Bugzilla::User instance.
=item C<logout_request>
Essentially, causes calls to C<Bugzilla-E<gt>user> to return C<undef>. This has the
effect of logging out a user for the current request only; cookies and
database sessions are left intact.
=item C<fields>
This is the standard way to get arrays or hashes of L<Bugzilla::Field>
objects when you need them. It takes the following named arguments
in a hashref:
=over
=item C<by_name>
If false (or not specified), this method will return an arrayref of
the requested fields.
If true, this method will return a hashref of fields, where the keys
are field names and the values are L<Bugzilla::Field> objects.
=item C<type>
Either a single C<FIELD_TYPE_*> constant or an arrayref of them. If specified,
the returned fields will be limited to the types in the list. If you don't
specify this argument, all fields will be returned.