Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(logger): do not print the own text of an unreadable message
Yudhi Armyndharis25a0a1e86f05
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.tsindex a10e404ec..c5755b112 100644--- a/src/common/services/logger.service.spec.ts+++ b/src/common/services/logger.service.spec.ts@@ -390,6 +390,48 @@ describe('LoggerService', () => {expect(getRawOutput(errorSpy)).toContain('saved-trace');});+ // The redaction never saw what such a value prints for itself, so it is not printed.+ describe('an object or function that has a text of its own but cannot be copied', () => {+ const ownText = (): object => ({+ get boom(): string {+ throw new Error('getter');+ },+ toString: () => 'password=value-text',+ });+ const functionWithText = (): (() => string) =>+ function leaky() {+ return 'password=value-text';+ };++ it.each([+ ['an object', ownText],+ ['a function', functionWithText],+ ])('writes %s passed as the message or as the trace as [Unprintable]', (_name, make) => {+ const errorSpy = jest.spyOn(console, 'error');++ logger.error(make());+ logger.error('saved-message', make());++ const lines = (errorSpy.mock.calls as string[][]).map(call => call[0]);+ expect(lines).toHaveLength(2);+ for (const line of lines) {+ expect(line).toContain('[Unprintable]');+ expect(line).not.toContain('value-text');+ }+ });+ });++ it.each([+ [42, '42'],+ [true, 'true'],+ [Symbol('s'), 'Symbol(s)'],+ [undefined, 'undefined'],+ ])('still writes the primitive %p as its own text', (value, text) => {+ logger.log(value);++ expect(getRawOutput(consoleSpy)).toContain(text);+ });+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> => {diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.tsindex 021a51367..0b07c0668 100644--- a/src/common/services/logger.service.ts+++ b/src/common/services/logger.service.ts@@ -86,16 +86,21 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unkn/** Written in place of a message or trace that cannot be read. */const UNPRINTABLE = '[Unprintable]';+/**+ * What a value that could not be turned into JSON is written as: a primitive as it prints, anything else not at all.+ * The text an object or a function gives for itself (its own toString, a function's source) never went through the+ * redaction, so it is not used.+ */+function plainText(value: unknown): string {+ return value === null || (typeof value !== 'object' && typeof value !== 'function') ? String(value) : UNPRINTABLE;+}+/** A non-string, non-Error message as one line of text (redacted like metadata, never throwing). */function stringifyMessage(message: unknown): string {try {- return JSON.stringify(redactSecrets(message)) ?? String(message);+ return JSON.stringify(redactSecrets(message)) ?? plainText(message);} catch {- try {- return String(message);- } catch {- return UNPRINTABLE;- }+ return plainText(message);}}