Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): count property names in the relay's size check

Yudhi Armyndharisea5ddcc0c3dc
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
index 4cd88c2c1..71c99028b 100644
--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
@@ -881,6 +881,20 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {
expect(logSpy).toHaveBeenCalledWith('[p1] shared-string', { metaTruncated: true, pluginId: 'p1' });
});
+ it('replaces one object with a long property name shared by 4000 entries by the size marker at once', async () => {
+ const loader = makeLoader();
+ seed(loader, { builtIn: false, instance: null });
+ await loader.enablePlugin('p1');
+ const logSpy = jest.spyOn(loggerOf(loader), 'log').mockImplementation(() => undefined);
+ const holder = { ['k'.repeat(100_000)]: 1 };
+
+ const started = Date.now();
+ loader.capturedOnLog!('log', 'shared-name', { list: Array.from({ length: 4000 }, () => holder) });
+
+ expect(Date.now() - started).toBeLessThan(1000);
+ expect(logSpy).toHaveBeenCalledWith('[p1] shared-name', { metaTruncated: true, pluginId: 'p1' });
+ });
+
it('counts a Map or a Set by its size', async () => {
const loader = makeLoader();
seed(loader, { builtIn: false, instance: null });
diff --git a/src/core/plugins/sandbox-log-meta.spec.ts b/src/core/plugins/sandbox-log-meta.spec.ts
index 81968cebe..c04556d75 100644
--- a/src/core/plugins/sandbox-log-meta.spec.ts
+++ b/src/core/plugins/sandbox-log-meta.spec.ts
@@ -10,19 +10,23 @@ describe('countMetaValues', () => {
return node;
};
- it('counts every value, containers included', () => {
+ it('counts every value, containers included, and the name of every property', () => {
expect(countMetaValues(null, NO_LIMIT)).toBe(1);
- expect(countMetaValues({ a: 1, b: [1, 2], c: { d: true } }, NO_LIMIT)).toBe(1 + 1 + 1 + 2 + 1 + 1);
+ expect(countMetaValues([1, [2, 3]], NO_LIMIT)).toBe(1 + 1 + 1 + 2);
+ // The root, then a: name and value, b: name, array and two elements, c: name, object, d: name and value.
+ expect(countMetaValues({ a: 1, b: [1, 2], c: { d: true } }, NO_LIMIT)).toBe(
+ 1 + (1 + 1) + (1 + 1 + 2) + (1 + 1 + 1 + 1),
+ );
});
// Each character of a string is written, and a string shared by several references is written at every use.
it('counts a string by its length, a shared one at every use', () => {
expect(countMetaValues('text', NO_LIMIT)).toBe(1 + 4);
expect(countMetaValues('', NO_LIMIT)).toBe(1);
- expect(countMetaValues({ d: 'x'.repeat(100) }, NO_LIMIT)).toBe(1 + 1 + 100);
+ expect(countMetaValues({ d: 'x'.repeat(100) }, NO_LIMIT)).toBe(1 + 1 + 1 + 100);
expect(countMetaValues(['abc', 'abc'], NO_LIMIT)).toBe(1 + 2 * (1 + 3));
const small = { s: 'x'.repeat(10) };
- expect(countMetaValues({ list: [small, small] }, NO_LIMIT)).toBe(1 + 1 + 2 * (1 + 1 + 10));
+ expect(countMetaValues({ list: [small, small] }, NO_LIMIT)).toBe(1 + 4 + 1 + 2 * (1 + 1 + 1 + 10));
const holder = { s: 'x'.repeat(500_000) };
const started = Date.now();
@@ -35,7 +39,7 @@ describe('countMetaValues', () => {
it('counts a shared reference at every use', () => {
const shared = { x: 1 };
- expect(countMetaValues({ a: shared, b: shared }, NO_LIMIT)).toBe(1 + 2 * (1 + 1));
+ expect(countMetaValues({ a: shared, b: shared }, NO_LIMIT)).toBe(1 + 2 * (1 + 1 + 1 + 1));
});
it('counts a typed array by its length, and a Map or a Set by its size', () => {
@@ -52,7 +56,7 @@ describe('countMetaValues', () => {
const count = countMetaValues({ boxed: new String('x'.repeat(10_000_000)) }, 8192);
expect(Date.now() - started).toBeLessThan(1000);
- expect(count).toBe(1 + 1 + 10_000_000);
+ expect(count).toBe(1 + 5 + 1 + 10_000_000);
expect(countMetaValues(new String('abc'), NO_LIMIT)).toBe(1 + 3);
// What structuredClone hands back may come from another realm, where instanceof String is false.
expect(countMetaValues(structuredClone(new String('abc')), NO_LIMIT)).toBe(1 + 3);
@@ -65,15 +69,34 @@ describe('countMetaValues', () => {
expect(countMetaValues(Object(10n), NO_LIMIT)).toBe(1);
});
+ // JSON.stringify writes the name of a property at every use of the object that has it, as it writes its value.
+ it('counts a property name by its length, a shared object at every use', () => {
+ expect(countMetaValues({ abc: 1 }, NO_LIMIT)).toBe(1 + 3 + 1);
+ expect(countMetaValues({ '': 1 }, NO_LIMIT)).toBe(1 + 0 + 1);
+ expect(countMetaValues({ a: { bcd: 1 } }, NO_LIMIT)).toBe(1 + 1 + 1 + 3 + 1);
+ expect(countMetaValues([{ abc: 1 }, { abc: 1 }], NO_LIMIT)).toBe(1 + 2 * (1 + 3 + 1));
+
+ const holder = { ['k'.repeat(100_000)]: 1 };
+ const started = Date.now();
+ const count = countMetaValues(
+ Array.from({ length: 4000 }, () => holder),
+ 8192,
+ );
+
+ expect(Date.now() - started).toBeLessThan(1000);
+ expect(count).toBeGreaterThan(8192);
+ });
+
it('counts a property that JSON.stringify skips, so the count can only err toward more', () => {
- expect(countMetaValues({ a: undefined, b: () => 1, c: Symbol('s') }, NO_LIMIT)).toBe(1 + 3);
+ expect(countMetaValues({ a: undefined, b: () => 1, c: Symbol('s') }, NO_LIMIT)).toBe(1 + 3 * (1 + 1));
});
it('does not follow a cycle', () => {
const circular: Record<string, unknown> = { id: 1 };
circular.self = circular;
- expect(countMetaValues(circular, 8192)).toBe(1 + 1 + 1);
+ // The root, id: name and value, self: name and the root met again (counted once, not followed).
+ expect(countMetaValues(circular, 8192)).toBe(1 + (2 + 1) + (4 + 1));
});
it('stops once the count passes the limit, however large the text would be', () => {
@@ -81,7 +104,15 @@ describe('countMetaValues', () => {
const count = countMetaValues(fanOut(9), 8192);
expect(Date.now() - started).toBeLessThan(1000);
- expect(count).toBe(8193);
+ expect(count).toBeGreaterThan(8192);
+ expect(count).toBeLessThanOrEqual(8192 + 3);
+ });
+
+ // The root costs 1 and every element 1: the walk stops at the first count past the limit, not before or after it.
+ it('stops at the first count past the limit', () => {
+ expect(countMetaValues(new Array<number>(20_000).fill(1), 8192)).toBe(8193);
+ expect(countMetaValues(new Array<number>(8191).fill(1), 8192)).toBe(8192);
+ expect(countMetaValues(new Array<number>(8192).fill(1), 8192)).toBe(8193);
});
it('counts a typed array without visiting its elements', () => {
@@ -89,7 +120,7 @@ describe('countMetaValues', () => {
const count = countMetaValues({ b: new Uint8Array(20_000_000) }, 8192);
expect(Date.now() - started).toBeLessThan(1000);
- expect(count).toBe(1 + 1 + 20_000_000);
+ expect(count).toBe(1 + 1 + 1 + 20_000_000);
});
it('handles nesting far deeper than the call stack allows', () => {
@@ -97,6 +128,6 @@ describe('countMetaValues', () => {
for (let i = 0; i < 100_000; i++) deep = { next: deep };
expect(countMetaValues(deep, 8192)).toBeGreaterThan(8192);
- expect(countMetaValues({ a: { b: { c: 1 } } }, 8192)).toBe(1 + 1 + 1 + 1);
+ expect(countMetaValues({ a: { b: { c: 1 } } }, 8192)).toBe(1 + 2 + 2 + 2);
});
});
diff --git a/src/core/plugins/sandbox-log-meta.ts b/src/core/plugins/sandbox-log-meta.ts
index cb1a2dad1..9d0072b4a 100644
--- a/src/core/plugins/sandbox-log-meta.ts
+++ b/src/core/plugins/sandbox-log-meta.ts
@@ -4,14 +4,16 @@
* JSON.stringify cannot be stopped part way: it lists every key of an object or typed array before it calls a
* replacer for the first one, and it writes a shared reference out again at every use. So the size of the text has
* to be bounded before it runs. A shared reference is counted at every use, a string, a typed array and a String
- * wrapper by their length. The count errs toward more: a property that JSON.stringify skips (undefined, a function, a symbol)
- * and the entries of a Map or Set (written as {}) are counted too. Every value that is written costs at least one
- * character, so a count past `limit` means a text longer than `limit`.
+ * wrapper by their length, and a property name by its length (it is written at every use of its object too). The
+ * count errs toward more: a property that JSON.stringify skips (undefined, a function, a symbol) and the entries of
+ * a Map or Set (written as {}) are counted too. Every value that is written costs at least one character, so a
+ * count past `limit` means a text longer than `limit`.
*/
export function countMetaValues(meta: unknown, limit: number): number {
let count = 0;
const open = new Set<object>();
- const frames: Array<{ node: object; items: ArrayLike<unknown>; next: number }> = [];
+ // An object is walked by its own enumerable keys, an array by its elements.
+ const frames: Array<{ node: object; keys: string[] | null; length: number; next: number }> = [];
const visit = (value: unknown): void => {
count++;
if (typeof value === 'string') count += value.length;
@@ -33,13 +35,20 @@ export function countMetaValues(meta: unknown, limit: number): number {
// A cycle: JSON.stringify refuses it, so there is nothing more to count.
if (open.has(value)) return;
open.add(value);
- frames.push({ node: value, items: Array.isArray(value) ? value : Object.values(value), next: 0 });
+ const keys = Array.isArray(value) ? null : Object.keys(value);
+ frames.push({ node: value, keys, length: keys ? keys.length : (value as unknown[]).length, next: 0 });
};
visit(meta);
while (frames.length > 0 && count <= limit) {
const frame = frames[frames.length - 1];
- if (frame.next < frame.items.length) {
- visit(frame.items[frame.next++]);
+ if (frame.next < frame.length) {
+ const index = frame.next++;
+ if (frame.keys) {
+ count += frame.keys[index].length;
+ visit((frame.node as Record<string, unknown>)[frame.keys[index]]);
+ } else {
+ visit((frame.node as unknown[])[index]);
+ }
} else {
open.delete(frame.node);
frames.pop();