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

✨[REPLAY-336] Privacy by Default: MVP scaffold #914

Closed
wants to merge 5 commits into from
Closed
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
7 changes: 7 additions & 0 deletions packages/core/src/domain/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const DEFAULT_CONFIGURATION = {
silentMultipleInit: false,
trackInteractions: false,
trackViewsManually: false,
censorshipLevel: 'PUBLIC',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • It could be interesting to pass recording options to the startSessionReplayRecording function instead of init, so they are scoped and can be changed every time the recording is restarted
  • I'm not found of censorship: I think it is a bit negatively connoted. What about something like privacyLevel or maskMode?
  • We should have an object constants (ex: similar to StatusType) to avoid hardcoding values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startSessionReplayRecording is interesting. 🤔


/**
* arbitrary value, byte precision not needed
Expand Down Expand Up @@ -54,6 +55,7 @@ export interface UserConfiguration {
trackViewsManually?: boolean
proxyHost?: string
beforeSend?: BeforeSendCallback
censorshipLevel?: string

service?: string
env?: string
Expand Down Expand Up @@ -81,6 +83,7 @@ export type Configuration = typeof DEFAULT_CONFIGURATION &

service?: string
beforeSend?: BeforeSendCallback
censorshipLevel?: string

isEnabled: (feature: string) => boolean
}
Expand Down Expand Up @@ -139,6 +142,10 @@ export function buildConfiguration(userConfiguration: UserConfiguration, buildEn
configuration.trackViewsManually = !!userConfiguration.trackViewsManually
}

if ('censorshipLevel' in userConfiguration) {
configuration.censorshipLevel = userConfiguration.censorshipLevel ?? configuration.censorshipLevel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should diverge from the current configuration handling here. Just keep whatever value the user provides.

Suggested change
configuration.censorshipLevel = userConfiguration.censorshipLevel ?? configuration.censorshipLevel
configuration.censorshipLevel = userConfiguration.censorshipLevel

}

return configuration
}

Expand Down
1 change: 1 addition & 0 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { startRum } from './startRum'
export interface RumUserConfiguration extends UserConfiguration {
applicationId: string
beforeSend?: (event: RumEvent, context: RumEventDomainContext) => void | boolean
censorshipLevel?: string
}

export type RumPublicApi = ReturnType<typeof makeRumPublicApi>
Expand Down
8 changes: 8 additions & 0 deletions packages/rum-recorder/src/boot/startRecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import { startSegmentCollection } from '../domain/segmentCollection'
import { send } from '../transport/send'
import { RawRecord, RecordType } from '../types'

let configurationProviderRef: Configuration | undefined

export function startRecording(
lifeCycle: LifeCycle,
applicationId: string,
configuration: Configuration,
session: RumSession,
parentContexts: ParentContexts
) {
configurationProviderRef = configuration
const { addRecord, stop: stopSegmentCollection } = startSegmentCollection(
lifeCycle,
applicationId,
Expand All @@ -36,6 +39,7 @@ export function startRecording(
stop: () => {
stopRecording()
stopSegmentCollection()
configurationProviderRef = undefined
},
}
}
Expand All @@ -47,3 +51,7 @@ export function trackViewEndRecord(lifeCycle: LifeCycle, addRawRecord: (record:
})
})
}

export function getRumRecorderConfig(): Configuration | undefined {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will speak about this a bit further. I don't think it will be that useful to have a singleton for the configuration, because the "censorship level" depends on the DOM tree, not only the user configuration.

Copy link
Contributor Author

@jagracey jagracey Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming is going to need to change as we progress through the POC.
In this case, this variable represents the default censorship mode.
The name should be changed reflect that (along with a few other vars + consts)

return configurationProviderRef
}
22 changes: 22 additions & 0 deletions packages/rum-recorder/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const enum CensorshipLevel {
PRIVATE = 'PRIVATE',
FORMS = 'FORMS',
PUBLIC = 'PUBLIC',
}

export const enum InputPrivacyMode {
NONE = 1,
IGNORED,
Expand All @@ -12,3 +18,19 @@ export const PRIVACY_ATTR_VALUE_INPUT_MASKED = 'input-masked'
export const PRIVACY_CLASS_HIDDEN = 'dd-privacy-hidden'
export const PRIVACY_CLASS_INPUT_IGNORED = 'dd-privacy-input-ignored'
export const PRIVACY_CLASS_INPUT_MASKED = 'dd-privacy-input-masked'

export const PRIVACY_INPUT_MASK = '*****'

export const FORM_PRIVATE_TAG_NAMES: { [tagName: string]: true } = {
INPUT: true,
LABEL: true,
SELECT: true,
TEXTAREA: true,
BUTTON: true,
FIELDSET: true,
DATALIST: true,
OUPUT: true,
OPTION: true,
OPTGROUP: true,
LEGEND: true,
}
4 changes: 4 additions & 0 deletions packages/rum-recorder/src/domain/record/privacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
// to obfuscate.
const PRIVACY_INPUT_TYPES_TO_IGNORE = ['email', 'password', 'tel']

const MASKING_CHAR = '᙮'

// Returns true if the given DOM node should be hidden. Ancestors
// are not checked.
export function nodeShouldBeHidden(node: Node): boolean {
Expand Down Expand Up @@ -89,3 +91,5 @@ function isElement(node: Node): node is Element {
function isInputElement(elem: Element): elem is HTMLInputElement {
return elem.tagName === 'INPUT'
}

export const censorText = (text: string) => text.replace(/[^\s]/g, MASKING_CHAR)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent:

Suggested change
export const censorText = (text: string) => text.replace(/[^\s]/g, MASKING_CHAR)
export const censorText = (text: string) => text.replace(/\S/g, MASKING_CHAR)

25 changes: 24 additions & 1 deletion packages/rum-recorder/src/domain/record/serializationUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { buildUrl } from '@datadog/browser-core'
import { InputPrivacyMode } from '../../constants'
import { CensorshipLevel, InputPrivacyMode, PRIVACY_INPUT_MASK } from '../../constants'
import { getRumRecorderConfig } from '../../boot/startRecording'
import { getNodeInputPrivacyMode, getNodeOrAncestorsInputPrivacyMode } from './privacy'
import { SerializedNodeWithId } from './types'

Expand Down Expand Up @@ -125,5 +126,27 @@ export function getElementInputValue(element: Element, ancestorInputPrivacyMode?
}

export function maskValue(value: string) {
if (isFlagEnabled('privacy-by-default-poc')) {
return value.replace(/.+/, PRIVACY_INPUT_MASK)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FMU, you are trying to replace the value with PRIVACY_INPUT_MASK, right? So the value length doesn't leak.

First, what about:

Suggested change
return value.replace(/.+/, PRIVACY_INPUT_MASK)
return PRIVACY_INPUT_MASK

This is not enough to avoid leaking input length, because we are capturing every keystroke, so we will generate one IncrementalSnapshot.Input record for each character entered by the user. We might want to adjust things around here.

But, I think we should not try to implement this in the same PR.

Copy link
Contributor Author

@jagracey jagracey Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. More to discuss, probably making a distinction between block and mask (or other synonyms) whereby mask censors (or at least throttles) JS events. I imagine block makes more sense for simple stuff like text nodes (textContent).

return PRIVACY_INPUT_MASK yeah in hindsight....😳

}
return value.replace(/./g, '*')
}

export function isFlagEnabled(feature: string): boolean {
const configuration = getRumRecorderConfig()
if (!configuration) {
return false
}
return configuration.isEnabled(feature)
}

export function getCensorshipLevel(): CensorshipLevel {
const configuration = getRumRecorderConfig()
if (!configuration) {
// Should never happen. Default to private
return CensorshipLevel.PRIVATE
}
// PENDING review from core package, core defines `censorshipLevel` as any string.
const level: CensorshipLevel = configuration.censorshipLevel as CensorshipLevel
return level
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function doStartSegmentCollection(
state = {
status: SegmentCollectionStatus.SegmentPending,
segment,
expirationTimeoutId: setTimeout(
expirationTimeoutId: window.setTimeout(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed. We'll have to audit which files are included during e2e tests and avoid including too many files in nodejs.

monitor(() => {
flushSegment('max_duration')
}),
Expand Down