Skip to content

Commit 74a0829

Browse files
fix(core): Skip anonymous callbacks while searching frame URLs. (#3842)
* fix(core): Skip anonymous callbacks while searching frame URLs. * Apply suggestions from code review Co-authored-by: Kamil Ogórek <[email protected]> Co-authored-by: Kamil Ogórek <[email protected]>
1 parent fd6e641 commit 74a0829

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

packages/core/src/integrations/inboundfilters.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
2-
import { Event, Integration } from '@sentry/types';
2+
import { Event, Integration, StackFrame } from '@sentry/types';
33
import { getEventDescription, isMatchingPattern, logger } from '@sentry/utils';
44

55
// "Script error." is hard coded into browsers for errors that it can't read.
@@ -189,17 +189,30 @@ export class InboundFilters implements Integration {
189189
return [];
190190
}
191191

192+
/** JSDoc */
193+
private _getLastValidUrl(frames: StackFrame[] = []): string | null {
194+
for (let i = frames.length - 1; i >= 0; i--) {
195+
const frame = frames[i];
196+
197+
if (frame?.filename !== '<anonymous>') {
198+
return frame.filename || null;
199+
}
200+
}
201+
202+
return null;
203+
}
204+
192205
/** JSDoc */
193206
private _getEventFilterUrl(event: Event): string | null {
194207
try {
195208
if (event.stacktrace) {
196209
const frames = event.stacktrace.frames;
197-
return (frames && frames[frames.length - 1].filename) || null;
210+
return this._getLastValidUrl(frames);
198211
}
199212
if (event.exception) {
200213
const frames =
201214
event.exception.values && event.exception.values[0].stacktrace && event.exception.values[0].stacktrace.frames;
202-
return (frames && frames[frames.length - 1].filename) || null;
215+
return this._getLastValidUrl(frames);
203216
}
204217
return null;
205218
} catch (oO) {

packages/core/test/lib/integrations/inboundfilters.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,5 +456,36 @@ describe('InboundFilters', () => {
456456
),
457457
).toBe(true);
458458
});
459+
460+
it('should search for script names when there is an anonymous callback at the last frame', () => {
461+
const messageEvent = {
462+
message: 'any',
463+
stacktrace: {
464+
frames: [
465+
{ filename: 'https://our-side.com/js/bundle.js' },
466+
{ filename: 'https://awesome-analytics.io/some/file.js' },
467+
{ filename: '<anonymous>' },
468+
],
469+
},
470+
};
471+
472+
expect(
473+
inboundFilters._isAllowedUrl(
474+
messageEvent,
475+
inboundFilters._mergeOptions({
476+
allowUrls: ['https://awesome-analytics.io/some/file.js'],
477+
}),
478+
),
479+
).toBe(true);
480+
481+
expect(
482+
inboundFilters._isDeniedUrl(
483+
messageEvent,
484+
inboundFilters._mergeOptions({
485+
denyUrls: ['https://awesome-analytics.io/some/file.js'],
486+
}),
487+
),
488+
).toBe(true);
489+
});
459490
});
460491
});

0 commit comments

Comments
 (0)