UNPKG

7.43 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2var tslib_1 = require("tslib");
3var hub_1 = require("@sentry/hub");
4var utils_1 = require("@sentry/utils");
5// "Script error." is hard coded into browsers for errors that it can't read.
6// this is the result of a script being pulled in from an external domain and CORS.
7var DEFAULT_IGNORE_ERRORS = [/^Script error\.?$/, /^Javascript error: Script error\.? on line 0$/];
8/** Inbound filters configurable by the user */
9var InboundFilters = /** @class */ (function () {
10 function InboundFilters(_options) {
11 if (_options === void 0) { _options = {}; }
12 this._options = _options;
13 /**
14 * @inheritDoc
15 */
16 this.name = InboundFilters.id;
17 }
18 /**
19 * @inheritDoc
20 */
21 InboundFilters.prototype.setupOnce = function () {
22 hub_1.addGlobalEventProcessor(function (event) {
23 var hub = hub_1.getCurrentHub();
24 if (!hub) {
25 return event;
26 }
27 var self = hub.getIntegration(InboundFilters);
28 if (self) {
29 var client = hub.getClient();
30 var clientOptions = client ? client.getOptions() : {};
31 // This checks prevents most of the occurrences of the bug linked below:
32 // https://github.com/getsentry/sentry-javascript/issues/2622
33 // The bug is caused by multiple SDK instances, where one is minified and one is using non-mangled code.
34 // Unfortunatelly we cannot fix it reliably (thus reserved property in rollup's terser config),
35 // as we cannot force people using multiple instances in their apps to sync SDK versions.
36 var options = typeof self._mergeOptions === 'function' ? self._mergeOptions(clientOptions) : {};
37 if (typeof self._shouldDropEvent !== 'function') {
38 return event;
39 }
40 return self._shouldDropEvent(event, options) ? null : event;
41 }
42 return event;
43 });
44 };
45 /** JSDoc */
46 InboundFilters.prototype._shouldDropEvent = function (event, options) {
47 if (this._isSentryError(event, options)) {
48 utils_1.logger.warn("Event dropped due to being internal Sentry Error.\nEvent: " + utils_1.getEventDescription(event));
49 return true;
50 }
51 if (this._isIgnoredError(event, options)) {
52 utils_1.logger.warn("Event dropped due to being matched by `ignoreErrors` option.\nEvent: " + utils_1.getEventDescription(event));
53 return true;
54 }
55 if (this._isDeniedUrl(event, options)) {
56 utils_1.logger.warn("Event dropped due to being matched by `denyUrls` option.\nEvent: " + utils_1.getEventDescription(event) + ".\nUrl: " + this._getEventFilterUrl(event));
57 return true;
58 }
59 if (!this._isAllowedUrl(event, options)) {
60 utils_1.logger.warn("Event dropped due to not being matched by `allowUrls` option.\nEvent: " + utils_1.getEventDescription(event) + ".\nUrl: " + this._getEventFilterUrl(event));
61 return true;
62 }
63 return false;
64 };
65 /** JSDoc */
66 InboundFilters.prototype._isSentryError = function (event, options) {
67 if (!options.ignoreInternal) {
68 return false;
69 }
70 try {
71 return ((event &&
72 event.exception &&
73 event.exception.values &&
74 event.exception.values[0] &&
75 event.exception.values[0].type === 'SentryError') ||
76 false);
77 }
78 catch (_oO) {
79 return false;
80 }
81 };
82 /** JSDoc */
83 InboundFilters.prototype._isIgnoredError = function (event, options) {
84 if (!options.ignoreErrors || !options.ignoreErrors.length) {
85 return false;
86 }
87 return this._getPossibleEventMessages(event).some(function (message) {
88 // Not sure why TypeScript complains here...
89 return options.ignoreErrors.some(function (pattern) { return utils_1.isMatchingPattern(message, pattern); });
90 });
91 };
92 /** JSDoc */
93 InboundFilters.prototype._isDeniedUrl = function (event, options) {
94 // TODO: Use Glob instead?
95 if (!options.denyUrls || !options.denyUrls.length) {
96 return false;
97 }
98 var url = this._getEventFilterUrl(event);
99 return !url ? false : options.denyUrls.some(function (pattern) { return utils_1.isMatchingPattern(url, pattern); });
100 };
101 /** JSDoc */
102 InboundFilters.prototype._isAllowedUrl = function (event, options) {
103 // TODO: Use Glob instead?
104 if (!options.allowUrls || !options.allowUrls.length) {
105 return true;
106 }
107 var url = this._getEventFilterUrl(event);
108 return !url ? true : options.allowUrls.some(function (pattern) { return utils_1.isMatchingPattern(url, pattern); });
109 };
110 /** JSDoc */
111 InboundFilters.prototype._mergeOptions = function (clientOptions) {
112 if (clientOptions === void 0) { clientOptions = {}; }
113 return {
114 allowUrls: tslib_1.__spread((this._options.whitelistUrls || []), (this._options.allowUrls || []), (clientOptions.whitelistUrls || []), (clientOptions.allowUrls || [])),
115 denyUrls: tslib_1.__spread((this._options.blacklistUrls || []), (this._options.denyUrls || []), (clientOptions.blacklistUrls || []), (clientOptions.denyUrls || [])),
116 ignoreErrors: tslib_1.__spread((this._options.ignoreErrors || []), (clientOptions.ignoreErrors || []), DEFAULT_IGNORE_ERRORS),
117 ignoreInternal: typeof this._options.ignoreInternal !== 'undefined' ? this._options.ignoreInternal : true,
118 };
119 };
120 /** JSDoc */
121 InboundFilters.prototype._getPossibleEventMessages = function (event) {
122 if (event.message) {
123 return [event.message];
124 }
125 if (event.exception) {
126 try {
127 var _a = (event.exception.values && event.exception.values[0]) || {}, _b = _a.type, type = _b === void 0 ? '' : _b, _c = _a.value, value = _c === void 0 ? '' : _c;
128 return ["" + value, type + ": " + value];
129 }
130 catch (oO) {
131 utils_1.logger.error("Cannot extract message for event " + utils_1.getEventDescription(event));
132 return [];
133 }
134 }
135 return [];
136 };
137 /** JSDoc */
138 InboundFilters.prototype._getEventFilterUrl = function (event) {
139 try {
140 if (event.stacktrace) {
141 var frames_1 = event.stacktrace.frames;
142 return (frames_1 && frames_1[frames_1.length - 1].filename) || null;
143 }
144 if (event.exception) {
145 var frames_2 = event.exception.values && event.exception.values[0].stacktrace && event.exception.values[0].stacktrace.frames;
146 return (frames_2 && frames_2[frames_2.length - 1].filename) || null;
147 }
148 return null;
149 }
150 catch (oO) {
151 utils_1.logger.error("Cannot extract url for event " + utils_1.getEventDescription(event));
152 return null;
153 }
154 };
155 /**
156 * @inheritDoc
157 */
158 InboundFilters.id = 'InboundFilters';
159 return InboundFilters;
160}());
161exports.InboundFilters = InboundFilters;
162//# sourceMappingURL=inboundfilters.js.map
\No newline at end of file