-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
61d4dd8
feat(logging): create LoggingInterceptorOptions interface
amarlankri 45e6afa
feat(logging): add option to logging interceptor to mask request headers
amarlankri 1c6b4f0
test(logging): add tests for masking option of LoggingInterceptor
amarlankri d70ced8
docs(logging): add docs masking options of logging interceptor
amarlankri a4630b1
refactor(logging): fix typo in README.md
amarlankri 0d23fd3
refactor(logging): handle potential errors in masking function
amarlankri 502c8e7
refactor(logging): do not mask headers if masking is disabled
amarlankri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { IncomingHttpHeaders } from 'http'; | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
|
@@ -14,6 +15,48 @@ import { tap } from 'rxjs/operators'; | |
import { parse, stringify } from 'flatted'; | ||
import { LogOptions, METHOD_LOG_METADATA } from './log.decorator'; | ||
|
||
/** | ||
* Logging interceptor options | ||
*/ | ||
export interface LoggingInterceptorOptions { | ||
/** | ||
* User prefix to add to the logs | ||
*/ | ||
userPrefix?: string; | ||
/** | ||
* Disable masking | ||
*/ | ||
disableMasking?: boolean; | ||
/** | ||
* Masking placeholder | ||
*/ | ||
maskingPlaceholder?: string; | ||
/** | ||
* Masking options to apply to all routes | ||
*/ | ||
mask?: LoggingInterceptorMaskingOptions; | ||
} | ||
|
||
/** | ||
* Masking options of the logging interceptor | ||
*/ | ||
export interface LoggingInterceptorMaskingOptions { | ||
/** | ||
* Masking options to apply to the headers of the request | ||
*/ | ||
requestHeader?: RequestHeaderMask; | ||
} | ||
|
||
/** | ||
* Masking options of the request headers | ||
*/ | ||
export interface RequestHeaderMask { | ||
/** | ||
* Mask of a request header. The key is the header name and the value is a boolean or a function that returns the data to log. | ||
*/ | ||
[headerKey: string]: boolean | ((headerValue: string | string[]) => unknown); | ||
} | ||
|
||
/** | ||
* Interceptor that logs input/output requests | ||
*/ | ||
|
@@ -24,11 +67,13 @@ export class LoggingInterceptor implements NestInterceptor { | |
private userPrefix: string; | ||
private disableMasking: boolean; | ||
private maskingPlaceholder: string | undefined; | ||
private mask: LoggingInterceptorMaskingOptions | undefined; | ||
|
||
constructor(@Optional() options?: { userPrefix?: string; disableMasking?: boolean; maskingPlaceholder?: string }) { | ||
constructor(@Optional() options?: LoggingInterceptorOptions) { | ||
this.userPrefix = options?.userPrefix ?? ''; | ||
this.disableMasking = options?.disableMasking ?? false; | ||
this.maskingPlaceholder = options?.maskingPlaceholder ?? '****'; | ||
this.mask = options?.mask; | ||
} | ||
|
||
/** | ||
|
@@ -54,6 +99,15 @@ export class LoggingInterceptor implements NestInterceptor { | |
public setMaskingPlaceholder(placeholder: string | undefined): void { | ||
this.maskingPlaceholder = placeholder; | ||
} | ||
|
||
/** | ||
* Set the masking options | ||
* @param mask | ||
*/ | ||
public setMask(mask: LoggingInterceptorMaskingOptions): void { | ||
this.mask = mask; | ||
} | ||
|
||
/** | ||
* Intercept method, logs before and after the request being processed | ||
* @param context details about the current request | ||
|
@@ -68,13 +122,14 @@ export class LoggingInterceptor implements NestInterceptor { | |
|
||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions | ||
const maskedBody = options?.mask?.request ? this.maskData(body, options.mask.request) : body; | ||
const maskedHeaders = this.maskHeaders(headers); | ||
|
||
this.logger.log( | ||
{ | ||
message, | ||
method, | ||
body: maskedBody, | ||
headers, | ||
headers: maskedHeaders, | ||
}, | ||
ctx, | ||
); | ||
|
@@ -209,4 +264,52 @@ export class LoggingInterceptor implements NestInterceptor { | |
|
||
return parsedData; | ||
} | ||
|
||
/** | ||
* Mask the given headers | ||
* @param headers the headers to mask | ||
* @returns the masked headers | ||
*/ | ||
private maskHeaders(headers: IncomingHttpHeaders): Record<string, unknown> { | ||
g-ongenae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (this.disableMasking || this.mask?.requestHeader === undefined) { | ||
return headers; | ||
} | ||
|
||
return Object.keys(headers).reduce<Record<string, unknown>>( | ||
(maskedHeaders: Record<string, unknown>, headerKey: string): Record<string, unknown> => { | ||
const headerValue = headers[headerKey]; | ||
const mask = this.mask?.requestHeader?.[headerKey]; | ||
|
||
if (headerValue === undefined) { | ||
return maskedHeaders; | ||
} | ||
|
||
if (mask === true) { | ||
return { | ||
...maskedHeaders, | ||
[headerKey]: this.maskingPlaceholder, | ||
}; | ||
} | ||
|
||
if (typeof mask === 'function') { | ||
try { | ||
return { | ||
...maskedHeaders, | ||
[headerKey]: mask(headerValue), | ||
}; | ||
} catch (err) { | ||
this.logger.warn(`LoggingInterceptor - Masking error for header ${headerKey}`, err); | ||
|
||
return { | ||
...maskedHeaders, | ||
[headerKey]: this.maskingPlaceholder, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}; | ||
} | ||
} | ||
|
||
return maskedHeaders; | ||
}, | ||
headers, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.