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
Changes from 3 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
17 changes: 10 additions & 7 deletions packages/logging-interceptor/src/logging.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,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
const parsedData = JSON.parse(JSON.stringify(data));
Copy link
Member

Choose a reason for hiding this comment

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

I think, it should handle circular references in the stringify.

Suggested change
const parsedData = JSON.parse(JSON.stringify(data));
const parsedData = JSON.parse(JSON.stringify(data, handleCircularReferences));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this an option in stringify ?

Copy link
Member

@g-ongenae g-ongenae Nov 17, 2023

Choose a reason for hiding this comment

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

You can pass a replacer in JSON.stringify to detect circular references and replace them by reference ID to prevent from having an infinite loop during the stringify.

Or maybe a good alternative is to use util.inspect(myObject)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used a package that deals with the handling of circular references


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