Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(logger): copy arrays as plain arrays when redacting

Yudhi Armyndharis6309176bf499
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index 630453517..b9d0c2c10 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -302,6 +302,23 @@ describe('LoggerService', () => {
});
});
+ describe('an array subclass', () => {
+ class Rows extends Array<unknown> {
+ toJSON(): string[] {
+ return ['replaced'];
+ }
+ }
+
+ it('is written as the plain array it holds, in the metadata and as the message', () => {
+ logger.log('saved', { rows: Rows.from([1, { token: 't' }]) });
+ logger.log(Rows.from([1, 2]));
+
+ const calls = consoleSpy.mock.calls as string[][];
+ expect(JSON.parse(calls[0][0])).toMatchObject({ rows: [1, { token: '[REDACTED]' }] });
+ expect(JSON.parse(calls[1][0])).toMatchObject({ message: '[1,2]' });
+ });
+ });
+
// Nest passes the framework's own message and trace through, so the two slots hold whatever it was handed.
describe('a message or trace that cannot be read', () => {
const revokedProxy = (): object => {
@@ -653,6 +670,15 @@ describe('redactSecrets', () => {
expect(copy.error.stack).toContain('Error: boom');
});
+ it('copies an array as a plain array, whatever class it is an instance of', () => {
+ class Rows extends Array<unknown> {}
+
+ const copy = redactSecrets({ rows: Rows.from([1, { token: 't' }]) }) as { rows: unknown[] };
+
+ expect(Object.getPrototypeOf(copy.rows)).toBe(Array.prototype);
+ expect(copy.rows).toEqual([1, { token: '[REDACTED]' }]);
+ });
+
describe('an array longer than one call may walk', () => {
const zeros = (length: number): number[] => new Array<number>(length).fill(0);
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index 1f37a78b5..5096c8ebc 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -64,8 +64,11 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unkn
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.length > (deep ? budget.nodes : REDACT_MAX_ARRAY_LENGTH) ? '[Truncated]' : node.map(copy);
+ if (Array.isArray(node)) {
+ if (node.length > (deep ? budget.nodes : REDACT_MAX_ARRAY_LENGTH)) 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]));
+ }
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 d275b607a..61bec1631 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 an array subclass as the plain array it holds', () => {
+ class Rows extends Array<unknown> {
+ toJSON(): string[] {
+ return ['replaced'];
+ }
+ }
+
+ expect(write({ rows: Rows.from([1, { password: 'p' }]) }, 'x')).toMatchObject({
+ rows: [1, { password: '[redacted]' }],
+ });
+ });
+
it('writes a sparse array of the largest length as [Truncated] at once', () => {
const list: unknown[] = [];
list.length = 2 ** 32 - 1;