Skip to content

Commit

Permalink
ref(parser): limit max stack lines we parse
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Mar 16, 2023
1 parent ad32f94 commit fb1abb4
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/utils/src/stacktrace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { StackFrame, StackLineParser, StackLineParserFn, StackParser } from '@sentry/types';

const STACKTRACE_LIMIT = 50;
const STACKTRACE_FRAME_LIMIT = 50;
// Used to sanitize webpack (error: *) wrapped stack errors
const WEBPACK_ERROR_REGEXP = /\(error: (.*)\)/;

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

return (stack: string, skipFirst: number = 0): StackFrame[] => {
const frames: StackFrame[] = [];
for (const line of stack.split('\n').slice(skipFirst)) {
const lines = stack.split('\n');

for (let i = skipFirst; i < lines.length; i++) {
const line = lines[i];
// 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:
Expand All @@ -37,6 +40,10 @@ export function createStackParser(...parsers: StackLineParser[]): StackParser {
break;
}
}

if (frames.length >= STACKTRACE_FRAME_LIMIT) {
break;
}
}

return stripSentryFramesAndReverse(frames);
Expand Down Expand Up @@ -67,7 +74,7 @@ export function stripSentryFramesAndReverse(stack: ReadonlyArray<StackFrame>): S
return [];
}

const localStack = stack.slice(0, STACKTRACE_LIMIT);
const localStack = stack.slice(0, STACKTRACE_FRAME_LIMIT);

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

0 comments on commit fb1abb4

Please sign in to comment.