Skip to content

Commit d163d6b

Browse files
committed
chore: add support for arbitrarily deep object redacting
1 parent cb5a83b commit d163d6b

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/mongodb-redact/src/index.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,22 @@ db.history.updateOne({"_id": ObjectId("63ed1d522d8573fa5c203660")}, {$set:{chang
2525

2626
describe('mongodb-redact', function () {
2727
describe('Secrets', function () {
28-
it('should redact provided secrets', function () {
28+
it('should redact provided secrets in strings', function () {
2929
const res = redact('foo@bar', [{ value: 'foo@bar', kind: 'password' }]);
3030
expect(res).to.equal('<password>');
3131
});
32+
33+
it('should redact provided secrets in objects', function () {
34+
const res = redact({ key: 'foo@bar' }, [
35+
{ value: 'foo@bar', kind: 'password' },
36+
]);
37+
expect(res).to.deep.equal({ key: '<password>' });
38+
});
39+
40+
it('should redact provided secrets in arrays', function () {
41+
const res = redact(['foo@bar'], [{ value: 'foo@bar', kind: 'password' }]);
42+
expect(res).to.deep.equal(['<password>']);
43+
});
3244
});
3345

3446
describe('Types', function () {

packages/mongodb-redact/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ export function redact<T>(
1414
if (isPlainObject(message)) {
1515
// recursively walk through all values of an object
1616
return Object.fromEntries(
17-
Object.entries(message).map(([key, value]) => [key, redact(value)]),
17+
Object.entries(message).map(([key, value]) => [
18+
key,
19+
redact(value, secrets),
20+
]),
1821
) as T;
1922
}
2023
if (Array.isArray(message)) {
2124
// walk through array and redact each value
22-
return message.map((msg) => redact(msg)) as T;
25+
return message.map((msg) => redact(msg, secrets)) as T;
2326
}
2427
if (typeof message !== 'string') {
2528
// all non-string types can be safely returned

0 commit comments

Comments
 (0)