Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): redact Baileys wire logs at any depth

Yudhi Armyndharis4075f04d084d
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index 1a40e0b51..ee6578384 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -1,7 +1,7 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { ConsoleLogger, Logger } from '@nestjs/common';
-import { LoggerService, LogLevel, LogFormat, createLogger } from './logger.service';
+import { LoggerService, LogLevel, LogFormat, createLogger, redactSecrets } from './logger.service';
import { runWithRequestId } from './request-context';
interface LogEntry {
@@ -525,3 +525,28 @@ describe('LoggerService', () => {
});
});
});
+
+// The Baileys wire logger reuses this copy with its own key policy and marker, so the options are pinned directly.
+describe('redactSecrets', () => {
+ it('replaces the value under a secret-named key with [REDACTED] unless told otherwise', () => {
+ expect(redactSecrets({ accessToken: 't', visible: 'v' })).toEqual({ accessToken: '[REDACTED]', visible: 'v' });
+ });
+
+ it('takes the credential keys and the marker from its options', () => {
+ const options = { isSecretKey: (key: string) => key === 'password', marker: '[masked]' };
+
+ expect(redactSecrets({ accessToken: 't', password: 'p' }, options)).toEqual({
+ accessToken: 't',
+ password: '[masked]',
+ });
+ });
+
+ it('copies an Error with its name, message and stack, and its own props through the same checks, when asked to', () => {
+ const error = Object.assign(new Error('boom'), { token: 't', attempts: 5n });
+
+ const copy = redactSecrets({ error }, { errors: true }) as { error: Record<string, unknown> };
+
+ expect(copy.error).toMatchObject({ name: 'Error', message: 'boom', token: '[REDACTED]', attempts: '5' });
+ expect(copy.error.stack).toContain('Error: boom');
+ });
+});
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index 56c5090bf..2f5f584b2 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -34,26 +34,44 @@ const SECRET_KEY_PATTERN = /password|passwd|secret|token|api[-_]?key|authorizati
const REDACT_FULL_DEPTH = 5;
const REDACT_DEEP_NODES = 1000;
+export interface RedactOptions {
+ /** Whether the value under this key is a credential. Defaults to the secret-name pattern above. */
+ isSecretKey?: (key: string) => boolean;
+ /** Written in place of a credential's value. */
+ marker?: string;
+ /** Copy an Error with its name, message and stack too, which a copy of its own properties alone would lose. */
+ errors?: boolean;
+}
+
+const isSecretName = (key: string): boolean => SECRET_KEY_PATTERN.test(key);
+
/**
- * Return a copy of `value` with secret-named keys' values replaced by `[REDACTED]` (recursive). The copy always
+ * Return a copy of `value` with credential keys' values replaced by `[REDACTED]` (recursive). The copy always
* serializes: an object met again inside itself becomes `[Circular]`, a nested BigInt its decimal text, and
- * whatever lies beyond the deep node budget `[Truncated]`.
+ * whatever lies beyond the deep node budget `[Truncated]`. Shared with the Baileys wire logger.
*/
-function redactSecrets(value: unknown, path: object[] = [], budget = { nodes: REDACT_DEEP_NODES }): unknown {
- if (value === null || typeof value !== 'object') return value;
- if (path.includes(value)) return '[Circular]';
- if (path.length >= REDACT_FULL_DEPTH && --budget.nodes < 0) return '[Truncated]';
- const parents = [...path, value];
- const copy = (child: unknown): unknown =>
- typeof child === 'bigint' ? child.toString() : redactSecrets(child, parents, budget);
- if (Array.isArray(value)) return value.map(copy);
- const out: Record<string, unknown> = {};
- for (const [key, val] of Object.entries(value as Record<string, unknown>)) {
- // JSON.stringify would call an own toJSON on the copy and write whatever it returns, redacted or not.
- if (key === 'toJSON' && typeof val === 'function') continue;
- out[key] = SECRET_KEY_PATTERN.test(key) ? '[REDACTED]' : copy(val);
- }
- return out;
+export function redactSecrets(value: unknown, options: RedactOptions = {}): unknown {
+ const { isSecretKey = isSecretName, marker = '[REDACTED]', errors = false } = options;
+ const budget = { nodes: REDACT_DEEP_NODES };
+ const walk = (node: unknown, path: object[]): unknown => {
+ if (node === null || typeof node !== 'object') return node;
+ if (path.includes(node)) return '[Circular]';
+ if (path.length >= REDACT_FULL_DEPTH && --budget.nodes < 0) return '[Truncated]';
+ const parents = [...path, node];
+ const copy = (child: unknown): unknown => (typeof child === 'bigint' ? child.toString() : walk(child, parents));
+ if (Array.isArray(node)) return node.map(copy);
+ const props = node as Record<string, unknown>;
+ const source =
+ errors && node instanceof Error ? { name: node.name, message: node.message, stack: node.stack, ...props } : props;
+ const out: Record<string, unknown> = {};
+ for (const [key, val] of Object.entries(source)) {
+ // JSON.stringify would call an own toJSON on the copy and write whatever it returns, redacted or not.
+ if (key === 'toJSON' && typeof val === 'function') continue;
+ out[key] = isSecretKey(key) ? marker : copy(val);
+ }
+ return out;
+ };
+ return walk(value, []);
}
/** A non-string, non-Error message as one line of text (redacted like metadata, never throwing). */
diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.ts
index 9f831f7f8..e47c3b21a 100644
--- a/src/engine/adapters/baileys-logger.spec.ts
+++ b/src/engine/adapters/baileys-logger.spec.ts
@@ -1,19 +1,24 @@
import { createBaileysLogger } from './baileys-logger';
-describe('createBaileysLogger redacts credentials the library hands it', () => {
- const captureLine = (write: (obj: unknown, msg?: string) => void, payload: unknown): Record<string, unknown> => {
- const written: string[] = [];
- const original = process.stdout.write.bind(process.stdout);
- (process.stdout as { write: unknown }).write = (chunk: string): boolean => {
- written.push(chunk);
- return true;
- };
- try {
- write(payload);
- } finally {
- (process.stdout as { write: unknown }).write = original;
- }
- return JSON.parse(written.join('')) as Record<string, unknown>;
+
+// Everything the logger writes to stdout while `emit` runs.
+const captureOutput = (emit: () => void): string => {
+ const written: string[] = [];
+ const original = process.stdout.write.bind(process.stdout);
+ (process.stdout as { write: unknown }).write = (chunk: string): boolean => {
+ written.push(chunk);
+ return true;
};
+ try {
+ emit();
+ } finally {
+ (process.stdout as { write: unknown }).write = original;
+ }
+ return written.join('');
+};
+
+describe('createBaileysLogger redacts credentials the library hands it', () => {
+ const captureLine = (write: (obj: unknown, msg?: string) => void, payload: unknown): Record<string, unknown> =>
+ JSON.parse(captureOutput(() => write(payload))) as Record<string, unknown>;
it('replaces a proxy password carried by a socks connect error', () => {
const previous = process.env.BAILEYS_LOG_LEVEL;
@@ -51,3 +56,143 @@ describe('createBaileysLogger redacts credentials the library hands it', () => {
}
});
});
+
+// The library hands this logger whatever object a failure carried, so the record is not ours to shape.
+describe('createBaileysLogger writes any record the library hands it', () => {
+ let previousLevel: string | undefined;
+ beforeEach(() => {
+ previousLevel = process.env.BAILEYS_LOG_LEVEL;
+ process.env.BAILEYS_LOG_LEVEL = 'debug';
+ });
+ afterEach(() => {
+ if (previousLevel === undefined) delete process.env.BAILEYS_LOG_LEVEL;
+ else process.env.BAILEYS_LOG_LEVEL = previousLevel;
+ });
+
+ const write = (record: unknown, msg?: string): Record<string, unknown> =>
+ JSON.parse(captureOutput(() => createBaileysLogger().debug(record, msg))) as Record<string, unknown>;
+ const nest = (levels: number, leaf: unknown): unknown => {
+ let value = leaf;
+ for (let i = 0; i < levels; i++) value = { level: value };
+ return value;
+ };
+ const withCycle = (): Record<string, unknown> => {
+ const socket: Record<string, unknown> = { id: 1 };
+ socket.self = socket;
+ return socket;
+ };
+
+ it.each(['password', 'secret', 'token', 'apikey', 'api_key', 'authorization', 'Authorization', 'API_KEY'])(
+ 'replaces the value under %s',
+ key => {
+ expect(write({ [key]: 'value-1', visible: 'v' }, 'x')).toMatchObject({ [key]: '[redacted]', visible: 'v' });
+ },
+ );
+
+ it('writes an ordinary record exactly as given, apart from its credentials', () => {
+ const record = {
+ attempt: 2,
+ tokenCount: 3, // names a credential word but is not one: only the exact names above are replaced
+ list: [1, 'two', { three: 3 }],
+ nested: { ok: true, none: null },
+ };
+
+ const { ts, ...line } = write({ ...record, token: 't' }, 'retrying');
+
+ expect(ts).toEqual(expect.any(String));
+ expect(JSON.stringify(line)).toBe(
+ JSON.stringify({ level: 'debug', context: 'baileys-wire', ...record, token: '[redacted]', msg: 'retrying' }),
+ );
+ });
+
+ it('writes a plain string as the message', () => {
+ expect(write('connection opened')).toMatchObject({
+ level: 'debug',
+ context: 'baileys-wire',
+ msg: 'connection opened',
+ });
+ });
+
+ it('writes a wide record in full at the levels records normally have', () => {
+ const { items } = write({ items: Array.from({ length: 1500 }, (_, id) => ({ id, tags: [id] })) }, 'x') as {
+ items: unknown[];
+ };
+
+ expect(items).toHaveLength(1500);
+ expect(items[1499]).toEqual({ id: 1499, tags: [1499] });
+ });
+
+ it('replaces a credential nested deeper than five levels', () => {
+ const line = JSON.stringify(write({ record: nest(8, { password: 'value-deep', visible: 'v' }) }, 'x'));
+
+ expect(line).not.toContain('value-deep');
+ expect(line).toContain('"password":"[redacted]","visible":"v"');
+ });
+
+ it('writes what lies beyond the bounded amount of deep data as [Truncated], never raw', () => {
+ const line = write({ record: nest(1500, { password: 'value-far' }), jid: 'j' }, 'x');
+
+ expect(JSON.stringify(line)).not.toContain('value-far');
+ expect(JSON.stringify(line)).toContain('"[Truncated]"');
+ expect(line.jid).toBe('j');
+ });
+
+ it.each([
+ ['a first-level', 1],
+ ['a deep', 7],
+ ])('writes %s circular reference as [Circular] and keeps the message', (_name, levels) => {
+ const line = write({ record: nest(levels, withCycle()) }, 'socket closed');
+
+ expect(line.msg).toBe('socket closed');
+ expect(JSON.stringify(line)).toContain('"self":"[Circular]"');
+ });
+
+ it('writes a BigInt as its decimal text', () => {
+ expect(write({ total: 10n, nested: { ids: [1n, 2n] } }, 'x')).toMatchObject({
+ total: '10',
+ nested: { ids: ['1', '2'] },
+ });
+ });
+
+ it('never calls a toJSON on the record, so it cannot write what the redaction removed', () => {
+ const line = write({ entity: { id: 1, toJSON: () => ({ password: 'value-json' }) } }, 'x');
+
+ expect(JSON.stringify(line)).not.toContain('value-json');
+ expect(line.entity).toEqual({ id: 1 });
+ });
+
+ it.each([
+ [
+ 'a getter that throws',
+ {
+ get boom(): string {
+ throw new Error('getter');
+ },
+ },
+ ],
+ [
+ 'a proxy whose traps throw',
+ new Proxy(
+ {},
+ {
+ ownKeys: (): never => {
+ throw new Error('ownKeys');
+ },
+ },
+ ),
+ ],
+ ])('writes the message without the record when it holds %s', (_name, record) => {
+ expect(write(record, 'connection update')).toMatchObject({ msg: 'connection update', metadata: '[Unreadable]' });
+ });
+
+ it("keeps an error's message and stack when its own props hold a cycle or a BigInt", () => {
+ const err = Object.assign(new Error('stream errored'), { attempts: 5n });
+ Object.assign(err, { self: err });
+
+ const line = write({ err }, 'x') as { err: Record<string, unknown> };
+
+ expect(line.err).toMatchObject({ name: 'Error', message: 'stream errored', attempts: '5', self: '[Circular]' });
+ expect(line.err.stack).toContain('baileys-logger.spec.ts');
+ });
+});
diff --git a/src/engine/adapters/baileys-logger.ts b/src/engine/adapters/baileys-logger.ts
index c9682692f..1b5028055 100644
--- a/src/engine/adapters/baileys-logger.ts
+++ b/src/engine/adapters/baileys-logger.ts
@@ -1,3 +1,4 @@
+import { redactSecrets, type RedactOptions } from '../../common/services/logger.service';
import { BaileysLogger } from '../types/baileys.types';
/** Fully silent logger so Baileys does not spam stdout; diagnostics flow via connection.update. */
@@ -19,29 +20,20 @@ export function createSilentLogger(): BaileysLogger {
const SECRET_KEYS: ReadonlySet<string> = new Set(['password', 'secret', 'token', 'apikey', 'api_key', 'authorization']);
/**
- * Replace credential-valued keys before a library record is serialised.
+ * How a library record is redacted before it is serialised.
*
* Baileys hands this logger whatever object the failure carried, and a `socks` connect error carries
* the whole proxy config, password included, as its only enumerable property. At debug level that
* went to stdout verbatim, which on a shipped deployment means the proxy password in the container
- * log. Bounded depth: these records nest a couple of levels at most, and a logger must not walk an
- * arbitrary graph on the wire path. An Error's message and stack are not enumerable, so they are copied
- * explicitly: they are what an operator raised the log level to see.
+ * log. An Error's message and stack are not enumerable, so they are copied explicitly: they are what
+ * an operator raised the log level to see. Below the fifth level the copy is bounded (see
+ * redactSecrets), because a logger must not walk an arbitrary graph on the wire path.
*/
-function redactSecrets(value: unknown, depth = 0): unknown {
- if (depth > 4 || value === null || typeof value !== 'object') return value;
- if (Array.isArray(value)) return value.map(v => redactSecrets(v, depth + 1));
- if (value instanceof Error) {
- const own = redactSecrets({ ...value }, depth) as Record<string, unknown>;
- return { name: value.name, message: value.message, stack: value.stack, ...own };
- }
- return Object.fromEntries(
- Object.entries(value as Record<string, unknown>).map(([k, v]) => [
- k,
- SECRET_KEYS.has(k.toLowerCase()) ? '[redacted]' : redactSecrets(v, depth + 1),
- ]),
- );
-}
+const REDACTION: RedactOptions = {
+ isSecretKey: key => SECRET_KEYS.has(key.toLowerCase()),
+ marker: '[redacted]',
+ errors: true,
+};
const BAILEYS_LOG_LEVELS = ['trace', 'debug', 'info', 'warn', 'error'];
@@ -64,13 +56,19 @@ export function createBaileysLogger(): BaileysLogger {
if (BAILEYS_LOG_LEVELS.indexOf(lvl) < threshold) {
return;
}
- const rec =
- typeof obj === 'string'
- ? { msg: obj }
- : { ...(redactSecrets(obj) as Record<string, unknown>), ...(msg ? { msg } : {}) };
- process.stdout.write(
- JSON.stringify({ ts: new Date().toISOString(), level: lvl, context: 'baileys-wire', ...rec }) + '\n',
- );
+ const head = { ts: new Date().toISOString(), level: lvl, context: 'baileys-wire' };
+ let line: string;
+ try {
+ const rec =
+ typeof obj === 'string'
+ ? { msg: obj }
+ : { ...(redactSecrets(obj, REDACTION) as Record<string, unknown>), ...(msg ? { msg } : {}) };
+ line = JSON.stringify({ ...head, ...rec });
+ } catch {
+ // A getter or proxy trap in the record threw. This runs inside Baileys and must not throw: write the message alone.
+ line = JSON.stringify({ ...head, ...(msg ? { msg } : {}), metadata: '[Unreadable]' });
+ }
+ process.stdout.write(line + '\n');
};
const logger: BaileysLogger = {
level: configured,