Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(logger): truncate an array too long to walk

Yudhi Armyndharisacd99af68388
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index ee6578384..c6703e0d8 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -151,11 +151,10 @@ describe('LoggerService', () => {
expect(items[1499]).toEqual({ id: 1499, tags: [1499] });
});
- it('truncates a wide structure that sits deep in the metadata after the bound', () => {
- logger.log('cfg', { a: { b: { c: { d: { items: Array.from({ length: 1500 }, (_, id) => ({ id })) } } } } });
+ it('truncates the rows of a wide array below the last whole level once the budget is spent', () => {
+ logger.log('cfg', { a: { b: { c: { items: Array.from({ length: 1500 }, (_, id) => ({ id })) } } } });
- const { items } = (getLogOutput(consoleSpy) as unknown as { a: { b: { c: { d: { items: unknown[] } } } } }).a.b.c
- .d;
+ const { items } = (getLogOutput(consoleSpy) as unknown as { a: { b: { c: { items: unknown[] } } } }).a.b.c;
expect(items).toHaveLength(1500);
expect(items[0]).toEqual({ id: 0 });
expect(items[1499]).toBe('[Truncated]');
@@ -272,6 +271,37 @@ describe('LoggerService', () => {
});
});
+ // Array.prototype.map visits every index of an array, holes included, so a sparse array costs its length
+ // however little it holds: the largest one takes tens of seconds to walk.
+ describe('a sparse array of the largest length', () => {
+ const sparse = (): unknown[] => {
+ const list: unknown[] = [];
+ list.length = 2 ** 32 - 1;
+ return list;
+ };
+ 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 started = Date.now();
+ logger.log('saved', { sessionId: 's1', list: sparse() });
+
+ expect(Date.now() - started).toBeLessThan(1000);
+ expect(parsed()).toMatchObject({ message: 'saved', sessionId: 's1', list: '[Truncated]' });
+ });
+
+ it('is written as [Truncated] at once below the fifth level, with the rest of the line kept', () => {
+ const started = Date.now();
+ logger.log('saved', { sessionId: 's1', a: { b: { c: { d: { e: { list: sparse() } } } } } });
+
+ expect(Date.now() - started).toBeLessThan(1000);
+ expect(parsed()).toMatchObject({
+ message: 'saved',
+ sessionId: 's1',
+ a: { b: { c: { d: { e: { list: '[Truncated]' } } } } },
+ });
+ });
+ });
+
describe('error', () => {
it('should log error messages to console.error', () => {
const errorSpy = jest.spyOn(console, 'error');
@@ -549,4 +579,23 @@ describe('redactSecrets', () => {
expect(copy.error).toMatchObject({ name: 'Error', message: 'boom', token: '[REDACTED]', attempts: '5' });
expect(copy.error.stack).toContain('Error: boom');
});
+
+ describe('an array longer than one call may walk', () => {
+ const zeros = (length: number): number[] => new Array<number>(length).fill(0);
+
+ it('copies a million entries in the first levels and writes a longer array as [Truncated]', () => {
+ const whole = redactSecrets({ list: zeros(1_000_000) }) as { list: unknown[] };
+
+ expect(whole.list).toHaveLength(1_000_000);
+ expect(redactSecrets({ list: zeros(1_000_001) })).toEqual({ list: '[Truncated]' });
+ });
+
+ it('copies a deep array that fits in what is left of the node budget and writes a longer one as [Truncated]', () => {
+ // The array sits at the fifth level: it costs 1 of the 1000 nodes, which leaves 999 entries.
+ const deep = (list: unknown): unknown => ({ a: { b: { c: { d: { list } } } } });
+
+ expect(redactSecrets(deep(zeros(999)))).toEqual(deep(zeros(999)));
+ expect(redactSecrets(deep(zeros(1000)))).toEqual(deep('[Truncated]'));
+ });
+ });
});
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index 2f5f584b2..7497ccb0a 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -28,11 +28,15 @@ export interface LogContext {
// (case-insensitive), never the value's content, to avoid false positives on innocuous fields.
const SECRET_KEY_PATTERN = /password|passwd|secret|token|api[-_]?key|authorization|credential|pepper|private[-_]?key/i;
-// Metadata is copied whole down to this depth, which is where it normally lives. Below it one log call copies at
-// most REDACT_DEEP_NODES more objects and arrays and writes the rest as [Truncated], never raw, so a densely
-// linked object graph cannot make a single log call walk it for long.
+// Metadata is copied whole down to this depth, however wide or dense, which is where it normally lives; that part is
+// not bounded. Below it one log call copies at most REDACT_DEEP_NODES more objects and arrays and writes the rest as
+// [Truncated], never raw, so a densely linked object graph cannot make it walk that part for long.
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.
+const REDACT_MAX_ARRAY_LENGTH = 1_000_000;
export interface RedactOptions {
/** Whether the value under this key is a credential. Defaults to the secret-name pattern above. */
@@ -56,10 +60,12 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unkn
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 deep = path.length >= REDACT_FULL_DEPTH;
+ if (deep && --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);
+ if (Array.isArray(node))
+ return node.length > (deep ? budget.nodes : REDACT_MAX_ARRAY_LENGTH) ? '[Truncated]' : 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;
diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.ts
index e47c3b21a..d275b607a 100644
--- a/src/engine/adapters/baileys-logger.spec.ts
+++ b/src/engine/adapters/baileys-logger.spec.ts
@@ -130,6 +130,17 @@ describe('createBaileysLogger writes any record the library hands it', () => {
expect(line).toContain('"password":"[redacted]","visible":"v"');
});
+ it('writes a sparse array of the largest length as [Truncated] at once', () => {
+ const list: unknown[] = [];
+ list.length = 2 ** 32 - 1;
+
+ const started = Date.now();
+ const line = write({ list, jid: 'j' }, 'x');
+
+ expect(Date.now() - started).toBeLessThan(1000);
+ expect(line).toMatchObject({ list: '[Truncated]', jid: 'j', msg: 'x' });
+ });
+
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');