Skip to content

add redact to morgan #185

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions packages/ecs-morgan-format/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */ })
Expand All @@ -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
Expand Down Expand Up @@ -175,6 +180,16 @@ function ecsFormat (opts) {
formatHttpRequest(ecsFields, req)
formatHttpResponse(ecsFields, res)

if (redactPaths.length > 0) {
const fastRedactOpts = {
paths: opts.redactPaths,
serialize: false,
};
const redact = fastRedact(fastRedactOpts);

redact(ecsFields);
}

return stringify(ecsFields)
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/ecs-morgan-format/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"@elastic/ecs-helpers": "^2.1.1",
"fast-redact": "^3.5.0",
"safe-stable-stringify": "^2.4.3"
},
"devDependencies": {
Expand Down
17 changes: 17 additions & 0 deletions packages/ecs-morgan-format/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})