Skip to content

Commit

Permalink
fix: redaction should recurse into arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshMock committed Mar 7, 2024
1 parent af8ff61 commit cbc1c58
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ export function redactObject (obj: Record<string, any>, additionalKeys: string[]
value = `${value.origin}${value.pathname}${value.search}`
}

if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
if (seen.get(value) !== true) {
// if this Object hasn't been seen, recursively redact it
seen.set(value, true)
value = doRedact(value)
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
// if it's an array, redact each item
value = value.map(v => doRedact(v))
} else {
// if it has been seen, set the value that goes in newObj to null
// this is what prevents the circular references
value = null
if (seen.get(value) !== true) {
// if this Object hasn't been seen, recursively redact it
seen.set(value, true)
value = doRedact(value)
} else {
// if it has been seen, set the value that goes in newObj to null
// this is what prevents the circular references
value = null
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/unit/security.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,18 @@ test('redactObject', t => {
t.equal(result.url, 'http://foo.com/path/to/endpoint?query=true')
})

t.test('properly recurses into arrays', t => {
t.plan(2)
const result = redactObject({
foo: [
{ authorization: 'foo' },
{ password: 'bar' },
]
})

t.notMatch(result.foo[0].authorization, 'foo')
t.notMatch(result.foo[1].password, 'bar')
})

t.end()
})

0 comments on commit cbc1c58

Please sign in to comment.