-
Notifications
You must be signed in to change notification settings - Fork 40
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
Conversation
@@ -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)); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
const parsedData = JSON.parse(JSON.stringify(data)); | |
const parsedData = JSON.parse(JSON.stringify(data, handleCircularReferences)); |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this 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" |
There was a problem hiding this comment.
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
.
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 likenew ObjectId()