Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(logger): leave functions out of the redacted copy
Yudhi Armyndharis19819681bb50
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.tsindex d13d862b6..b5cdf0699 100644--- a/src/common/services/logger.service.spec.ts+++ b/src/common/services/logger.service.spec.ts@@ -287,6 +287,63 @@ describe('LoggerService', () => {expect(parsed().entity).toEqual({ id: 1 });});+ // A function is not data: the copy leaves it out, as JSON.stringify leaves it out of an object and writes null+ // for it in an array, so nothing a function has or returns reaches the line.+ describe('a function in the metadata, the message or the trace', () => {+ const credentials = (): Record<string, string> => ({ password: 'value-fn', other: 'value-fn-other' });+ class WithStaticToJson {+ static toJSON(): Record<string, string> {+ return credentials();+ }+ }+ const functions: Array<[string, () => unknown]> = [+ ['a function with a toJSON of its own', () => Object.assign(() => 1, { toJSON: credentials })],+ ['a class with a static toJSON', () => WithStaticToJson],+ ['a function with properties of its own', () => Object.assign(() => 1, { password: 'value-fn', id: 7 })],+ ];+ const nested = (leaf: unknown, levels: number): unknown => {+ let value = leaf;+ for (let i = 0; i < levels; i++) value = { level: value };+ return value;+ };++ it.each(functions)('is left out of the copy at every depth: %s', (_name, make) => {+ const fn = make();++ expect(JSON.stringify(redactSecrets({ handler: fn, id: 1 }))).toBe('{"id":1}');+ expect(JSON.stringify(redactSecrets([fn, 1]))).toBe('[null,1]');+ for (const depth of [0, 1, 4, 5, 6, 8]) {+ expect(JSON.stringify(redactSecrets(nested(fn, depth))) ?? '').not.toContain('value-fn');+ }+ expect(redactSecrets(fn)).toBeUndefined();+ });++ describe.each([LogFormat.JSON, LogFormat.PRETTY])('in %s output', format => {+ beforeEach(() => {+ LoggerService.setLogFormat(format);+ });++ it.each(functions)('writes nothing of %s given as metadata', (_name, make) => {+ logger.log('saved', { handler: make(), list: [make()], deep: nested(make(), 6) });++ expect(getRawOutput(consoleSpy)).toContain('saved');+ expect(getRawOutput(consoleSpy)).not.toContain('value-fn');+ });++ it.each(functions)('writes %s given as the message or as the trace as [Unprintable]', (_name, make) => {+ const errorSpy = jest.spyOn(console, 'error');++ logger.log(make());+ logger.error('failed', make());++ for (const line of [getRawOutput(consoleSpy), getRawOutput(errorSpy)]) {+ expect(line).toContain('[Unprintable]');+ expect(line).not.toContain('value-fn');+ }+ });+ });+ });+describe.each([LogFormat.JSON, LogFormat.PRETTY])('in %s output', format => {beforeEach(() => {LoggerService.setLogFormat(format);diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.tsindex 63a323041..56d347c03 100644--- a/src/common/services/logger.service.ts+++ b/src/common/services/logger.service.ts@@ -59,6 +59,8 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unknconst { isSecretKey = isSecretName, marker = '[REDACTED]', errors = false } = options;const budget = { nodes: REDACT_DEEP_NODES };const walk = (node: unknown, path: object[]): unknown => {+ // A function is not data: it is left out, as JSON.stringify leaves it out of an object and writes null in an array.+ if (typeof node === 'function') return undefined;if (node === null || typeof node !== 'object') return node;if (path.includes(node)) return '[Circular]';const deep = path.length >= REDACT_FULL_DEPTH;diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.tsindex 85b6d408b..6864d26dc 100644--- a/src/engine/adapters/baileys-logger.spec.ts+++ b/src/engine/adapters/baileys-logger.spec.ts@@ -247,6 +247,39 @@ describe('createBaileysLogger writes any record the library hands it', () => {expect(line.entity).toEqual({ id: 1 });});+ // A function is not data: it is left out of the record, so nothing it has or returns is written.+ describe('a function in the record', () => {+ class WithStaticToJson {+ static toJSON(): Record<string, string> {+ return { password: 'value-fn' };+ }+ }+ const functions: Array<[string, () => unknown]> = [+ [+ 'a function with a toJSON of its own',+ () => Object.assign(() => 1, { toJSON: () => ({ password: 'value-fn' }) }),+ ],+ ['a class with a static toJSON', () => WithStaticToJson],+ ['a function with properties of its own', () => Object.assign(() => 1, { note: 'value-fn', nested: { id: 7 } })],+ ];++ it.each(functions)('leaves out %s found in the record', (_name, make) => {+ const line = write({ id: 1, handler: make(), list: [make()], deep: nest(6, make()) }, 'x');++ expect(JSON.stringify(line)).not.toContain('value-fn');+ expect(line).toMatchObject({ id: 1, msg: 'x', list: [null] });+ expect(line).not.toHaveProperty('handler');+ });++ it.each(functions)('writes the message alone when the record is %s', (_name, make) => {+ const line = write(make(), 'x');++ expect(JSON.stringify(line)).not.toContain('value-fn');+ expect(Object.keys(line).sort()).toEqual(['context', 'level', 'msg', 'ts']);+ expect(line).toMatchObject({ level: 'debug', context: 'baileys-wire', msg: 'x' });+ });+ });+it.each([['a getter that throws',