/
inboundfilters.ts
220 lines (198 loc) · 6.96 KB
/
inboundfilters.ts
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
import type { Event, EventProcessor, Hub, Integration, StackFrame } from '@sentry/types';
import { getEventDescription, logger, stringMatchesSomePattern } from '@sentry/utils';
// "Script error." is hard coded into browsers for errors that it can't read.
// this is the result of a script being pulled in from an external domain and CORS.
const DEFAULT_IGNORE_ERRORS = [/^Script error\.?$/, /^Javascript error: Script error\.? on line 0$/];
const DEFAULT_IGNORE_TRANSACTIONS = [
/^.*healthcheck.*$/,
/^.*healthy.*$/,
/^.*live.*$/,
/^.*ready.*$/,
/^.*heartbeat.*$/,
/^.*\/health$/,
/^.*\/healthz$/,
];
/** Options for the InboundFilters integration */
export interface InboundFiltersOptions {
allowUrls: Array<string | RegExp>;
denyUrls: Array<string | RegExp>;
ignoreErrors: Array<string | RegExp>;
ignoreTransactions: Array<string | RegExp>;
ignoreInternal: boolean;
disableErrorDefaults: boolean;
disableTransactionDefaults: boolean;
}
/** Inbound filters configurable by the user */
export class InboundFilters implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'InboundFilters';
/**
* @inheritDoc
*/
public name: string = InboundFilters.id;
public constructor(private readonly _options: Partial<InboundFiltersOptions> = {}) {}
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (processor: EventProcessor) => void, getCurrentHub: () => Hub): void {
const eventProcess: EventProcessor = (event: Event) => {
const hub = getCurrentHub();
if (hub) {
const self = hub.getIntegration(InboundFilters);
if (self) {
const client = hub.getClient();
const clientOptions = client ? client.getOptions() : {};
const options = _mergeOptions(self._options, clientOptions);
return _shouldDropEvent(event, options) ? null : event;
}
}
return event;
};
eventProcess.id = this.name;
addGlobalEventProcessor(eventProcess);
}
}
/** JSDoc */
export function _mergeOptions(
internalOptions: Partial<InboundFiltersOptions> = {},
clientOptions: Partial<InboundFiltersOptions> = {},
): Partial<InboundFiltersOptions> {
return {
allowUrls: [...(internalOptions.allowUrls || []), ...(clientOptions.allowUrls || [])],
denyUrls: [...(internalOptions.denyUrls || []), ...(clientOptions.denyUrls || [])],
ignoreErrors: [
...(internalOptions.ignoreErrors || []),
...(clientOptions.ignoreErrors || []),
...(internalOptions.disableErrorDefaults ? [] : DEFAULT_IGNORE_ERRORS),
],
ignoreTransactions: [
...(internalOptions.ignoreTransactions || []),
...(clientOptions.ignoreTransactions || []),
...(internalOptions.disableTransactionDefaults ? [] : DEFAULT_IGNORE_TRANSACTIONS),
],
ignoreInternal: internalOptions.ignoreInternal !== undefined ? internalOptions.ignoreInternal : true,
};
}
/** JSDoc */
export function _shouldDropEvent(event: Event, options: Partial<InboundFiltersOptions>): boolean {
if (options.ignoreInternal && _isSentryError(event)) {
__DEBUG_BUILD__ &&
logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
return true;
}
if (_isIgnoredError(event, options.ignoreErrors)) {
__DEBUG_BUILD__ &&
logger.warn(
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
);
return true;
}
if (_isIgnoredTransaction(event, options.ignoreTransactions)) {
__DEBUG_BUILD__ &&
logger.warn(
`Event dropped due to being matched by \`ignoreTransactions\` option.\nEvent: ${getEventDescription(event)}`,
);
return true;
}
if (_isDeniedUrl(event, options.denyUrls)) {
__DEBUG_BUILD__ &&
logger.warn(
`Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
event,
)}.\nUrl: ${_getEventFilterUrl(event)}`,
);
return true;
}
if (!_isAllowedUrl(event, options.allowUrls)) {
__DEBUG_BUILD__ &&
logger.warn(
`Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
event,
)}.\nUrl: ${_getEventFilterUrl(event)}`,
);
return true;
}
return false;
}
function _isIgnoredError(event: Event, ignoreErrors?: Array<string | RegExp>): boolean {
// If event.type, this is not an error
if (event.type || !ignoreErrors || !ignoreErrors.length) {
return false;
}
return _getPossibleEventMessages(event).some(message => stringMatchesSomePattern(message, ignoreErrors));
}
function _isIgnoredTransaction(event: Event, ignoreTransactions?: Array<string | RegExp>): boolean {
if (event.type !== 'transaction' || !ignoreTransactions || !ignoreTransactions.length) {
return false;
}
const name = event.transaction;
return name ? stringMatchesSomePattern(name, ignoreTransactions) : false;
}
function _isDeniedUrl(event: Event, denyUrls?: Array<string | RegExp>): boolean {
// TODO: Use Glob instead?
if (!denyUrls || !denyUrls.length) {
return false;
}
const url = _getEventFilterUrl(event);
return !url ? false : stringMatchesSomePattern(url, denyUrls);
}
function _isAllowedUrl(event: Event, allowUrls?: Array<string | RegExp>): boolean {
// TODO: Use Glob instead?
if (!allowUrls || !allowUrls.length) {
return true;
}
const url = _getEventFilterUrl(event);
return !url ? true : stringMatchesSomePattern(url, allowUrls);
}
function _getPossibleEventMessages(event: Event): string[] {
if (event.message) {
return [event.message];
}
if (event.exception) {
const { values } = event.exception;
try {
const { type = '', value = '' } = (values && values[values.length - 1]) || {};
return [`${value}`, `${type}: ${value}`];
} catch (oO) {
__DEBUG_BUILD__ && logger.error(`Cannot extract message for event ${getEventDescription(event)}`);
return [];
}
}
return [];
}
function _isSentryError(event: Event): boolean {
try {
// @ts-ignore can't be a sentry error if undefined
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return event.exception.values[0].type === 'SentryError';
} catch (e) {
// ignore
}
return false;
}
function _getLastValidUrl(frames: StackFrame[] = []): string | null {
for (let i = frames.length - 1; i >= 0; i--) {
const frame = frames[i];
if (frame && frame.filename !== '<anonymous>' && frame.filename !== '[native code]') {
return frame.filename || null;
}
}
return null;
}
function _getEventFilterUrl(event: Event): string | null {
try {
let frames;
try {
// @ts-ignore we only care about frames if the whole thing here is defined
frames = event.exception.values[0].stacktrace.frames;
} catch (e) {
// ignore
}
return frames ? _getLastValidUrl(frames) : null;
} catch (oO) {
__DEBUG_BUILD__ && logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
return null;
}
}