Skip to content

Commit

Permalink
apply text mask settings to inputs rrweb-io#1096
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellanoce committed Apr 6, 2023
1 parent f355dd3 commit 7c3f51c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 3 deletions.
26 changes: 26 additions & 0 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,11 @@ function serializeNode(
keepIframeSrcFn,
newlyAddedElement,
rootId,
maskAllText,
maskTextClass,
unmaskTextClass,
maskTextSelector,
unmaskTextSelector,
});
case n.TEXT_NODE:
return serializeTextNode(n as Text, {
Expand Down Expand Up @@ -695,6 +700,11 @@ function serializeElementNode(
*/
newlyAddedElement?: boolean;
rootId: number | undefined;
maskAllText: boolean;
maskTextClass: string | RegExp;
unmaskTextClass: string | RegExp;
maskTextSelector: string | null;
unmaskTextSelector: string | null;
},
): serializedNode | false {
const {
Expand All @@ -710,6 +720,11 @@ function serializeElementNode(
keepIframeSrcFn,
newlyAddedElement = false,
rootId,
maskAllText,
maskTextClass,
unmaskTextClass,
maskTextSelector,
unmaskTextSelector,
} = options;
const needBlock = _isBlockedElement(n, blockClass, blockSelector);
const tagName = getValidTagName(n);
Expand Down Expand Up @@ -771,12 +786,23 @@ function serializeElementNode(
: typeof attributes.type === 'string'
? attributes.type.toLowerCase()
: null;

const forceMask = needMaskingText(
n,
maskTextClass,
maskTextSelector,
unmaskTextClass,
unmaskTextSelector,
maskAllText,
);

attributes.value = maskInputValue({
type,
tagName,
value,
maskInputOptions,
maskInputFn,
forceMask,
});
} else if (checked) {
attributes.checked = checked;
Expand Down
5 changes: 4 additions & 1 deletion packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,22 @@ export function maskInputValue({
type,
value,
maskInputFn,
forceMask,
}: {
maskInputOptions: MaskInputOptions;
tagName: string;
type: string | null;
value: string | null;
maskInputFn?: MaskInputFn;
forceMask?: boolean;
}): string {
let text = value || '';
const actualType = type && type.toLowerCase();

if (
maskInputOptions[tagName.toLowerCase() as keyof MaskInputOptions] ||
(actualType && maskInputOptions[actualType as keyof MaskInputOptions])
(actualType && maskInputOptions[actualType as keyof MaskInputOptions]) ||
forceMask
) {
if (maskInputFn) {
text = maskInputFn(text);
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ function record<T = eventWithTime>(
unmaskTextSelector,
inlineStylesheet,
maskAllInputs: maskInputOptions,
maskInputFn,
maskTextFn,
slimDOM: slimDOMOptions,
dataURLOptions,
Expand Down
10 changes: 10 additions & 0 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,22 @@ export default class MutationBuffer {
if (attributeName === 'value') {
const type = getInputType(target);

const forceMask = needMaskingText(
m.target,
this.maskTextClass,
this.maskTextSelector,
this.unmaskTextClass,
this.unmaskTextSelector,
this.maskAllText,
);

value = maskInputValue({
maskInputOptions: this.maskInputOptions,
tagName: target.tagName,
type,
value,
maskInputFn: this.maskInputFn,
forceMask,
});
}
if (
Expand Down
25 changes: 23 additions & 2 deletions packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { MaskInputOptions, maskInputValue, Mirror } from 'rrweb-snapshot';
import {
MaskInputOptions,
maskInputValue,
Mirror,
needMaskingText,
} from 'rrweb-snapshot';
import type { FontFaceSet } from 'css-font-loading-module';
import {
throttle,
Expand Down Expand Up @@ -355,6 +360,11 @@ function initInputObserver({
maskInputFn,
sampling,
userTriggeredOnInput,
maskAllText,
maskTextClass,
unmaskTextClass,
maskTextSelector,
unmaskTextSelector,
}: observerParam): listenerHandler {
function eventHandler(event: Event) {
let target = getEventTarget(event) as HTMLElement | null;
Expand Down Expand Up @@ -384,18 +394,29 @@ function initInputObserver({
let isChecked = false;
const type: Lowercase<string> = getInputType(target) || '';

const forceMask = needMaskingText(
target as Node,
maskTextClass,
maskTextSelector,
unmaskTextClass,
unmaskTextSelector,
maskAllText,
);

if (type === 'radio' || type === 'checkbox') {
isChecked = (target as HTMLInputElement).checked;
} else if (
maskInputOptions[tagName.toLowerCase() as keyof MaskInputOptions] ||
maskInputOptions[type as keyof MaskInputOptions]
maskInputOptions[type as keyof MaskInputOptions] ||
forceMask
) {
text = maskInputValue({
maskInputOptions,
tagName,
type,
value: text,
maskInputFn,
forceMask,
});
}
cbWithDedup(
Expand Down
23 changes: 23 additions & 0 deletions packages/rrweb/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ describe('record integration tests', function (this: ISuite) {
assertSnapshot(snapshots);
});

it('can use maskTextSelector to configure which inputs should be masked', async () => {
const page: puppeteer.Page = await browser.newPage();
await page.goto('about:blank');
await page.setContent(
getHtml.call(this, 'form.html', {
maskTextSelector: 'input[type="text"],textarea',
maskInputFn: () => '*'.repeat(10),
}),
);

await page.type('input[type="text"]', 'test');
await page.click('input[type="radio"]');
await page.click('input[type="checkbox"]');
await page.type('textarea', 'textarea test');
await page.type('input[type="password"]', 'password');
await page.select('select', '1');

const snapshots = (await page.evaluate(
'window.snapshots',
)) as eventWithTime[];
assertSnapshot(snapshots);
});

it('should mask password value attribute with maskInputOptions', async () => {
const page: puppeteer.Page = await browser.newPage();
await page.goto('about:blank');
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ export function generateRecordSnippet(options: recordOptions<eventWithTime>) {
maskTextSelector: ${JSON.stringify(options.maskTextSelector)},
maskAllInputs: ${options.maskAllInputs},
maskInputOptions: ${JSON.stringify(options.maskAllInputs)},
maskInputFn: ${options.maskInputFn},
userTriggeredOnInput: ${options.userTriggeredOnInput},
maskTextFn: ${options.maskTextFn},
recordCanvas: ${options.recordCanvas},
Expand Down

0 comments on commit 7c3f51c

Please sign in to comment.