From 3ea17320b24f000805d29f2d68db2db933b6d0b5 Mon Sep 17 00:00:00 2001 From: Salim Ben Dakhlia Date: Thu, 16 Nov 2023 16:41:51 +0100 Subject: [PATCH 1/5] fix: parse the data to json before masking the properties --- .../src/logging.interceptor.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/logging-interceptor/src/logging.interceptor.ts b/packages/logging-interceptor/src/logging.interceptor.ts index 2e1acfd0..a89936c8 100644 --- a/packages/logging-interceptor/src/logging.interceptor.ts +++ b/packages/logging-interceptor/src/logging.interceptor.ts @@ -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)); + if (this.disableMasking) { - return data; + return dataToMask; } 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(dataToMask)) { + return dataToMask.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((maskedObject: object, key: string): object => { + if (typeof dataToMask === 'object' && dataToMask !== null) { + return Object.keys(dataToMask).reduce((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((dataToMask as any)[key], maskingOptions, nestedPath), }; }, {}); } - return data; + return dataToMask; } } From e4262ff896b98ca7f948cf6b1c46172fea6e2234 Mon Sep 17 00:00:00 2001 From: Salim Ben Dakhlia Date: Thu, 16 Nov 2023 17:02:37 +0100 Subject: [PATCH 2/5] chore: add comment to explain data parsing --- packages/logging-interceptor/src/logging.interceptor.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/logging-interceptor/src/logging.interceptor.ts b/packages/logging-interceptor/src/logging.interceptor.ts index a89936c8..760908ea 100644 --- a/packages/logging-interceptor/src/logging.interceptor.ts +++ b/packages/logging-interceptor/src/logging.interceptor.ts @@ -173,6 +173,7 @@ 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)); if (this.disableMasking) { From 1c9b574cc7080e0fcc036b1de0bf1212d493d0b1 Mon Sep 17 00:00:00 2001 From: Salim Ben Dakhlia Date: Thu, 16 Nov 2023 17:23:26 +0100 Subject: [PATCH 3/5] chore: change variable name --- .../src/logging.interceptor.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/logging-interceptor/src/logging.interceptor.ts b/packages/logging-interceptor/src/logging.interceptor.ts index 760908ea..d8bbce99 100644 --- a/packages/logging-interceptor/src/logging.interceptor.ts +++ b/packages/logging-interceptor/src/logging.interceptor.ts @@ -174,33 +174,33 @@ export class LoggingInterceptor implements NestInterceptor { */ 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)); + const parsedData = JSON.parse(JSON.stringify(data)); if (this.disableMasking) { - return dataToMask; + return parsedData; } if (maskingOptions === true || maskingOptions.includes(path)) { return this.maskingPlaceholder; } - if (Array.isArray(dataToMask)) { - return dataToMask.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 dataToMask === 'object' && dataToMask !== null) { - return Object.keys(dataToMask).reduce((maskedObject: object, key: string): object => { + if (typeof parsedData === 'object' && parsedData !== null) { + return Object.keys(parsedData).reduce((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((dataToMask as any)[key], maskingOptions, nestedPath), + [key]: this.maskData((parsedData as any)[key], maskingOptions, nestedPath), }; }, {}); } - return dataToMask; + return parsedData; } } From a7a5f26272c089e8285795234110b058d520aa81 Mon Sep 17 00:00:00 2001 From: Salim Ben Dakhlia Date: Fri, 17 Nov 2023 11:11:09 +0100 Subject: [PATCH 4/5] feat: handle circular references --- package-lock.json | 9 ++++----- package.json | 3 ++- packages/logging-interceptor/src/logging.interceptor.ts | 6 ++++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9d6a134f..19483032 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,8 @@ "@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" }, "devDependencies": { "@algoan/eslint-config": "^2.0.1", @@ -8550,8 +8551,7 @@ "node_modules/flatted": { "version": "3.2.9", "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz", - "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==", - "dev": true + "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==" }, "node_modules/follow-redirects": { "version": "1.15.3", @@ -25489,8 +25489,7 @@ "flatted": { "version": "3.2.9", "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz", - "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==", - "dev": true + "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==" }, "follow-redirects": { "version": "1.15.3", diff --git a/package.json b/package.json index 700df4dd..9a691f31 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/packages/logging-interceptor/src/logging.interceptor.ts b/packages/logging-interceptor/src/logging.interceptor.ts index d8bbce99..8d07c9a3 100644 --- a/packages/logging-interceptor/src/logging.interceptor.ts +++ b/packages/logging-interceptor/src/logging.interceptor.ts @@ -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'; /** @@ -173,8 +175,8 @@ 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)); + // 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 parsedData; From 6020da3462c058c331af17f0c8fdf4b30f58a6f2 Mon Sep 17 00:00:00 2001 From: Salim Ben Dakhlia Date: Fri, 17 Nov 2023 11:42:05 +0100 Subject: [PATCH 5/5] chore: correct import --- package-lock.json | 7 ++++++- package.json | 3 +-- packages/logging-interceptor/package.json | 3 +++ packages/logging-interceptor/src/logging.interceptor.ts | 1 - 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 19483032..c42b3ddb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19105,6 +19105,9 @@ "name": "@algoan/nestjs-logging-interceptor", "version": "2.2.0", "license": "ISC", + "dependencies": { + "flatted": "^3.2.9" + }, "peerDependencies": { "@nestjs/common": ">=5", "@nestjs/core": ">=5", @@ -19177,7 +19180,9 @@ }, "@algoan/nestjs-logging-interceptor": { "version": "file:packages/logging-interceptor", - "requires": {} + "requires": { + "flatted": "^3.2.9" + } }, "@algoan/nestjs-pagination": { "version": "file:packages/pagination", diff --git a/package.json b/package.json index 9a691f31..700df4dd 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,6 @@ "@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", - "flatted": "^3.2.9" + "@algoan/nestjs-pagination": "file:packages/pagination" } } diff --git a/packages/logging-interceptor/package.json b/packages/logging-interceptor/package.json index 57834931..50f5da83 100644 --- a/packages/logging-interceptor/package.json +++ b/packages/logging-interceptor/package.json @@ -37,5 +37,8 @@ "@nestjs/core": ">=5", "express": ">=4", "rxjs": ">=6" + }, + "dependencies": { + "flatted": "^3.2.9" } } diff --git a/packages/logging-interceptor/src/logging.interceptor.ts b/packages/logging-interceptor/src/logging.interceptor.ts index 8d07c9a3..a937d9eb 100644 --- a/packages/logging-interceptor/src/logging.interceptor.ts +++ b/packages/logging-interceptor/src/logging.interceptor.ts @@ -11,7 +11,6 @@ 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';