Skip to content

Commit ebfde81

Browse files
committed
Add mask-unless-allowlisted privacy level support for standard attributes action names
1 parent 3310e9c commit ebfde81

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

packages/rum-core/src/domain/action/getActionNameFromElement.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ describe('getActionNameFromElement', () => {
530530
expect(nameSource).toBe('mask_placeholder')
531531
})
532532

533-
it('preserves privacy level of the element when node privacy level is mask-unless-allowlisted', () => {
533+
fit('preserves privacy level of the element when node privacy level is mask-unless-allowlisted', () => {
534534
const testCases = [
535535
{
536536
html: `
@@ -643,6 +643,16 @@ describe('getActionNameFromElement', () => {
643643
expectedName: 'foo bar baz',
644644
expectedNameSource: 'text_content',
645645
},
646+
{
647+
html: `
648+
<div data-dd-privacy="mask-unless-allowlisted" aria-label="bar" target>
649+
<span>bar</span>
650+
</div>
651+
`,
652+
defaultPrivacyLevel: NodePrivacyLevel.MASK_UNLESS_ALLOWLISTED,
653+
expectedName: 'Masked Element',
654+
expectedNameSource: 'standard_attribute',
655+
},
646656
]
647657
testCases.forEach(({ html, defaultPrivacyLevel, allowlist, expectedName, expectedNameSource }) => {
648658
mockExperimentalFeatures([ExperimentalFeature.USE_TREE_WALKER_FOR_ACTION_NAME])

packages/rum-core/src/domain/action/getActionNameFromElement.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ExperimentalFeature, isExperimentalFeatureEnabled, safeTruncate } from '@datadog/browser-core'
22
import { getPrivacySelector, NodePrivacyLevel } from '../privacyConstants'
3-
import { getNodePrivacyLevel, shouldMaskNode } from '../privacy'
3+
import { getNodePrivacyLevel, maskDisallowedTextContent, shouldMaskNode } from '../privacy'
44
import type { NodePrivacyLevelCache } from '../privacy'
55
import type { RumConfiguration } from '../configuration'
66
import { isElementNode } from '../../browser/htmlDomUtils'
@@ -84,7 +84,7 @@ const priorityStrategies: NameStrategy[] = [
8484
return getActionNameFromTextualContent(element, rumConfiguration)
8585
}
8686
},
87-
(element) => getActionNameFromStandardAttribute(element, 'aria-label'),
87+
(element, rumConfiguration) => getActionNameFromStandardAttribute(element, 'aria-label', rumConfiguration),
8888
// associated element text designated by the aria-labelledby attribute
8989
(element, rumConfiguration) => {
9090
const labelledByAttribute = element.getAttribute('aria-labelledby')
@@ -100,10 +100,10 @@ const priorityStrategies: NameStrategy[] = [
100100
}
101101
}
102102
},
103-
(element) => getActionNameFromStandardAttribute(element, 'alt'),
104-
(element) => getActionNameFromStandardAttribute(element, 'name'),
105-
(element) => getActionNameFromStandardAttribute(element, 'title'),
106-
(element) => getActionNameFromStandardAttribute(element, 'placeholder'),
103+
(element, rumConfiguration) => getActionNameFromStandardAttribute(element, 'alt', rumConfiguration),
104+
(element, rumConfiguration) => getActionNameFromStandardAttribute(element, 'name', rumConfiguration),
105+
(element, rumConfiguration) => getActionNameFromStandardAttribute(element, 'title', rumConfiguration),
106+
(element, rumConfiguration) => getActionNameFromStandardAttribute(element, 'placeholder', rumConfiguration),
107107
// SELECT first OPTION text
108108
(element, rumConfiguration) => {
109109
if ('options' in element && element.options.length > 0) {
@@ -169,9 +169,19 @@ function getElementById(refElement: Element, id: string) {
169169
return refElement.ownerDocument ? refElement.ownerDocument.getElementById(id) : null
170170
}
171171

172-
function getActionNameFromStandardAttribute(element: Element | HTMLElement, attribute: string): ActionName {
172+
function getActionNameFromStandardAttribute(
173+
element: Element | HTMLElement,
174+
attribute: string,
175+
rumConfiguration: RumConfiguration
176+
): ActionName {
177+
const { enablePrivacyForActionName, defaultPrivacyLevel } = rumConfiguration
178+
const nodeSelfPrivacyLevel = getNodePrivacyLevel(element, defaultPrivacyLevel)
179+
const attributeValue = element.getAttribute(attribute)
173180
return {
174-
name: element.getAttribute(attribute) || '',
181+
name:
182+
enablePrivacyForActionName && nodeSelfPrivacyLevel === NodePrivacyLevel.MASK_UNLESS_ALLOWLISTED
183+
? maskDisallowedTextContent(attributeValue || '', ACTION_NAME_PLACEHOLDER)
184+
: attributeValue || '',
175185
nameSource: ActionNameSource.STANDARD_ATTRIBUTE,
176186
}
177187
}

packages/rum-core/src/domain/action/trackClickActions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ function computeClickActionBase(
251251
}
252252

253253
const { name, nameSource } = getActionNameFromElement(event.target, configuration, nodePrivacyLevel)
254+
console.log('name', name, nameSource, event.target, nodePrivacyLevel, configuration)
254255

255256
return {
256257
type: ActionType.CLICK,

0 commit comments

Comments
 (0)