Skip to content

Commit

Permalink
fix: Input censor is always preserved for redact
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradLang committed Sep 20, 2023
1 parent 8bcb486 commit a4cc18b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 5 deletions.
92 changes: 92 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,98 @@ testLog(
['remove'],
);

testLog(
'should use input censor when redacting',
{
redact: 'Should be redacted',
},
{
redact: '[SHHH]',
},
'info',
{
redact: {
paths: ['redact'],
censor: (_value, _path) => '[SHHH]',
},
},
);

testLog(
'should not use input censor when removing',
{
remove: 'Should be "removed"',
},
{},
'info',
{
redact: {
paths: ['remove'],
remove: true,
censor: (_value, _path) => 'You have been erased!',
},
},
['remove'],
);

testLog(
'should not use input censor when redacting and removing with no redact paths',
{
remove: 'Should be "removed"',
},
{},
'info',
{
redact: {
paths: [],
removePaths: ['remove'],
censor: (_value, _path) => 'You have been erased!',
},
},
['remove'],
);

testLog(
'should use input censor function to redact when redacting and removing',
{
redact: 'Should be redacted',
remove: 'Should be "removed"',
},
{
redact: '[SHHH]',
},
'info',
{
redact: {
paths: ['redact'],
removePaths: ['remove'],
censor: (_value, path) =>
path.includes('redact') ? '[SHHH]' : 'You have been erased!',
},
},
['remove'],
);

testLog(
'should use input censor text to redact when redacting and removing',
{
redact: 'Should be redacted',
remove: 'Should be "removed"',
},
{
redact: '[SHHH]',
},
'info',
{
redact: {
paths: ['redact'],
removePaths: ['remove'],
censor: '[SHHH]',
},
},
['remove'],
);

testLog(
'should remove specified paths',
{
Expand Down
15 changes: 10 additions & 5 deletions src/redact/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,20 @@ const configureRedactCensor = (redact: ExtendedRedact): ExtendedRedact => {
return redact;
}

const { paths: redactPaths, removePaths } = redact;
const redactSet = new Set(redactPaths);
const { paths: redactPaths, removePaths, censor } = redact;
const removeSet = new Set(removePaths);
const censorText = typeof censor === 'string' ? censor : '[Redacted]';
const censorPath = (value: unknown, path: string[]): unknown =>
typeof censor === 'function' ? censor(value, path) : censorText;

return redactPaths.length === 0
? { paths: removePaths, remove: true }
? { paths: removePaths, remove: true, censor }
: {
paths: [...redactPaths, ...removePaths],
censor: (_value, path) =>
redactSet.has(keyFromPath(path)) ? '[Redacted]' : undefined,
censor: (value, path) =>
removeSet.has(keyFromPath(path))
? undefined
: censorPath(value, path),
};
};

Expand Down

0 comments on commit a4cc18b

Please sign in to comment.