Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(logger): bound typed arrays and wide objects below the fifth level
Yudhi Armyndharis3e7138170a69
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.tsindex 5dbbe47d4..4cc65f331 100644--- a/src/common/services/logger.service.spec.ts+++ b/src/common/services/logger.service.spec.ts@@ -302,6 +302,31 @@ describe('LoggerService', () => {});});+ // Object.entries lists one entry per byte of a Buffer, so a few megabytes cost seconds and hundreds of megabytes.+ describe('a Buffer of 5 million bytes', () => {+ const parsed = (): Record<string, unknown> => getLogOutput(consoleSpy) as unknown as Record<string, unknown>;++ it('is written as [Truncated] at once in the first levels, with the rest of the line kept', () => {+ const file = Buffer.alloc(5_000_000);++ const started = Date.now();+ logger.log('saved', { sessionId: 's1', file });++ expect(Date.now() - started).toBeLessThan(1000);+ expect(parsed()).toMatchObject({ message: 'saved', sessionId: 's1', file: '[Truncated]' });+ });++ it('is written as [Truncated] at once below the fifth level, with the rest of the line kept', () => {+ const file = Buffer.alloc(5_000_000);++ const started = Date.now();+ logger.log('saved', { sessionId: 's1', a: { b: { c: { d: { e: { file } } } } } });++ expect(Date.now() - started).toBeLessThan(1000);+ expect(parsed()).toMatchObject({ sessionId: 's1', a: { b: { c: { d: { e: { file: '[Truncated]' } } } } } });+ });+ });+describe('an array given as the metadata itself', () => {const parsed = (): Record<string, unknown> => getLogOutput(consoleSpy) as unknown as Record<string, unknown>;@@ -801,6 +826,36 @@ describe('redactSecrets', () => {expect(redactSecrets(deep(zeros(999)))).toEqual(deep(zeros(999)));expect(redactSecrets(deep(zeros(1000)))).toEqual(deep('[Truncated]'));});++ // A typed array lists one entry per element, as an array does, and an object one per key.+ describe('a typed array or a wide object', () => {+ const deep = (list: unknown): unknown => ({ a: { b: { c: { d: { list } } } } });+ const keysOf = (copy: unknown): number =>+ Object.keys((copy as { a: { b: { c: { d: { list: object } } } } }).a.b.c.d.list).length;+ const wide = (keys: number): Record<string, number> =>+ Object.fromEntries(Array.from({ length: keys }, (_, key) => [`k${key}`, key]));++ it('copies a deep typed array that fits in what is left of the node budget and writes a longer one as [Truncated]', () => {+ expect(keysOf(redactSecrets(deep(new Uint8Array(999))))).toBe(999);+ expect(redactSecrets(deep(new Uint8Array(1000)))).toEqual(deep('[Truncated]'));+ expect(redactSecrets(deep(Buffer.alloc(1000)))).toEqual(deep('[Truncated]'));+ });++ it('copies a deep object whose keys fit in what is left of the node budget and writes a wider one as [Truncated]', () => {+ expect(keysOf(redactSecrets(deep(wide(999))))).toBe(999);+ expect(redactSecrets(deep(wide(1000)))).toEqual(deep('[Truncated]'));+ });++ it('copies a wide object whole in the first five levels', () => {+ const copy = redactSecrets({ list: wide(5000) }) as { list: object };++ expect(Object.keys(copy.list)).toHaveLength(5000);+ });++ it('writes a small typed array in the first levels as one entry per element, as before', () => {+ expect(redactSecrets({ list: Buffer.from([1, 2, 3]) })).toEqual({ list: { 0: 1, 1: 2, 2: 3 } });+ });+ });});describe('the bounds of the copy', () => {@@ -826,14 +881,30 @@ describe('redactSecrets', () => {});it('copies exactly 1000 objects of the sixth level and writes the rest as [Truncated]', () => {- const rows = rowsIn(redactSecrets(rowsBelow(4, objects(1500))), 4);+ const rows = rowsIn(+ redactSecrets(+ rowsBelow(+ 4,+ Array.from({ length: 1500 }, () => ({})),+ ),+ ),+ 4,+ );expect(rows).toHaveLength(1500);- expect(rows[999]).toEqual({ id: 999 });+ expect(rows[999]).toEqual({});expect(rows[1000]).toBe('[Truncated]');expect(rows.filter(row => row === '[Truncated]')).toHaveLength(500);});+ it('leaves the last of the 1000 nodes no room for an object that has a key', () => {+ const rows = rowsIn(redactSecrets(rowsBelow(4, objects(1500))), 4);++ expect(rows[998]).toEqual({ id: 998 });+ expect(rows[999]).toBe('[Truncated]');+ expect(rows.filter(row => row === '[Truncated]')).toHaveLength(501);+ });+it('writes an object met again inside itself as [Circular], however many objects lie between', () => {const first: Record<string, unknown> = { id: 'first' };first.second = { id: 'second', first };diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.tsindex c76e126f4..63a323041 100644--- a/src/common/services/logger.service.ts+++ b/src/common/services/logger.service.ts@@ -35,7 +35,8 @@ const REDACT_FULL_DEPTH = 5;const REDACT_DEEP_NODES = 1000;// Array.prototype.map visits every index, holes included, so a sparse array costs its length however little it// holds (2**32 - 1 takes tens of seconds). An array longer than this, or below the whole levels longer than what is-// left of the node budget, is written as [Truncated] before any index is visited.+// left of the node budget, is written as [Truncated] before any index is visited. The same goes for a typed array (a+// Buffer included), which lists one entry per element, and below the whole levels for an object, one per key.const REDACT_MAX_ARRAY_LENGTH = 1_000_000;export interface RedactOptions {@@ -64,16 +65,21 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unknif (deep && --budget.nodes < 0) return '[Truncated]';const parents = [...path, node];const copy = (child: unknown): unknown => (typeof child === 'bigint' ? child.toString() : walk(child, parents));+ const room = deep ? budget.nodes : REDACT_MAX_ARRAY_LENGTH;if (Array.isArray(node)) {- if (node.length > (deep ? budget.nodes : REDACT_MAX_ARRAY_LENGTH)) return '[Truncated]';+ if (node.length > room) return '[Truncated]';// A plain Array read by index: map would build the copy through the constructor of an Array subclass.return Array.from({ length: node.length }, (_, index) => copy(node[index]));}+ if (ArrayBuffer.isView(node) && ((node as { length?: number }).length ?? 0) > room) return '[Truncated]';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 keys = Object.keys(source);+ if (deep && keys.length > budget.nodes) return '[Truncated]';const out: Record<string, unknown> = {};- for (const [key, val] of Object.entries(source)) {+ for (const key of keys) {+ const val = source[key];// 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);diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.tsindex e8b8c7203..0723881ae 100644--- a/src/engine/adapters/baileys-logger.spec.ts+++ b/src/engine/adapters/baileys-logger.spec.ts@@ -130,6 +130,18 @@ describe('createBaileysLogger writes any record the library hands it', () => {expect(line).toContain('"password":"[redacted]","visible":"v"');});+ it('writes a Buffer of 5 million bytes as [Truncated] at once, in the first levels and below the fifth', () => {+ const file = Buffer.alloc(5_000_000);++ const started = Date.now();+ const shallow = write({ file, jid: 'j' }, 'x');+ const deep = write({ a: { b: { c: { d: { e: { file } } } } } }, 'x');++ expect(Date.now() - started).toBeLessThan(1000);+ expect(shallow).toMatchObject({ file: '[Truncated]', jid: 'j', msg: 'x' });+ expect(deep).toMatchObject({ a: { b: { c: { d: { e: { file: '[Truncated]' } } } } }, msg: 'x' });+ });+it('writes a record that is an array longer than the limit as one [Truncated] field', () => {const line = write(new Array<number>(1_000_001).fill(0), 'x');