summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Sentry.pm
blob: 007736cea51170ff5ec705e48775dbfe280c94e4 (plain)
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
# 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::Sentry;

use 5.10.1;
use strict;
use warnings;

use base qw(Exporter);
our @EXPORT = qw(
    sentry_handle_error
    sentry_should_notify
);

use Carp;
use DateTime;
use File::Temp;
use JSON ();
use List::MoreUtils qw( any );
use LWP::UserAgent;
use Sys::Hostname;
use URI;
use URI::QueryParam;

use Bugzilla::Constants;
use Bugzilla::RNG qw(irand);
use Bugzilla::Util;
use Bugzilla::WebService::Constants;

use constant CONFIG => {
    # 'codes' lists the code-errors which are sent to sentry
    codes => [qw(
        bug_error
        chart_datafile_corrupt
        chart_dir_nonexistent
        chart_file_open_fail
        illegal_content_type_method
        jobqueue_insert_failed
        ldap_bind_failed
        mail_send_error
        template_error
        token_generation_error
    )],

    # any error/warning messages matching these regex's will not be logged or
    # sent to sentry
    ignore => [
        qr/^compiled template :\s*$/,
        qr/^Use of uninitialized value \$compiled in concatenation \(\.\) or string/,
    ],

    # any error/warning messages matching these regex's will be logged but not
    # sent to sentry
    sentry_ignore => [
        qr/Software caused connection abort/,
        qr/Could not check out .*\/cvsroot/,
        qr/Unicode character \S+ is illegal/,
        qr/Lost connection to MySQL server during query/,
        qr/Call me again when you have some data to chart/,
        qr/relative paths are not allowed/,
        qr/Illegal mix of collations for operation/,
    ],

    # (ab)use the logger to classify error/warning types
    logger => [
        {
            match => [
                qr/DBD::mysql/,
                qr/Can't connect to the database/,
            ],
            logger => 'database_error',
        },
        {
            match  => [ qr/PatchReader/ ],
            logger => 'patchreader',
        },
        {
            match  => [ qr/Use of uninitialized value/ ],
            logger => 'uninitialized_warning',
        },
    ],
};

sub sentry_generate_id {
    return sprintf('%04x%04x%04x%04x%04x%04x%04x%04x',
        irand(0xffff), irand(0xffff),
        irand(0xffff),
        irand(0x0fff) | 0x4000,
        irand(0x3fff) | 0x8000,
        irand(0xffff), irand(0xffff), irand(0xffff)
    );
}

sub sentry_should_notify {
    my $code_error = shift;
    return grep { $_ eq $code_error } @{ CONFIG->{codes} };
}

sub sentry_handle_error {
    my $level = shift;
    my @message = split(/\n/, shift);
    my $id = sentry_generate_id();

    my $is_error = $level eq 'error';
    if ($level ne 'error' && $level ne 'warning') {
        # it's a code-error
        return 0 unless sentry_should_notify($level);
        $is_error = 1;
        $level = 'error';
    }

    # build traceback
    my $traceback;
    {
        # for now don't show function arguments, in case they contain
        # confidential data.  waiting on bug 700683
        #local $Carp::MaxArgLen  = 256;
        #local $Carp::MaxArgNums = 0;
        local $Carp::MaxArgNums = -1;
        local $Carp::CarpInternal{'CGI::Carp'} = 1;
        local $Carp::CarpInternal{'Bugzilla::Error'} = 1;
        local $Carp::CarpInternal{'Bugzilla::Sentry'} = 1;
        $traceback = trim(Carp::longmess());
    }

    # strip timestamp
    foreach my $line (@message) {
        $line =~ s/^\[[^\]]+\] //;
    }
    my $message = join(" ", map { trim($_) } grep { $_ ne '' } @message);

    # message content filtering
    foreach my $re (@{ CONFIG->{ignore} }) {
        return 0 if $message =~ $re;
    }

    # determine logger
    my $logger;
    foreach my $config (@{ CONFIG->{logger} }) {
        foreach my $re (@{ $config->{match} }) {
            if ($message =~ $re) {
                $logger = $config->{logger};
                last;
            }
        }
        last if $logger;
    }
    $logger ||= $level;

    # don't send to sentry unless configured
    my $send_to_sentry = Bugzilla->params->{sentry_uri} ? 1 : 0;

    # web service filtering
    if ($send_to_sentry
        && (Bugzilla->error_mode == ERROR_MODE_DIE_SOAP_FAULT || Bugzilla->error_mode == ERROR_MODE_JSON_RPC))
    {
        my ($code) = $message =~ /^(-?\d+): /;
        if ($code
            && !($code == ERROR_UNKNOWN_FATAL || $code == ERROR_UNKNOWN_TRANSIENT))
        {
            $send_to_sentry = 0;
        }
    }

    # message content filtering
    if ($send_to_sentry) {
        foreach my $re (@{ CONFIG->{sentry_ignore} }) {
            if ($message =~ $re) {
                $send_to_sentry = 0;
                last;
            }
        }
    }

    # invalid boolean search errors need special handling
    if ($message =~ /selectcol_arrayref failed: syntax error/
        && $message =~ /IN BOOLEAN MODE/
        && $message =~ /Bugzilla\/Search\.pm/)
    {
        $send_to_sentry = 0;
    }

    # for now, don't send patchreader errors to sentry
    $send_to_sentry = 0
        if $logger eq 'patchreader';

    # log to apache's error_log
    if ($send_to_sentry) {
        _write_to_error_log("$message [#$id]", $is_error);
    } else {
        $traceback =~ s/\n/ /g;
        _write_to_error_log("$message $traceback", $is_error);
    }

    return 0 unless $send_to_sentry;

    my $user_data = undef;
    eval {
        my $user = Bugzilla->user;
        if ($user->id) {
            $user_data = {
                id   => $user->login,
                name => $user->name,
            };
        }
    };

    my $uri = URI->new(Bugzilla->cgi->self_url);
    $uri->query(undef);

    # sanitise

    # sanitise these query-string params
    # names are checked as-is as well as prefixed by BUGZILLA_
    my @sanitise_params = qw( PASSWORD TOKEN API_KEY );

    # remove these ENV vars
    my @sanitise_vars = qw( HTTP_COOKIE HTTP_X_BUGZILLA_PASSWORD HTTP_X_BUGZILLA_API_KEY HTTP_X_BUGZILLA_TOKEN );

    foreach my $var (qw( QUERY_STRING REDIRECT_QUERY_STRING )) {
        next unless exists $ENV{$var};
        my @pairs = split('&', $ENV{$var});
        foreach my $pair (@pairs) {
            next unless $pair =~ /^([^=]+)=(.+)$/;
            my ($param, $value) = ($1, $2);
            if (any { uc($param) eq $_ || uc($param) eq "BUGZILLA_$_" } @sanitise_params) {
                $value = '*';
            }
            $pair = $param . '=' . $value;
        }
        $ENV{$var} = join('&', @pairs);
    }
    foreach my $var (qw( REQUEST_URI HTTP_REFERER )) {
        next unless exists $ENV{$var};
        my $uri = URI->new($ENV{$var});
        foreach my $param ($uri->query_param) {
            if (any { uc($param) eq $_ || uc($param) eq "BUGZILLA_$_" } @sanitise_params) {
                $uri->query_param($param, '*');
            }
        }
        $ENV{$var} = $uri->as_string;
    }
    foreach my $var (@sanitise_vars) {
        delete $ENV{$var};
    }

    my $now = DateTime->now();
    my $data = {
        event_id    => $id,
        message     => $message,
        timestamp   => $now->iso8601(),
        level       => $level,
        platform    => 'Other',
        logger      => $logger,
        server_name => hostname(),
        'sentry.interfaces.User' => $user_data,
        'sentry.interfaces.Http' => {
            url             => $uri->as_string,
            method          => $ENV{REQUEST_METHOD},
            query_string    => $ENV{QUERY_STRING},
            env             => \%ENV,
        },
        extra       => {
            stacktrace      => $traceback,
        },
    };

    my $fh = File::Temp->new(
        DIR      => bz_locations()->{error_reports},
        TEMPLATE => $now->ymd('') . $now->hms('') . '-XXXX',
        SUFFIX   => '.dump',
        UNLINK   => 0,

    );
    if (!$fh) {
        warn "Failed to create dump file: $!\n";
        return;
    }
    print $fh JSON->new->utf8(1)->pretty(0)->allow_nonref(1)->encode($data);
    close($fh);
    return 1;
}

