-
Notifications
You must be signed in to change notification settings - Fork 40
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
[Logging] Masking authorization header #822
Conversation
759c2ec
to
30a3469
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you @amarlankri ! :)
requestHeader: { | ||
password: true, // Mask the header 'password' in the request | ||
authorization: (header: string | string[]) => { | ||
... // Handle the header value to keep non sensitve data for instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... // Handle the header value to keep non sensitve data for instance | |
... // Handle the header value to keep non sensitive data for instance |
if (typeof mask === 'function') { | ||
return { | ||
...maskedHeaders, | ||
[headerKey]: mask(headerValue), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a user-defined function, so it's very error-prone. I think we should catch the exception during the function execution.
|
||
this.logger.log( | ||
{ | ||
message, | ||
method, | ||
body: maskedBody, | ||
headers, | ||
headers: maskedHeaders, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case of exception, we could log the error message instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure to understand your proposition. You mean passing the message of the exception as the value of the header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This always logs the masked headers. I meant we should have an another flow to log the exception occured during the header masking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I proposed an implementation in my last commit. Tell me if you thought about another solution.
|
||
return { | ||
...maskedHeaders, | ||
[headerKey]: this.maskingPlaceholder, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of logging the error and leaving the hearder value unmasked. But masking with default place holder is a better solution I think.
LoggingInterceptorOptions interface is used to configure the logging interceptor.
e2233d7
to
502c8e7
Compare
Add option to
LoggingInterceptor
to mask sensitive data of header request from the log.Description
The solution implemented is described in #818.
Motivation and Context
Currently, all the headers of the request are logged:
nestjs-components/packages/logging-interceptor/src/logging.interceptor.ts
Lines 72 to 80 in c0b9a71
However, those headers may contain sensitive data. Especially, the
authorization
header may contain a JWT which can encode sensitive data, readable by anyone once decoded. So, the logging interceptor should provide a way to mask request headers.Fix #818
Types of changes