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

chore(logging): log all headers but truncate or redact sensitive values #1646

Closed
wants to merge 6 commits into from
43 changes: 28 additions & 15 deletions packages/server/logging/expressLogging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ export const LoggingExpressMiddleware = HttpLogger({
}
return 'info'
},
redact: {
paths: [
'req.headers.authorization',
'req.headers.cookie',
'req.headers["cf-connecting-ip"]',
'req.headers["true-client-ip"]',
'req.headers["x-real-ip"]',
'req.headers["x-forwarded-for"]'
],
censor: (value, path) => {
if (
path.length === 3 &&
path[0] === 'req' &&
path[1] === 'headers' &&
path[2] === 'authorization' &&
typeof value === 'string'
) {
if (value.toLocaleLowerCase().startsWith('bearer ')) {
return `${value.slice(0, 17)}[TRUNCATED(original_length:${value.length})]`
}
return `${value.slice(0, 10)}[TRUNCATED(original_length:${value.length})]`
}
return `[REDACTED(length:${value.length})]`
}
},

// we need to redact any potential sensitive data from being logged.
// as we do not know what headers may be sent in a request by a user or client
Expand All @@ -43,20 +68,8 @@ export const LoggingExpressMiddleware = HttpLogger({
id: req.raw.id,
method: req.raw.method,
path: req.raw.url?.split('?')[0], // Remove query params which might be sensitive
// Allowlist useful headers
headers: Object.fromEntries(
Object.entries(req.raw.headers).filter(
([key]) =>
![
'cookie',
'authorization',
'cf-connecting-ip',
'true-client-ip',
'x-real-ip',
'x-forwarded-for'
].includes(key.toLocaleLowerCase())
)
)
// headers are managed by the redact config above
headers: req.headers
}
}),
res: pino.stdSerializers.wrapResponseSerializer((res) => {
Expand All @@ -68,7 +81,7 @@ export const LoggingExpressMiddleware = HttpLogger({
return {
statusCode: res.raw.statusCode,
// Allowlist useful headers
headers: resRaw.raw.headers
headers: resRaw.headers
}
})
}
Expand Down