diff --git a/packages/node/src/integrations/contextlines.ts b/packages/node/src/integrations/contextlines.ts index dd654fb3a153..2a3fa3e12b53 100644 --- a/packages/node/src/integrations/contextlines.ts +++ b/packages/node/src/integrations/contextlines.ts @@ -10,6 +10,11 @@ const LRU_FILE_CONTENTS_CACHE = new LRUMap>(10); const LRU_FILE_CONTENTS_FS_READ_FAILED = new LRUMap(20); const DEFAULT_LINES_OF_CONTEXT = 7; const INTEGRATION_NAME = 'ContextLines'; +// Determines the upper bound of lineno/colno that we will attempt to read. Large colno values are likely to be +// minified code while large lineno values are likely to be bundled code. +// Exported for testing purposes. +export const MAX_CONTEXTLINES_COLNO: number = 1000; +export const MAX_CONTEXTLINES_LINENO: number = 10000; interface ContextLinesOptions { /** @@ -58,6 +63,15 @@ function shouldSkipContextLinesForFile(path: string): boolean { if (path.startsWith('data:')) return true; return false; } + +/** + * Determines if we should skip contextlines based off the max lineno and colno values. + */ +function shouldSkipContextLinesForFrame(frame: StackFrame): boolean { + if (frame.lineno !== undefined && frame.lineno > MAX_CONTEXTLINES_LINENO) return true; + if (frame.colno !== undefined && frame.colno > MAX_CONTEXTLINES_COLNO) return true; + return false; +} /** * Checks if we have all the contents that we need in the cache. */ @@ -216,7 +230,8 @@ async function addSourceContext(event: Event, contextLines: number): Promise { jest.clearAllMocks(); }); + describe('limits', () => { + test(`colno above ${MAX_CONTEXTLINES_COLNO}`, async () => { + expect.assertions(1); + const frames: StackFrame[] = [ + { + colno: MAX_CONTEXTLINES_COLNO + 1, + filename: 'file:///var/task/index.js', + lineno: 1, + function: 'fxn1', + }, + ]; + + const readStreamSpy = jest.spyOn(fs, 'createReadStream'); + await addContext(frames); + expect(readStreamSpy).not.toHaveBeenCalled(); + }); + + test(`lineno above ${MAX_CONTEXTLINES_LINENO}`, async () => { + expect.assertions(1); + const frames: StackFrame[] = [ + { + colno: 1, + filename: 'file:///var/task/index.js', + lineno: MAX_CONTEXTLINES_LINENO + 1, + function: 'fxn1', + }, + ]; + + const readStreamSpy = jest.spyOn(fs, 'createReadStream'); + await addContext(frames); + expect(readStreamSpy).not.toHaveBeenCalled(); + }); + }); + describe('lru file cache', () => { test('parseStack when file does not exist', async () => { expect.assertions(4);