Skip to content

Commit

Permalink
Fix wildcard redacting with a custom censor function (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
runk authored and mcollina committed Mar 27, 2019
1 parent 9a28e82 commit 8551286
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/redaction.js
Expand Up @@ -72,8 +72,10 @@ function redaction (opts, serialize) {
const result = {
[redactFmtSym]: fastRedact({ paths, censor, serialize, strict })
}
const serializedCensor = serialize(censor)
const topCensor = () => serializedCensor

const topCensor = (...args) =>
typeof censor === 'function' ? serialize(censor(...args)) : serialize(censor)

return [...Object.keys(shape), ...Object.getOwnPropertySymbols(shape)].reduce((o, k) => {
// top level key:
if (shape[k] === null) o[k] = topCensor
Expand Down
26 changes: 26 additions & 0 deletions test/redact.test.js
Expand Up @@ -481,6 +481,32 @@ test('redact – supports top level wildcard', async ({ is }) => {
is(req, '[Redacted]')
})

test('redact – supports top level wildcard with a censor function', async ({ is }) => {
const stream = sink()
const instance = pino({
redact: {
paths: ['*'],
censor: () => '[Redacted]'
}
}, stream)
instance.info({
req: {
id: 7915,
method: 'GET',
url: '/',
headers: {
host: 'localhost:3000',
connection: 'keep-alive',
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
},
remoteAddress: '::ffff:127.0.0.1',
remotePort: 58022
}
})
const { req } = await once(stream, 'data')
is(req, '[Redacted]')
})

test('redact – supports top level wildcard and leading wildcard', async ({ is }) => {
const stream = sink()
const instance = pino({ redact: ['*', '*.req'] }, stream)
Expand Down

0 comments on commit 8551286

Please sign in to comment.