diff --git a/package-lock.json b/package-lock.json index 9d6a134f..c42b3ddb 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", @@ -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", @@ -25489,8 +25494,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/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 2e1acfd0..a937d9eb 100644 --- a/packages/logging-interceptor/src/logging.interceptor.ts +++ b/packages/logging-interceptor/src/logging.interceptor.ts @@ -11,6 +11,7 @@ import { import { Request, Response } from 'express'; import { Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; +import { parse, stringify } from 'flatted'; import { LogOptions, METHOD_LOG_METADATA } from './log.decorator'; /** @@ -173,31 +174,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((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((data as any)[key], maskingOptions, nestedPath), + [key]: this.maskData((parsedData as any)[key], maskingOptions, nestedPath), }; }, {}); } - return data; + return parsedData; } }