Skip to content
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

fix(utils): Don't run stack trace regexes over huge strings #6627

Merged
merged 1 commit into from Jan 4, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/browser/test/unit/tracekit/chromium.test.ts
Expand Up @@ -547,4 +547,30 @@ describe('Tracekit - Chrome Tests', () => {
},
});
});

it('should drop frames that are over 1kb', () => {
const LONG_STR = 'A'.repeat(1040);

const LONG_FRAME = {
message: 'bad',
name: 'Error',
stack: `Error: bad
at aha (http://localhost:5000/:39:5)
at Foo.testMethod (http://localhost:5000/${LONG_STR}:44:7)
at http://localhost:5000/:50:19`,
};

const ex = exceptionFromError(parser, LONG_FRAME);

expect(ex).toEqual({
value: 'bad',
type: 'Error',
stacktrace: {
frames: [
{ filename: 'http://localhost:5000/', function: '?', lineno: 50, colno: 19, in_app: true },
{ filename: 'http://localhost:5000/', function: 'aha', lineno: 39, colno: 5, in_app: true },
],
},
});
});
});
8 changes: 8 additions & 0 deletions packages/utils/src/stacktrace.ts
Expand Up @@ -16,6 +16,14 @@ export function createStackParser(...parsers: StackLineParser[]): StackParser {
const frames: StackFrame[] = [];

for (const line of stack.split('\n').slice(skipFirst)) {
// Ignore lines over 1kb as they are unlikely to be stack frames.
// Many of the regular expressions use backtracking which results in run time that increases exponentially with
// input size. Huge strings can result in hangs/Denial of Service:
// https://github.com/getsentry/sentry-javascript/issues/2286
if (line.length > 1024) {
continue;
}

// https://github.com/getsentry/sentry-javascript/issues/5459
// Remove webpack (error: *) wrappers
const cleanedLine = line.replace(/\(error: (.*)\)/, '$1');
Expand Down