Skip to content

Commit fb1abb4

Browse files
committed
ref(parser): limit max stack lines we parse
1 parent ad32f94 commit fb1abb4

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

packages/utils/src/stacktrace.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { StackFrame, StackLineParser, StackLineParserFn, StackParser } from '@sentry/types';
22

3-
const STACKTRACE_LIMIT = 50;
3+
const STACKTRACE_FRAME_LIMIT = 50;
44
// Used to sanitize webpack (error: *) wrapped stack errors
55
const WEBPACK_ERROR_REGEXP = /\(error: (.*)\)/;
66

@@ -16,7 +16,10 @@ export function createStackParser(...parsers: StackLineParser[]): StackParser {
1616

1717
return (stack: string, skipFirst: number = 0): StackFrame[] => {
1818
const frames: StackFrame[] = [];
19-
for (const line of stack.split('\n').slice(skipFirst)) {
19+
const lines = stack.split('\n');
20+
21+
for (let i = skipFirst; i < lines.length; i++) {
22+
const line = lines[i];
2023
// Ignore lines over 1kb as they are unlikely to be stack frames.
2124
// Many of the regular expressions use backtracking which results in run time that increases exponentially with
2225
// input size. Huge strings can result in hangs/Denial of Service:
@@ -37,6 +40,10 @@ export function createStackParser(...parsers: StackLineParser[]): StackParser {
3740
break;
3841
}
3942
}
43+
44+
if (frames.length >= STACKTRACE_FRAME_LIMIT) {
45+
break;
46+
}
4047
}
4148

4249
return stripSentryFramesAndReverse(frames);
@@ -67,7 +74,7 @@ export function stripSentryFramesAndReverse(stack: ReadonlyArray<StackFrame>): S
6774
return [];
6875
}
6976

70-
const localStack = stack.slice(0, STACKTRACE_LIMIT);
77+
const localStack = stack.slice(0, STACKTRACE_FRAME_LIMIT);
7178

7279
const lastFrameFunction = localStack[localStack.length - 1].function;
7380
// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)

0 commit comments

Comments
 (0)