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

fix: parse the data to json before masking the properties #811

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/logging-interceptor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@
"@nestjs/core": ">=5",
"express": ">=4",
"rxjs": ">=6"
},
"dependencies": {
"flatted": "^3.2.9"
}
}
18 changes: 11 additions & 7 deletions packages/logging-interceptor/src/logging.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Request, Response } from 'express';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { parse, stringify } from 'flatted';
import { LogOptions, METHOD_LOG_METADATA } from './log.decorator';

/**
Expand Down Expand Up @@ -173,31 +174,34 @@ export class LoggingInterceptor implements NestInterceptor {
* @returns the masked data
*/
private maskData(data: unknown, maskingOptions: string[] | true, path: string = ''): unknown {
// Parse the data to avoid having constructors like new ObjectId() in the body and handle circular references
const parsedData = parse(stringify(data));

if (this.disableMasking) {
return data;
return parsedData;
}

if (maskingOptions === true || maskingOptions.includes(path)) {
return this.maskingPlaceholder;
}

if (Array.isArray(data)) {
return data.map((item: unknown): unknown => this.maskData(item, maskingOptions, path));
if (Array.isArray(parsedData)) {
return parsedData.map((item: unknown): unknown => this.maskData(item, maskingOptions, path));
}

// eslint-disable-next-line no-null/no-null
if (typeof data === 'object' && data !== null) {
return Object.keys(data).reduce<object>((maskedObject: object, key: string): object => {
if (typeof parsedData === 'object' && parsedData !== null) {
return Object.keys(parsedData).reduce<object>((maskedObject: object, key: string): object => {
const nestedPath = path ? `${path}.${key}` : key;

return {
...maskedObject,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key]: this.maskData((data as any)[key], maskingOptions, nestedPath),
[key]: this.maskData((parsedData as any)[key], maskingOptions, nestedPath),
};
}, {});
}

return data;
return parsedData;
}
}
Loading