Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow payload scrubbing override #1085

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/__tests__/extensions/replay/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defaultConfig } from '../../../posthog-core'
import { buildNetworkRequestOptions } from '../../../extensions/replay/config'
import { CapturedNetworkRequest } from '../../../types'

describe('config', () => {
describe('network request options', () => {
Expand Down Expand Up @@ -250,7 +251,7 @@ describe('config', () => {
})
})

it('should redact password even when a mask request fn is set', () => {
it('mask request fn replaces scrubPayload functionality', () => {
const posthogConfig = defaultConfig()
posthogConfig.session_recording.maskCapturedNetworkRequestFn = (data) => {
return {
Expand All @@ -259,6 +260,7 @@ describe('config', () => {
...(data.requestHeaders ? data.requestHeaders : {}),
'content-type': 'edited',
},
requestBody: 'the provided function ran',
}
}
const networkOptions = buildNetworkRequestOptions(posthogConfig, {})
Expand All @@ -269,16 +271,17 @@ describe('config', () => {
Authorization: 'Bearer 123',
'content-type': 'application/json',
},
requestBody: 'some body with password',
responseBody: 'some body with password',
})
requestBody: 'the original value',
responseBody: 'the original value',
} as Partial<CapturedNetworkRequest> as CapturedNetworkRequest)

expect(cleaned).toEqual({
name: 'something',
requestHeaders: {
'content-type': 'edited',
},
requestBody: '[SessionRecording] Request body redacted as might contain: password',
responseBody: '[SessionRecording] Response body redacted as might contain: password',
requestBody: 'the provided function ran',
responseBody: 'the original value',
})
})

Expand Down
8 changes: 2 additions & 6 deletions src/extensions/replay/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export const buildNetworkRequestOptions = (
const payloadLimiter = limitPayloadSize(config)

const enforcedCleaningFn: NetworkRecordOptions['maskRequestFn'] = (d: CapturedNetworkRequest) =>
scrubPayloads(payloadLimiter(ignorePostHogPaths(removeAuthorizationHeader(d))))
payloadLimiter(ignorePostHogPaths(removeAuthorizationHeader(d)))

const hasDeprecatedMaskFunction = _isFunction(instanceConfig.session_recording.maskNetworkRequestFn)

Expand All @@ -218,11 +218,7 @@ export const buildNetworkRequestOptions = (
? instanceConfig.session_recording.maskCapturedNetworkRequestFn?.(cleanedRequest) ?? undefined
: undefined
}
: undefined

if (!config.maskRequestFn) {
config.maskRequestFn = enforcedCleaningFn
}
: (data) => scrubPayloads(enforcedCleaningFn(data))

return {
...defaultNetworkOptions,
Expand Down
Loading