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

Conversation

Bendakh
Copy link
Contributor

@Bendakh Bendakh commented Nov 16, 2023

Description

Through this PR we parsed the body of the data to anonymise in the maskData data function to make sure that the recursive function is called on a formatted body without constructors like new ObjectId()

@@ -173,31 +173,33 @@ export class LoggingInterceptor implements NestInterceptor {
* @returns the masked data
*/
private maskData(data: unknown, maskingOptions: string[] | true, path: string = ''): unknown {
const dataToMask = JSON.parse(JSON.stringify(data));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add a comment to say why we did this so it is clearer in the code ?

@@ -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 dataToMask = JSON.parse(JSON.stringify(data));
Copy link
Contributor

Choose a reason for hiding this comment

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

parsedData would be more accurate.

@@ -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

@Bendakh Bendakh requested a review from g-ongenae November 17, 2023 10:12
Copy link
Member

@g-ongenae g-ongenae left a comment

Choose a reason for hiding this comment

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

Except for the module wrong package installation, LGTM.

package.json Outdated
@@ -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.

@Bendakh Bendakh requested a review from g-ongenae November 17, 2023 10:46
@g-ongenae g-ongenae merged commit 403b256 into algoan:master Nov 17, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants