Skip to content

Commit

Permalink
Signals: add redaction to formData (#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Nov 1, 2024
1 parent 5190f92 commit ed7a749
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
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

0 comments on commit ed7a749

Please sign in to comment.