Skip to content

Commit 57c3c64

Browse files
committed
ref(parser): limit max stack lines we parse
1 parent bd45dfc commit 57c3c64

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

55
/**
66
* Creates a stack parser with the supplied line parsers
@@ -14,7 +14,10 @@ export function createStackParser(...parsers: StackLineParser[]): StackParser {
1414

1515
return (stack: string, skipFirst: number = 0): StackFrame[] => {
1616
const frames: StackFrame[] = [];
17-
for (const line of stack.split('\n').slice(skipFirst)) {
17+
const lines = stack.split('\n');
18+
19+
for (let i = skipFirst; i < lines.length; i++) {
20+
const line = lines[i];
1821
// Ignore lines over 1kb as they are unlikely to be stack frames.
1922
// Many of the regular expressions use backtracking which results in run time that increases exponentially with
2023
// input size. Huge strings can result in hangs/Denial of Service:
@@ -35,6 +38,10 @@ export function createStackParser(...parsers: StackLineParser[]): StackParser {
3538
break;
3639
}
3740
}
41+
42+
if (frames.length >= STACKTRACE_FRAME_LIMIT) {
43+
break;
44+
}
3845
}
3946

4047
return stripSentryFramesAndReverse(frames);
@@ -79,7 +86,7 @@ export function stripSentryFramesAndReverse(stack: StackFrame[]): StackFrame[] {
7986

8087
// The frame where the crash happened, should be the last entry in the array
8188
return localStack
82-
.slice(0, STACKTRACE_LIMIT)
89+
.slice(0, STACKTRACE_FRAME_LIMIT)
8390
.map(frame => ({
8491
...frame,
8592
filename: frame.filename || localStack[0].filename,

0 commit comments

Comments
 (0)