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 4 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
9 changes: 4 additions & 5 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"@algoan/nestjs-google-pubsub-microservice": "file:packages/google-pubsub-microservice",
"@algoan/nestjs-http-exception-filter": "file:packages/http-exception-filter",
"@algoan/nestjs-logging-interceptor": "file:packages/logging-interceptor",
"@algoan/nestjs-pagination": "file:packages/pagination"
"@algoan/nestjs-pagination": "file:packages/pagination",
"flatted": "^3.2.9"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be installed in the packages/logging-interceptor/package.json not the global package.json.

}
}
19 changes: 12 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,8 @@ import {
import { Request, Response } from 'express';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
// eslint-disable-next-line import/no-extraneous-dependencies
import { parse, stringify } from 'flatted';
import { LogOptions, METHOD_LOG_METADATA } from './log.decorator';

/**
Expand Down Expand Up @@ -173,31 +175,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