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

feat: Mask page URLs in session recordings #811

Merged
merged 1 commit into from
Sep 27, 2023
Merged
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
34 changes: 31 additions & 3 deletions src/extensions/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
truncateLargeConsoleLogs,
} from './sessionrecording-utils'
import { PostHog } from '../posthog-core'
import { DecideResponse, Properties } from '../types'
import type { eventWithTime, listenerHandler } from '@rrweb/types'
import { DecideResponse, NetworkRequest, Properties } from '../types'
import { EventType, type eventWithTime, type listenerHandler } from '@rrweb/types'
import Config from '../config'
import { logger, loadScript, _timestamp } from '../utils'

Expand Down Expand Up @@ -376,7 +376,11 @@ export class SessionRecording {
// If anything could go wrong here it has the potential to block the main loop so we catch all errors.
try {
if (eventName === '$pageview') {
this.rrwebRecord?.addCustomEvent('$pageview', { href: window.location.href })
const href = this._maskUrl(window.location.href)
if (!href) {
return
}
this.rrwebRecord?.addCustomEvent('$pageview', { href })
}
} catch (e) {
logger.error('Could not add $pageview to rrweb session', e)
Expand All @@ -393,6 +397,14 @@ export class SessionRecording {
return
}

if (rawEvent.type === EventType.Meta) {
const href = this._maskUrl(rawEvent.data.href)
if (!href) {
return
}
rawEvent.data.href = href
}

const throttledEvent = this.mutationRateLimiter
? this.mutationRateLimiter.throttleMutations(rawEvent)
: rawEvent
Expand Down Expand Up @@ -424,6 +436,22 @@ export class SessionRecording {
}
}

private _maskUrl(url: string): string | undefined {
const userSessionRecordingOptions = this.instance.get_config('session_recording')

if (userSessionRecordingOptions.maskNetworkRequestFn) {
let networkRequest: NetworkRequest | null | undefined = {
url,
}

networkRequest = userSessionRecordingOptions.maskNetworkRequestFn(networkRequest)

return networkRequest?.url
}

return url
}

private _flushBuffer() {
if (this.flushBufferTimer) {
clearTimeout(this.flushBufferTimer)
Expand Down
Loading