Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redaction should recurse into arrays #87

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
Loading