Files
appium/packages/logger/test/unit/preprocessor.spec.ts
T
Mohamed Mostafa 8ac6bfcc3b fix(logger): mask secure values surrounded by non-word characters (#22553)
`\b` only asserts a boundary next to a word character, so text rules like
`P@ssw0rd!` never matched anything. Now it is only added to the sides where
the value itself begins or ends with a word character.
2026-07-26 10:34:47 -07:00

92 lines
4.0 KiB
TypeScript

import {describe, it, beforeEach} from 'node:test';
import {expect} from 'chai';
import {SecureValuesPreprocessor} from '../../lib/secure-values-preprocessor';
describe('Log Internals', function () {
let preprocessor: SecureValuesPreprocessor;
beforeEach(function () {
preprocessor = new SecureValuesPreprocessor();
});
it('should preprocess a string and make replacements', async function () {
const issues = await preprocessor.loadRules(['yolo']);
expect(issues.length).to.eql(0);
expect(preprocessor.rules.length).to.eql(1);
const replacer = preprocessor.rules[0].replacer;
expect(preprocessor.preprocess(':yolo" yo Yolo yyolo')).to.eql(`:${replacer}" yo Yolo yyolo`);
});
it('should preprocess a string and make replacements with multiple simple rules', async function () {
const issues = await preprocessor.loadRules(['yolo', 'yo']);
expect(issues.length).to.eql(0);
expect(preprocessor.rules.length).to.eql(2);
const replacer = preprocessor.rules[0].replacer;
expect(preprocessor.preprocess(':yolo" yo Yolo yyolo')).to.eql(`:${replacer}" ${replacer} Yolo yyolo`);
});
it('should preprocess a string and make replacements with multiple complex rules', async function () {
const replacer2 = '***';
const issues = await preprocessor.loadRules([
{text: 'yolo', flags: 'i'},
{pattern: '^:', replacer: replacer2},
]);
expect(issues.length).to.eql(0);
expect(preprocessor.rules.length).to.eql(2);
const replacer = preprocessor.rules[0].replacer;
expect(preprocessor.preprocess(':yolo" yo Yolo yyolo')).to.eql(`${replacer2}${replacer}" yo ${replacer} yyolo`);
});
it(`should preprocess a string and apply a rule where 'pattern' has priority over 'text'`, async function () {
const replacer = '***';
const issues = await preprocessor.loadRules([{pattern: '^:', text: 'yo', replacer}]);
expect(issues.length).to.eql(0);
expect(preprocessor.rules.length).to.eql(1);
expect(preprocessor.preprocess(':yolo" yo Yolo yyolo')).to.eql(`${replacer}yolo" yo Yolo yyolo`);
});
it('should preprocess a string and make replacements with multiple complex rules and issues', async function () {
const replacer2 = '***';
const issues = await preprocessor.loadRules([
{text: 'yolo', flags: 'i'},
{pattern: '^:(', replacer: replacer2},
]);
expect(issues.length).to.eql(1);
expect(preprocessor.rules.length).to.eql(1);
const replacer = preprocessor.rules[0].replacer;
expect(preprocessor.preprocess(':yolo" yo Yolo yyolo')).to.eql(`:${replacer}" yo ${replacer} yyolo`);
});
it('should preprocess values starting or ending with non-word characters', async function () {
const issues = await preprocessor.loadRules(['P@ssw0rd!', '+15550100', '#hunter2']);
expect(issues.length).to.eql(0);
const replacer = preprocessor.rules[0].replacer;
expect(preprocessor.preprocess('P@ssw0rd! call +15550100 tag #hunter2')).to.eql(
`${replacer} call ${replacer} tag ${replacer}`,
);
});
it(`should preprocess a value with non-word characters given as 'text'`, async function () {
const issues = await preprocessor.loadRules([{text: '$ecret'}]);
expect(issues.length).to.eql(0);
const replacer = preprocessor.rules[0].replacer;
expect(preprocessor.preprocess('the token is $ecret')).to.eql(`the token is ${replacer}`);
});
it('should not preprocess a value which is a part of a longer word', async function () {
const issues = await preprocessor.loadRules(['yolo']);
expect(issues.length).to.eql(0);
expect(preprocessor.preprocess('yolos yyolo')).to.eql('yolos yyolo');
});
it('should leave the string unchanged if all rules have issues', async function () {
const replacer2 = '***';
const issues = await preprocessor.loadRules([null, {flags: 'i'}, {pattern: '^:(', replacer: replacer2}] as any);
expect(issues.length).to.eql(3);
expect(preprocessor.rules.length).to.eql(0);
expect(preprocessor.preprocess(':yolo" yo Yolo yyolo')).to.eql(':yolo" yo Yolo yyolo');
});
});