Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(logger): take an array's reported length as a count
Yudhi Armyndharis057b672f110c
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.tsindex 846ee60ce..54371411f 100644--- a/src/common/services/logger.service.spec.ts+++ b/src/common/services/logger.service.spec.ts@@ -954,6 +954,60 @@ describe('redactSecrets', () => {expect(reads).toBe(1);});+ // The length is taken as a count the way Array.prototype.map takes it: a value that is not a number, a negative or+ // fractional one is cut to a whole number from zero, never handed on as it is.+ describe('an array that reports a length that is not a count', () => {+ const reporting = (length: unknown, items: unknown[] = []): unknown[] =>+ new Proxy(items, {+ get: (target, key, receiver): unknown => (key === 'length' ? length : Reflect.get(target, key, receiver)),+ });+ const secret = { password: 'value-length', toJSON: () => ({ password: 'value-length' }) };++ it.each([+ ['an object with a secret-named key', secret],+ ['an object with nothing', {}],+ ['an array', ['value-length']],+ ['a string that is not a number', 'value-length'],+ ['undefined', undefined],+ ['NaN', NaN],+ ['a negative number', -1],+ ['a negative infinity', -Infinity],+ ])('copies the array as empty when the length is %s, and keeps what is beside it', (_name, length) => {+ const copy = redactSecrets({ list: reporting(length), keep: 1 }) as { list: unknown[]; keep: number };++ expect(JSON.stringify(copy)).toBe('{"list":[],"keep":1}');+ expect(JSON.stringify(copy)).not.toContain('value-length');+ });++ it.each([+ ['a fractional number', 1.5, 1],+ ['a number as text', '2', 2],+ ])('cuts %s to a whole count', (_name, length, count) => {+ const copy = redactSecrets({ list: reporting(length, [10, 20, 30]) }) as { list: unknown[] };++ expect(copy.list).toEqual([10, 20, 30].slice(0, count));+ });++ it('writes an infinite length as [Truncated]', () => {+ expect(redactSecrets({ list: reporting(Infinity) })).toEqual({ list: '[Truncated]' });+ });++ it('keeps the line of a record whose array reports such a length', () => {+ const metadata = { list: reporting(-1), keep: 1 };+ const logger = new LoggerService();+ const log = jest.spyOn(console, 'log').mockImplementation();+ LoggerService.setLogFormat(LogFormat.JSON);+ try {+ logger.log('saved', metadata);++ expect(JSON.parse((log.mock.calls as string[][])[0][0])).toMatchObject({ list: [], keep: 1 });+ } finally {+ log.mockRestore();+ LoggerService.setLogFormat(null);+ }+ });+ });+describe('a typed array that claims another length than it has', () => {class Claiming extends Uint8Array {get length(): number {diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.tsindex c65641bb0..735216c43 100644--- a/src/common/services/logger.service.ts+++ b/src/common/services/logger.service.ts@@ -81,8 +81,8 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unknconst 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)) {- // Read once: the same number gates the copy and sizes it, whatever the object answers the next time.- const length = node.length;+ // Read once and taken as a count (whole, from zero), as map takes it: one number gates the copy and sizes it.+ const length = Math.max(0, Math.trunc(node.length) || 0);if (length > room) return '[Truncated]';// A plain Array filled by index, holes skipped: map would build it through the constructor of an Array subclass.const plain = new Array<unknown>(length);diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.tsindex 82b006045..fd8611b9a 100644--- a/src/engine/adapters/baileys-logger.spec.ts+++ b/src/engine/adapters/baileys-logger.spec.ts@@ -201,6 +201,19 @@ describe('createBaileysLogger writes any record the library hands it', () => {expect(write({ file, jid: 'j' }, 'x')).toMatchObject({ file: '[Truncated]', jid: 'j', msg: 'x' });});+ it('copies an array that reports a length that is not a count as empty, and keeps the rest of the record', () => {+ const reporting = (length: unknown): unknown[] =>+ new Proxy([], {+ get: (target, key, receiver): unknown => (key === 'length' ? length : Reflect.get(target, key, receiver)),+ });+ const line = JSON.stringify(+ write({ a: reporting({ password: 'value-length' }), b: reporting(-1), c: reporting(NaN), keep: 1 }, 'x'),+ );++ expect(line).not.toContain('value-length');+ expect(JSON.parse(line)).toMatchObject({ a: [], b: [], c: [], keep: 1, 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');