Skip to content

Commit

Permalink
fix: very defensive body redaction (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Feb 2, 2024
1 parent ca8f485 commit 199965b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/__tests__/extensions/replay/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('config', () => {
'content-type': 'application/json',
'content-length': '1000001',
},
requestBody: 'Request body too large to record',
requestBody: '[SessionReplay] Request body too large to record',
})
})

Expand All @@ -196,26 +196,25 @@ describe('config', () => {
'content-type': 'application/json',
'content-length': '1000001',
},
responseBody: 'Response body too large to record',
responseBody: '[SessionReplay] Response body too large to record',
})
})

it('cannot redact when there is no content length header', () => {
const networkOptions = buildNetworkRequestOptions(defaultConfig(), {})
const largeString = 'a'.repeat(1000001)
const cleaned = networkOptions.maskRequestFn!({
name: 'something',
requestHeaders: {
'content-type': 'application/json',
},
requestBody: largeString,
requestBody: 'some body that has no content length',
})
expect(cleaned).toEqual({
name: 'something',
requestHeaders: {
'content-type': 'application/json',
},
requestBody: largeString,
requestBody: '[SessionReplay] no content-length header for Request, cannot determine payload size',
})
})
})
Expand Down
11 changes: 5 additions & 6 deletions src/extensions/replay/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CapturedNetworkRequest, NetworkRecordOptions, PostHogConfig, Body } from '../../types'
import { _isFunction } from '../../utils/type-utils'
import { _isFunction, _isUndefined } from '../../utils/type-utils'
import { convertToURL } from '../../utils/request-utils'
import { logger } from '../../utils/logger'

Expand Down Expand Up @@ -86,12 +86,11 @@ function redactPayload(
description: string
): Body {
const requestContentLength = headers?.['content-length']
// in the interests of bundle size and the complexity of estimating payload size
// we only check the content-length header if it's present
// this might mean we can't always limit the payload, but that's better than
// having lots of code shipped to every browser that will rarely run
if (_isUndefined(requestContentLength)) {
return `[SessionReplay] no content-length header for ${description}, cannot determine payload size`
}
if (requestContentLength && parseInt(requestContentLength) > limit) {
return `${description} body too large to record`
return `[SessionReplay] ${description} body too large to record`
}
return payload
}
Expand Down

0 comments on commit 199965b

Please sign in to comment.