sub _write_to_error_log {
    my ($message, $is_error) = @_;
    if ($ENV{MOD_PERL}) {
        require Apache2::Log;
        if ($is_error) {
            Apache2::ServerRec::log_error($message);
        } else {
            Apache2::ServerRec::warn($message);
        }
    } else {
        print STDERR $message, "\n";
    }
}

# lifted from Bugzilla::Error
sub _in_eval {
    my $in_eval = 0;
    for (my $stack = 1; my $sub = (caller($stack))[3]; $stack++) {
        last if $sub =~ /^ModPerl/;
        last if $sub =~ /^Bugzilla::Template/;
        $in_eval = 1 if $sub =~ /^\(eval\)/;
    }
    return $in_eval;
}

sub _sentry_die_handler {
    my $message = shift;
    $message =~ s/^undef error - //;

    # avoid recursion, and check for CGI::Carp::die failures
    my $in_cgi_carp_die = 0;
    for (my $stack = 1; my $sub = (caller($stack))[3]; $stack++) {
        return if $sub =~ /:_sentry_die_handler$/;
        $in_cgi_carp_die = 1 if $sub =~ /CGI::Carp::die$/;
    }

    return if $Bugzilla::Template::is_processing;
    return if _in_eval();

    # mod_perl overrides exit to call die with this string
    exit if $message =~ /\bModPerl::Util::exit\b/;

    my $nested_error = '';
    my $is_compilation_failure = $message =~ /\bcompilation (aborted|failed)\b/i;

    # if we are called via CGI::Carp::die chances are something is seriously
    # wrong, so skip trying to use ThrowTemplateError
    if (!$in_cgi_carp_die && !$is_compilation_failure) {
        eval {
            my $cgi = Bugzilla->cgi;
            $cgi->close_standby_message('text/html', 'inline', 'error', 'html');
            Bugzilla::Error::ThrowTemplateError($message);
            print $cgi->multipart_final() if $cgi->{_multipart_in_progress};
        };
        $nested_error = $@ if $@;
    }

    if ($is_compilation_failure ||
        $in_cgi_carp_die ||
        ($nested_error && $nested_error !~ /\bModPerl::Util::exit\b/)
    ) {
        sentry_handle_error('error', $message);

        # and call the normal error management
        # (ISE for web pages, error response for web services, etc)
        CORE::die($message);
    }
    exit;
}

sub install_sentry_handler {
    require CGI::Carp;
    CGI::Carp::set_die_handler(\&_sentry_die_handler);
    $main::SIG{__WARN__} = sub {
        return if _in_eval();
        sentry_handle_error('warning', shift);
    };
}

BEGIN {
    if ($ENV{SCRIPT_NAME} || $ENV{MOD_PERL}) {
        install_sentry_handler();
    }
}

1;