Skip to content

Commit

Permalink
refactor(logging): do not mask headers if masking is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
amarlankri committed Dec 7, 2023
1 parent 0d23fd3 commit 502c8e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/logging-interceptor/src/logging.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ export class LoggingInterceptor implements NestInterceptor {
* @returns the masked headers
*/
private maskHeaders(headers: IncomingHttpHeaders): Record<string, unknown> {
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];
Expand Down
13 changes: 13 additions & 0 deletions packages/logging-interceptor/test/logging.interceptor.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-lines */
import {
BadRequestException,
HttpStatus,
Expand Down Expand Up @@ -487,5 +488,17 @@ describe('Logging interceptor', () => {

expect(logSpy.mock.calls[0][0].headers.authorization).toBe(placeholder);
});

it('should not mask request headers if masking is disabled', async () => {
const interceptor = app.get(ApplicationConfig).getGlobalInterceptors()[0] as LoggingInterceptor;
interceptor.setMask({ requestHeader: { authorization: true } });
interceptor.setDisableMasking(true);
const logSpy: jest.SpyInstance = jest.spyOn(Logger.prototype, 'log');
const url: string = `/cats/ok`;

await request(app.getHttpServer()).get(url).set('authorization', 'Bearer JWT').expect(HttpStatus.OK);

expect(logSpy.mock.calls[0][0].headers.authorization).toBe('Bearer JWT');
});
});
});

0 comments on commit 502c8e7

Please sign in to comment.