Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(logger): size a copy from the elements an object has

Yudhi Armyndharisb35cdb1c175c
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index 1dc4163a3..8038d372a 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -919,6 +919,45 @@ describe('redactSecrets', () => {
expect(redactSecrets(deep(zeros(1000)))).toEqual(deep('[Truncated]'));
});
+ // The caps are applied to the length an array or a typed array has, not to the length an object claims.
+ it('reads the length of an array once, for the cap and for the copy', () => {
+ let reads = 0;
+ const answering = new Proxy([], {
+ get: (target, key, receiver): unknown =>
+ key === 'length' ? (++reads === 1 ? 0 : 3) : Reflect.get(target, key, receiver),
+ });
+
+ expect(redactSecrets({ list: answering })).toEqual({ list: [] });
+ expect(reads).toBe(1);
+ });
+
+ describe('a typed array that claims another length than it has', () => {
+ class Claiming extends Uint8Array {
+ get length(): number {
+ return 0;
+ }
+ }
+ const redefined = (): Uint8Array => Object.defineProperty(new Uint8Array(1_000_001), 'length', { value: 0 });
+
+ it.each([
+ ['an own length property', redefined],
+ ['a length getter', (): Uint8Array => new Claiming(1_000_001)],
+ ])('is written as [Truncated] by the elements it has, with %s', (_name, make) => {
+ expect(redactSecrets({ file: make() })).toEqual({ file: '[Truncated]' });
+ expect(redactSecrets(make())).toBe('[Truncated]');
+ });
+
+ it('counts the elements of a typed array, not its bytes', () => {
+ const copy = redactSecrets({ list: new Float64Array(150_000) }) as { list: object };
+
+ expect(Object.keys(copy.list)).toHaveLength(150_000);
+ });
+
+ it('copies a DataView, which has no elements, as an empty object', () => {
+ expect(redactSecrets({ view: new DataView(new ArrayBuffer(8)) })).toEqual({ view: {} });
+ });
+ });
+
// 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 } } } } });
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index a5e92f77c..1adcba9a1 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -51,6 +51,17 @@ export interface RedactOptions {
const isSecretName = (key: string): boolean => SECRET_KEY_PATTERN.test(key);
+const TYPED_ARRAY_PROTOTYPE: object = Object.getPrototypeOf(Int8Array.prototype) as object;
+
+/** The elements a typed array holds, read from the array itself, not from a `length` it claims. A DataView has none. */
+function elementCount(view: ArrayBufferView): number {
+ try {
+ return Reflect.get(TYPED_ARRAY_PROTOTYPE, 'length', view) as number;
+ } catch {
+ return 0;
+ }
+}
+
/**
* Return a copy of `value` with credential keys' values replaced by `[REDACTED]` (recursive). The copy always
* serializes: an object met again inside itself becomes `[Circular]`, a nested BigInt its decimal text, and
@@ -70,11 +81,13 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unkn
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 > room) return '[Truncated]';
+ // Read once: the same number gates the copy and sizes it, whatever the object answers the next time.
+ const length = node.length;
+ if (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]));
+ return Array.from({ length }, (_, index) => copy(node[index]));
}
- if (ArrayBuffer.isView(node) && ((node as { length?: number }).length ?? 0) > room) return '[Truncated]';
+ if (ArrayBuffer.isView(node) && elementCount(node) > 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;
diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.ts
index b85ff1d21..82b006045 100644
--- a/src/engine/adapters/baileys-logger.spec.ts
+++ b/src/engine/adapters/baileys-logger.spec.ts
@@ -195,6 +195,12 @@ describe('createBaileysLogger writes any record the library hands it', () => {
expect(deep).toMatchObject({ a: { b: { c: { d: { e: { file: '[Truncated]' } } } } }, msg: 'x' });
});
+ it('counts a typed array by the elements it has, not by the length it claims', () => {
+ const file = Object.defineProperty(new Uint8Array(1_000_001), 'length', { value: 0 });
+
+ expect(write({ file, jid: 'j' }, 'x')).toMatchObject({ file: '[Truncated]', jid: 'j', 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');