-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node): Guard against invalid
maxSpanWaitDuration
values (#14632)
Today, if you pass e.g. `Infinity` or another large number to `maxSpanWaitDuration`, the SDK will error out because we try to do `new Array(maxSpanWaitDuration)`, which is impossible. Ideally, we can properly allow an infinite wait time, but for now this adds checks to ensure only valid values are passed in there, and we do not explode. See #14154 (comment)
- Loading branch information
Showing
2 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { logger } from '@sentry/core'; | ||
import { _clampSpanProcessorTimeout } from '../../src/sdk/initOtel'; | ||
|
||
describe('_clampSpanProcessorTimeout', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('works with undefined', () => { | ||
const loggerWarnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {}); | ||
const timeout = _clampSpanProcessorTimeout(undefined); | ||
expect(timeout).toBe(undefined); | ||
expect(loggerWarnSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('works with positive number', () => { | ||
const loggerWarnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {}); | ||
const timeout = _clampSpanProcessorTimeout(10); | ||
expect(timeout).toBe(10); | ||
expect(loggerWarnSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('works with 0', () => { | ||
const loggerWarnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {}); | ||
const timeout = _clampSpanProcessorTimeout(0); | ||
expect(timeout).toBe(undefined); | ||
expect(loggerWarnSpy).toHaveBeenCalledTimes(1); | ||
expect(loggerWarnSpy).toHaveBeenCalledWith( | ||
'`maxSpanWaitDuration` must be a positive number, using default value instead.', | ||
); | ||
}); | ||
|
||
it('works with negative number', () => { | ||
const loggerWarnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {}); | ||
const timeout = _clampSpanProcessorTimeout(-10); | ||
expect(timeout).toBe(undefined); | ||
expect(loggerWarnSpy).toHaveBeenCalledTimes(1); | ||
expect(loggerWarnSpy).toHaveBeenCalledWith( | ||
'`maxSpanWaitDuration` must be a positive number, using default value instead.', | ||
); | ||
}); | ||
|
||
it('works with -Infinity', () => { | ||
const loggerWarnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {}); | ||
const timeout = _clampSpanProcessorTimeout(-Infinity); | ||
expect(timeout).toBe(undefined); | ||
expect(loggerWarnSpy).toHaveBeenCalledTimes(1); | ||
expect(loggerWarnSpy).toHaveBeenCalledWith( | ||
'`maxSpanWaitDuration` must be a positive number, using default value instead.', | ||
); | ||
}); | ||
|
||
it('works with Infinity', () => { | ||
const loggerWarnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {}); | ||
const timeout = _clampSpanProcessorTimeout(Infinity); | ||
expect(timeout).toBe(1_000_000); | ||
expect(loggerWarnSpy).toHaveBeenCalledTimes(1); | ||
expect(loggerWarnSpy).toHaveBeenCalledWith('`maxSpanWaitDuration` is too high, using the maximum value of 1000000'); | ||
}); | ||
|
||
it('works with large number', () => { | ||
const loggerWarnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {}); | ||
const timeout = _clampSpanProcessorTimeout(1_000_000_000); | ||
expect(timeout).toBe(1_000_000); | ||
expect(loggerWarnSpy).toHaveBeenCalledTimes(1); | ||
expect(loggerWarnSpy).toHaveBeenCalledWith('`maxSpanWaitDuration` is too high, using the maximum value of 1000000'); | ||
}); | ||
|
||
it('works with NaN', () => { | ||
const loggerWarnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {}); | ||
const timeout = _clampSpanProcessorTimeout(NaN); | ||
expect(timeout).toBe(undefined); | ||
expect(loggerWarnSpy).toHaveBeenCalledTimes(1); | ||
expect(loggerWarnSpy).toHaveBeenCalledWith( | ||
'`maxSpanWaitDuration` must be a positive number, using default value instead.', | ||
); | ||
}); | ||
}); |