Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): keep an error's message and stack in wire diagnostics

Yudhi Armyndharis98b9ad787904
diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.ts
index 187983763..9f831f7f8 100644
--- a/src/engine/adapters/baileys-logger.spec.ts
+++ b/src/engine/adapters/baileys-logger.spec.ts
@@ -30,4 +30,24 @@ describe('createBaileysLogger redacts credentials the library hands it', () => {
process.env.BAILEYS_LOG_LEVEL = previous;
}
});
+
+ it("keeps an error's message and stack, nested or passed alone, and still redacts its props", () => {
+ const previous = process.env.BAILEYS_LOG_LEVEL;
+ process.env.BAILEYS_LOG_LEVEL = 'debug';
+ try {
+ const logger = createBaileysLogger();
+ const err = Object.assign(new Error('Socks5 proxy rejected connection'), {
+ options: { proxy: { host: 'p', password: 'hunter2' } },
+ });
+ for (const payload of [{ err }, err]) {
+ const line = JSON.stringify(captureLine(logger.error, payload));
+ expect(line).toContain('"message":"Socks5 proxy rejected connection"');
+ expect(line).toContain('baileys-logger.spec.ts');
+ expect(line).not.toContain('hunter2');
+ expect(line).toContain('"host":"p"');
+ }
+ } finally {
+ process.env.BAILEYS_LOG_LEVEL = previous;
+ }
+ });
});
diff --git a/src/engine/adapters/baileys-logger.ts b/src/engine/adapters/baileys-logger.ts
index d1276ac6b..c9682692f 100644
--- a/src/engine/adapters/baileys-logger.ts
+++ b/src/engine/adapters/baileys-logger.ts
@@ -25,11 +25,16 @@ const SECRET_KEYS: ReadonlySet<string> = new Set(['password', 'secret', 'token',
* 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.
+ * 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.
*/
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,
@@ -59,9 +64,10 @@ export function createBaileysLogger(): BaileysLogger {
if (BAILEYS_LOG_LEVELS.indexOf(lvl) < threshold) {
return;
}
- const rec = redactSecrets(
- typeof obj === 'string' ? { msg: obj } : { ...(obj as Record<string, unknown>), ...(msg ? { msg } : {}) },
- ) as Record<string, unknown>;
+ 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',
);