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

Signals: add redaction to formData #1179

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/two-pillows-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-signals': patch
---

Redact formData
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ describe(redactSignalData, () => {
it('should redact the value in the "target" property if the type is "interaction"', () => {
const signal = factories.createInteractionSignal({
eventType: 'change',
target: { value: 'secret' },
target: { value: 'secret', formData: { password: '123' } },
})
const expected = factories.createInteractionSignal({
eventType: 'change',
target: { value: 'XXX' },
target: { value: 'XXX', formData: { password: 'XXX' } },
})
expect(redactSignalData(signal)).toEqual(expected)
})
Expand Down
11 changes: 9 additions & 2 deletions packages/signals/signals/src/core/client/redact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import { Signal } from '@segment/analytics-signals-runtime'
export const redactSignalData = (signalArg: Signal): Signal => {
const signal = structuredClone(signalArg)
if (signal.type === 'interaction') {
if ('target' in signal.data && 'value' in signal.data.target) {
signal.data.target.value = redactJsonValues(signal.data.target.value)
if ('target' in signal.data) {
if ('value' in signal.data.target) {
signal.data.target.value = redactJsonValues(signal.data.target.value)
}
if ('formData' in signal.data.target) {
signal.data.target.formData = redactJsonValues(
signal.data.target.formData
)
}
}
} else if (signal.type === 'network') {
signal.data = redactJsonValues(signal.data, 2)
Expand Down
12 changes: 8 additions & 4 deletions packages/signals/signals/src/core/signal-generators/dom-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ interface ParsedElementBase {
id: string
labels?: Label[]
label?: Label
name: string
name?: string
nodeName: string
tagName: string
title: string
type: string
value: string
type?: string
value?: string
textContent?: string
innerText?: string
}
Expand All @@ -88,6 +88,8 @@ interface ParsedMediaElement extends ParsedElementBase {

interface ParsedHTMLFormElement extends ParsedElementBase {
formData: Record<string, string>
innerText: never
textContent: never
}

type AnyParsedElement =
Expand All @@ -99,7 +101,7 @@ type AnyParsedElement =

const parseElement = (el: HTMLElement): AnyParsedElement => {
const labels = parseLabels((el as HTMLInputElement).labels)
const base = {
const base: ParsedElementBase = {
// adding a bunch of fields that are not on _all_ elements, but are on enough that it's useful to have them here.
attributes: parseNodeMap(el.attributes),
classList: [...el.classList],
Expand Down Expand Up @@ -147,6 +149,8 @@ const parseElement = (el: HTMLElement): AnyParsedElement => {
} else if (el instanceof HTMLFormElement) {
return {
...base,
innerText: undefined,
textContent: undefined,
formData: parseFormData(new FormData(el)),
}
}
Expand Down
Loading