Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(logger): list a too wide object once below the fifth level
Yudhi Armyndharisfdfc18ef8431
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.tsindex b5cdf0699..1dc4163a3 100644--- a/src/common/services/logger.service.spec.ts+++ b/src/common/services/logger.service.spec.ts@@ -938,6 +938,44 @@ describe('redactSecrets', () => {expect(redactSecrets(deep(wide(1000)))).toEqual(deep('[Truncated]'));});+ // An object is listed (Object.keys) before its key count can be compared with the budget, so a too wide one is+ // charged for its keys once it is truncated: a second reference to it is refused without listing it again.+ it('lists a too wide object once, however many references to it lie below the fifth level', () => {+ let listings = 0;+ const counted = new Proxy(wide(10_000), {+ ownKeys: target => {+ listings++;+ return Reflect.ownKeys(target);+ },+ });++ const copy = redactSecrets(deep(Array.from({ length: 900 }, () => counted))) as {+ a: { b: { c: { d: { list: unknown[] } } } };+ };++ expect(listings).toBe(1);+ expect(copy.a.b.c.d.list).toHaveLength(900);+ expect(new Set(copy.a.b.c.d.list)).toEqual(new Set(['[Truncated]']));+ });++ it('finishes a record of 900 references to one wide object in a bounded time', () => {+ const shared = wide(20_000);+ const started = Date.now();++ redactSecrets(deep(Array.from({ length: 900 }, () => shared)));++ expect(Date.now() - started).toBeLessThan(500);+ });++ it('spends the node budget on the keys of a too wide object, so what follows it is [Truncated]', () => {+ expect(redactSecrets(deep({ wide: wide(1500), small: { id: 1 } }))).toEqual(+ deep({ wide: '[Truncated]', small: '[Truncated]' }),+ );+ expect(redactSecrets(deep({ small: { id: 1 }, wide: wide(1500) }))).toEqual(+ deep({ small: { id: 1 }, wide: '[Truncated]' }),+ );+ });+it('copies a wide object whole in the first five levels', () => {const copy = redactSecrets({ list: wide(5000) }) as { list: object };diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.tsindex 56d347c03..a5e92f77c 100644--- a/src/common/services/logger.service.ts+++ b/src/common/services/logger.service.ts@@ -36,7 +36,8 @@ 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. 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.+// Buffer included), which lists one entry per element. Below the whole levels an object is listed, then truncated if+// its keys do not fit in what is left of the budget, which they are then charged to.const REDACT_MAX_ARRAY_LENGTH = 1_000_000;export interface RedactOptions {@@ -78,7 +79,11 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unknconst 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]';+ if (deep && keys.length > budget.nodes) {+ // Listed once, then charged: another reference to this object, or any other node, is refused without listing.+ budget.nodes -= keys.length;+ return '[Truncated]';+ }const out: Record<string, unknown> = {};for (const key of keys) {const val = source[key];diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.tsindex 6864d26dc..b85ff1d21 100644--- a/src/engine/adapters/baileys-logger.spec.ts+++ b/src/engine/adapters/baileys-logger.spec.ts@@ -157,6 +157,23 @@ describe('createBaileysLogger writes any record the library hands it', () => {expect(Object.keys(fits)).toHaveLength(999);expect(wider).toBe('[Truncated]');});++ it('writes 900 references to one wide object below the fifth level as [Truncated] in a bounded time', () => {+ const shared = wide(20_000);++ const started = Date.now();+ const rows = at(+ write({ a: { b: { c: { d: { rows: Array.from({ length: 900 }, () => shared) } } } } }, 'x'),+ 'a',+ 'b',+ 'c',+ 'd',+ 'rows',+ ) as unknown[];++ expect(Date.now() - started).toBeLessThan(500);+ expect(new Set(rows)).toEqual(new Set(['[Truncated]']));+ });});it('replaces a credential nested deeper than five levels', () => {