From bc587c0c920e265c6525e0d2d7688b4f095ed22b Mon Sep 17 00:00:00 2001 From: Alex Malin Date: Sun, 4 Oct 2020 17:40:44 +0200 Subject: [PATCH] Making sure path is passed to censor when there is only a top level key. --- lib/proto.js | 2 +- lib/redaction.js | 7 ++++--- test/redact.test.js | 10 ++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index 5d1bb088a..2de4fc4b9 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -71,7 +71,7 @@ module.exports = function () { } const resetChildingsFormatter = bindings => bindings -function child (bindings) { +function child (bindings, ...args) { if (!bindings) { throw Error('missing bindings for child Pino') } diff --git a/lib/redaction.js b/lib/redaction.js index 7e45a78c2..4ab7b2849 100644 --- a/lib/redaction.js +++ b/lib/redaction.js @@ -76,13 +76,14 @@ function redaction (opts, serialize) { [redactFmtSym]: fastRedact({ paths, censor, serialize, strict }) } - const topCensor = (...args) => - typeof censor === 'function' ? serialize(censor(...args)) : serialize(censor) + const topCensor = (...args) => { + return 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 + o[k] = (value) => topCensor(value, [k]) } else { const wrappedCensor = typeof censor === 'function' ? (value, path) => { return censor(value, [k, ...path]) diff --git a/test/redact.test.js b/test/redact.test.js index 89ffca267..d40e938cb 100644 --- a/test/redact.test.js +++ b/test/redact.test.js @@ -220,6 +220,16 @@ test('redact.censor option – sets the redact value', async ({ is }) => { }) test('redact.censor option – can be a function that accepts value and path arguments', async ({ is }) => { + const stream = sink() + const instance = pino({ redact: { paths: ['topLevel'], censor: (value, path) => value + ' ' + path.join('.') } }, stream) + instance.info({ + topLevel: 'test' + }) + const { topLevel } = await once(stream, 'data') + is(topLevel, 'test topLevel') +}) + +test('redact.censor option – can be a function that accepts value and path arguments (nested path)', async ({ is }) => { const stream = sink() const instance = pino({ redact: { paths: ['req.headers.cookie'], censor: (value, path) => value + ' ' + path.join('.') } }, stream) instance.info({