Skip to content

Commit e94a36e

Browse files
authored
fix: Add safeguards for addToTrace (#1490)
1 parent 66f5eb8 commit e94a36e

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

docs/warning-codes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,6 @@
120120
### 59
121121
`Session replay harvested before a session trace payload could be sent. This could be problematic for replays that rely on a trace`
122122
### 60
123-
`Session trace aborted`
123+
`Session trace aborted`
124+
### 61
125+
`Timestamps must be non-negative and end time cannot be before start time.`

src/loaders/api/addToTrace.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
import { originTime } from '../../common/constants/runtime'
66
import { handle } from '../../common/event-emitter/handle'
7+
import { warn } from '../../common/util/console'
78
import { FEATURE_NAMES } from '../features/features'
89
import { ADD_TO_TRACE } from './constants'
910
import { setupAPI } from './sharedHandlers'
@@ -20,6 +21,11 @@ export function setupAddToTraceAPI (agent) {
2021
t: 'api'
2122
}
2223

24+
if (report.s < 0 || report.e < 0 || report.e < report.s) {
25+
warn(61, { start: report.s, end: report.e })
26+
return
27+
}
28+
2329
handle('bstApi', [report], undefined, FEATURE_NAMES.sessionTrace, agent.ee)
2430
}, agent)
2531
}

tests/components/api.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,40 @@ describe('API tests', () => {
154154
const payload = handleModule.handle.mock.calls.find(callArr => callArr[0] === 'bstApi')[1][0]
155155
expect(payload.e - payload.s).toEqual(1000) // end - start was 1000ms apart in API call
156156
})
157+
158+
test('should return error code for negative timestamps', () => {
159+
agent.addToTrace({
160+
name: 'Event Name',
161+
start: Date.now(),
162+
end: -1000,
163+
origin: 'Origin of event'
164+
})
165+
166+
expect(console.debug).toHaveBeenCalledTimes(1)
167+
expect(console.debug).toHaveBeenLastCalledWith(expect.stringContaining('New Relic Warning: https://github.com/newrelic/newrelic-browser-agent/blob/main/docs/warning-codes.md#61'), expect.any(Object))
168+
169+
agent.addToTrace({
170+
name: 'Event Name',
171+
start: -1234,
172+
end: Date.now() + 1000,
173+
origin: 'Origin of event'
174+
})
175+
176+
expect(console.debug).toHaveBeenCalledTimes(2)
177+
expect(console.debug).toHaveBeenLastCalledWith(expect.stringContaining('New Relic Warning: https://github.com/newrelic/newrelic-browser-agent/blob/main/docs/warning-codes.md#61'), expect.any(Object))
178+
})
179+
180+
test('should return error code for end time before start time', () => {
181+
agent.addToTrace({
182+
name: 'Event Name',
183+
start: Date.now() + 2000,
184+
end: Date.now() + 1000,
185+
origin: 'Origin of event'
186+
})
187+
188+
expect(console.debug).toHaveBeenCalled()
189+
expect(console.debug).toHaveBeenLastCalledWith(expect.stringContaining('New Relic Warning: https://github.com/newrelic/newrelic-browser-agent/blob/main/docs/warning-codes.md#61'), expect.any(Object))
190+
})
157191
})
158192

159193
describe('addRelease', () => {

0 commit comments

Comments
 (0)