Skip to content

fix(core): Skip anonymous callbacks while searching frame URLs. #3842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
import { Event, Integration } from '@sentry/types';
import { Event, Integration, StackFrame } from '@sentry/types';
import { getEventDescription, isMatchingPattern, logger } from '@sentry/utils';

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

/** JSDoc */
private _getLastValidUrl(frames: StackFrame[] = []): string | null {
for (let i = frames.length - 1; i >= 0; i--) {
const frame = frames[i];

if (frame?.filename !== '<anonymous>') {
return frame.filename || null;
}
}

return null;
}

/** JSDoc */
private _getEventFilterUrl(event: Event): string | null {
try {
if (event.stacktrace) {
const frames = event.stacktrace.frames;
return (frames && frames[frames.length - 1].filename) || null;
return this._getLastValidUrl(frames);
}
if (event.exception) {
const frames =
event.exception.values && event.exception.values[0].stacktrace && event.exception.values[0].stacktrace.frames;
return (frames && frames[frames.length - 1].filename) || null;
return this._getLastValidUrl(frames);
}
return null;
} catch (oO) {
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/lib/integrations/inboundfilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,5 +456,36 @@ describe('InboundFilters', () => {
),
).toBe(true);
});

it('should search for script names when there is an anonymous callback at the last frame', () => {
const messageEvent = {
message: 'any',
stacktrace: {
frames: [
{ filename: 'https://our-side.com/js/bundle.js' },
{ filename: 'https://awesome-analytics.io/some/file.js' },
{ filename: '<anonymous>' },
],
},
};

expect(
inboundFilters._isAllowedUrl(
messageEvent,
inboundFilters._mergeOptions({
allowUrls: ['https://awesome-analytics.io/some/file.js'],
}),
),
).toBe(true);

expect(
inboundFilters._isDeniedUrl(
messageEvent,
inboundFilters._mergeOptions({
denyUrls: ['https://awesome-analytics.io/some/file.js'],
}),
),
).toBe(true);
});
});
});