Skip to content

Commit

Permalink
feat: add an option to disable header masking for a route
Browse files Browse the repository at this point in the history
  • Loading branch information
Bendakh committed Sep 13, 2024
1 parent 3559108 commit ffc7776
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/logging-interceptor/src/log.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export interface MaskingOptions {
* If it is an array of strings, it will mask only the specified fields.
*/
response?: string[] | boolean;
/**
* If true, it will disable the header masking.
*/
disableHeaderMask?: boolean;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/logging-interceptor/src/logging.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ 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);
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
const maskedHeaders = options?.mask?.disableHeaderMask ? headers : this.maskHeaders(headers);

this.logger.log(
{
Expand Down
11 changes: 11 additions & 0 deletions packages/logging-interceptor/test/logging.interceptor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,17 @@ describe('Logging interceptor', () => {
expect(logSpy.mock.calls[0][0].headers.authorization).toBe('Bearer JWT');
});

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

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

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

it('should not mask a request header if the corresponding mask is false', async () => {
const interceptor = app.get(ApplicationConfig).getGlobalInterceptors()[0] as LoggingInterceptor;
interceptor.setMask({ requestHeader: { authorization: false } });
Expand Down
16 changes: 16 additions & 0 deletions packages/logging-interceptor/test/test-app/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ export class CatsController {
return { id: 1, ...payload };
}

@Post('header')
@Log({
mask: {
request: ['birthdate', 'interests.description', 'address', 'enemies'],
response: ['id', 'birthdate', 'interests.description', 'address', 'enemies'],
disableHeaderMask: true,
},
})
public createCatUnmaskedHeader(@Body() payload: CreateCatDto) {
if (payload.name === 'dog') {
throw new BadRequestException({ message: 'You cannot name a cat dog' });
}

return { id: 1, ...payload };
}

@Get()
@Log({
mask: {
Expand Down

0 comments on commit ffc7776

Please sign in to comment.