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

ref(parser): limit max stack lines we parse #7410

Merged
merged 1 commit into from
Mar 16, 2023
Merged
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
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