| 17 | * |
| 18 | */ |
| 19 | export function createStackParser(...parsers: StackLineParser[]): StackParser { |
| 20 | const sortedParsers = parsers.sort((a, b) => a[0] - b[0]).map(p => p[1]); |
| 21 | |
| 22 | return (stack: string, skipFirstLines: number = 0, framesToPop: number = 0): StackFrame[] => { |
| 23 | const frames: StackFrame[] = []; |
| 24 | const lines = stack.split('\n'); |
| 25 | |
| 26 | for (let i = skipFirstLines; i < lines.length; i++) { |
| 27 | let line = lines[i] as string; |
| 28 | // Truncate lines over 1kb because many of the regular expressions use |
| 29 | // backtracking which results in run time that increases exponentially |
| 30 | // with input size. Huge strings can result in hangs/Denial of Service: |
| 31 | // https://github.com/getsentry/sentry-javascript/issues/2286 |
| 32 | if (line.length > 1024) { |
| 33 | line = line.slice(0, 1024); |
| 34 | } |
| 35 | |
| 36 | // https://github.com/getsentry/sentry-javascript/issues/5459 |
| 37 | // Remove webpack (error: *) wrappers |
| 38 | const cleanedLine = WEBPACK_ERROR_REGEXP.test(line) ? line.replace(WEBPACK_ERROR_REGEXP, '$1') : line; |
| 39 | |
| 40 | // https://github.com/getsentry/sentry-javascript/issues/7813 |
| 41 | // Skip Error: lines |
| 42 | // Using includes() instead of a regex to avoid O(n²) backtracking on long lines |
| 43 | // https://github.com/getsentry/sentry-javascript/issues/20052 |
| 44 | if (cleanedLine.includes('Error: ')) { |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | for (const parser of sortedParsers) { |
| 49 | const frame = parser(cleanedLine); |
| 50 | |
| 51 | if (frame) { |
| 52 | frames.push(frame); |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (frames.length >= STACKTRACE_FRAME_LIMIT + framesToPop) { |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return stripSentryFramesAndReverse(frames.slice(framesToPop)); |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Gets a stack parser implementation from Options.stackParser |