Skip to content

Commit

Permalink
Merge branch 'main' of github.com:davidmarkclements/fast-redact
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Mar 7, 2024
2 parents cc56593 + 59b84b2 commit 2c2c4be
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/redactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ function redactor ({ secret, serialize, wcLen, strict, isCensorFct, censorFctTak
${strictImpl(strict, serialize)}
}
const { censor, secret } = this
const originalSecret = {}
const secretKeys = Object.keys(secret)
for (var i = 0; i < secretKeys.length; i++) {
originalSecret[secretKeys[i]] = secret[secretKeys[i]]
}
${redactTmpl(secret, isCensorFct, censorFctTakesPath)}
this.compileRestore()
${dynamicRedactTmpl(wcLen > 0, isCensorFct, censorFctTakesPath)}
this.secret = originalSecret
${resultTmpl(serialize)}
`).bind(state)

redact.secret = secret

if (serialize === false) {
redact.restore = (o) => state.restore(o)
}
Expand Down
8 changes: 5 additions & 3 deletions lib/restorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ function restoreTmpl (resetters, paths, hasWildcards) {
for (var i = len - 1; i >= ${paths.length}; i--) {
const k = keys[i]
const o = secret[k]
if (o.flat === true) this.groupRestore(o)
else this.nestedRestore(o)
secret[k] = null
if (o) {
if (o.flat === true) this.groupRestore(o)
else this.nestedRestore(o)
secret[k] = null
}
}
` : ''

Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ test('masks according to supplied censor function with nested wildcards', ({ end
end()
})

test('does not increment secret size', ({ end, is }) => {
const redact = fastRedact({ paths: ['*.b'], censor: censorFct, serialize: false })
is(redact({ a: { b: '0123456' } }).a.b, 'xxx56')
is(redact.secret[''].length, 1)
is(redact({ c: { b: '0123456', d: 'pristine' } }).c.b, 'xxx56')
is(redact.secret[''].length, 1)
is(redact({ c: { b: '0123456', d: 'pristine' } }).c.d, 'pristine')
is(redact.secret[''].length, 1)
end()
})

test('masks according to supplied censor-with-path function', ({ end, is }) => {
const redact = fastRedact({ paths: ['a'], censor: censorWithPath, serialize: false })
is(redact({ a: '0123456' }).a, 'a xxx56')
Expand Down

0 comments on commit 2c2c4be

Please sign in to comment.