From 3f1c3312388e422c47f0eebed4a3054e57e36a74 Mon Sep 17 00:00:00 2001 From: Uri Tevel Date: Wed, 27 Mar 2024 18:26:39 +0200 Subject: [PATCH 1/2] add redact to morgan --- packages/ecs-morgan-format/index.js | 17 +++++++++++++++++ packages/ecs-morgan-format/package.json | 1 + packages/ecs-morgan-format/test/basic.test.js | 17 +++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/packages/ecs-morgan-format/index.js b/packages/ecs-morgan-format/index.js index b3d2da7..1375ce4 100644 --- a/packages/ecs-morgan-format/index.js +++ b/packages/ecs-morgan-format/index.js @@ -24,6 +24,7 @@ const { formatHttpRequest, formatHttpResponse } = require('@elastic/ecs-helpers') +const fastRedact = require('fast-redact'); // We will query the Elastic APM agent if it is available. let elasticApm = null @@ -64,6 +65,7 @@ const stringify = safeStableStringify.configure({ deterministic: false }) // The former allows specifying other options. function ecsFormat (opts) { let format = morgan.combined + let redactPaths = []; let apmIntegration = true if (opts && typeof opts === 'object') { // Usage: ecsFormat({ /* opts */ }) @@ -73,6 +75,9 @@ function ecsFormat (opts) { if (opts.apmIntegration != null) { apmIntegration = opts.apmIntegration } + if (opts.redactPaths != null && Array.isArray(opts.redactPaths) && opts.redactPaths.length > 0) { + redactPaths = opts.redactPaths; + } } else if (opts) { // Usage: ecsFormat(format) format = opts @@ -175,6 +180,18 @@ function ecsFormat (opts) { formatHttpRequest(ecsFields, req) formatHttpResponse(ecsFields, res) + if (redactPaths.length > 0) { + const fastRedactOpts = { + paths: opts.redactPaths, + // This option tells fast-redact to just do the redactions in-place. + // Leave serialization to a separate Winston formatter. + serialize: false, + }; + const redact = fastRedact(fastRedactOpts); + + redact(ecsFields); + } + return stringify(ecsFields) } } diff --git a/packages/ecs-morgan-format/package.json b/packages/ecs-morgan-format/package.json index 0eb646c..60cb160 100644 --- a/packages/ecs-morgan-format/package.json +++ b/packages/ecs-morgan-format/package.json @@ -41,6 +41,7 @@ }, "dependencies": { "@elastic/ecs-helpers": "^2.1.1", + "fast-redact": "^3.5.0", "safe-stable-stringify": "^2.4.3" }, "devDependencies": { diff --git a/packages/ecs-morgan-format/test/basic.test.js b/packages/ecs-morgan-format/test/basic.test.js index 090986e..13f5298 100644 --- a/packages/ecs-morgan-format/test/basic.test.js +++ b/packages/ecs-morgan-format/test/basic.test.js @@ -235,3 +235,20 @@ test('can configure correlation fields', t => { t.end() }) }) + +test('redact authorization', t => { + const stream = split().on('data', line => { + const rec = JSON.parse(line) + const test = rec.http.request.headers.authorization; + t.equal(test, '[REDACTED]') + }) + const logger = morgan(ecsFormat({ + format: 'tiny', + redactPaths: ['http.request.headers.authorization'], + }), { stream }) + + makeExpressServerAndRequest(logger, '/?foo=bar', { method: 'POST', headers: { authorization: 'Bearer gjfkgkdfgkjdfk' } }, 'hi', function (err) { + t.error(err) + t.end() + }) +}) From 2dc54bef021c99102c86b988f4ef3a056cb4acf0 Mon Sep 17 00:00:00 2001 From: Uri Tevel Date: Thu, 28 Mar 2024 14:43:00 +0200 Subject: [PATCH 2/2] remove comment --- packages/ecs-morgan-format/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/ecs-morgan-format/index.js b/packages/ecs-morgan-format/index.js index 1375ce4..ac32c4b 100644 --- a/packages/ecs-morgan-format/index.js +++ b/packages/ecs-morgan-format/index.js @@ -183,8 +183,6 @@ function ecsFormat (opts) { if (redactPaths.length > 0) { const fastRedactOpts = { paths: opts.redactPaths, - // This option tells fast-redact to just do the redactions in-place. - // Leave serialization to a separate Winston formatter. serialize: false, }; const redact = fastRedact(fastRedactOpts);