Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(logger): redact an Error trace whose stack is not text
Yudhi Armyndharisdc4cf2e46c8c
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.tsindex b9d0c2c10..2a963a3c2 100644--- a/src/common/services/logger.service.spec.ts+++ b/src/common/services/logger.service.spec.ts@@ -389,6 +389,36 @@ describe('LoggerService', () => {expect(getRawOutput(errorSpy)).toContain('[Unprintable]');expect(getRawOutput(errorSpy)).toContain('saved-trace');});++ describe('an Error passed as the trace whose stack is not text', () => {+ const withStack = (stack: unknown): Error => Object.assign(new Error('boom'), { stack });+ const cyclic = (): Record<string, unknown> => {+ const value: Record<string, unknown> = { id: 1 };+ value.self = value;+ return value;+ };++ it.each([+ ['an object', () => withStack({ password: 'value-stack', frame: 'f1' }), '"password":"[REDACTED]"'],+ ['a list of call sites', () => withStack([{ token: 'value-stack' }]), '"token":"[REDACTED]"'],+ ['a cyclic object', () => withStack(cyclic()), '[Circular]'],+ ['a BigInt', () => withStack(98765432109876543210n), '98765432109876543210'],+ [+ 'nothing, with an object as the message',+ () => Object.assign(new Error('x'), { stack: undefined, message: { password: 'value-stack' } }),+ '"password":"[REDACTED]"',+ ],+ ])('writes %s as redacted text and keeps the message', (_name, make, written) => {+ const errorSpy = jest.spyOn(console, 'error');++ expect(() => logger.error('saved-message', make())).not.toThrow();++ const line = getRawOutput(errorSpy);+ expect(line).toContain('saved-message');+ expect(line).not.toContain('value-stack');+ expect(line.replace(/\\"/g, '"')).toContain(written);+ });+ });});});diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.tsindex 5096c8ebc..021a51367 100644--- a/src/common/services/logger.service.ts+++ b/src/common/services/logger.service.ts@@ -194,12 +194,9 @@ export class LoggerService implements NestLoggerService {}let trace: string | undefined;try {- trace =- rawTrace === undefined || typeof rawTrace === 'string'- ? rawTrace- : rawTrace instanceof Error- ? (rawTrace.stack ?? rawTrace.message)- : stringifyMessage(rawTrace);+ // An Error's stack is text unless something replaced it; anything that is not text is redacted like metadata.+ const text = rawTrace instanceof Error ? (rawTrace.stack ?? rawTrace.message) : rawTrace;+ trace = text === undefined || typeof text === 'string' ? text : stringifyMessage(text);} catch {trace = UNPRINTABLE;}