Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): count a String wrapper by its length in the relay
Yudhi Armyndharisd82b661828be
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.tsindex 1e5ce8240..7ced95141 100644--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts@@ -845,6 +845,20 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {expect(logSpy).toHaveBeenCalledWith('[p1] buffer', { metaTruncated: true, pluginId: 'p1' });});+ it('replaces a String wrapper of 10 million characters 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 started = Date.now();+ const boxed = structuredClone(new String('x'.repeat(10_000_000)));+ loader.capturedOnLog!('log', 'boxed', boxed as unknown as Record<string, unknown>);++ expect(Date.now() - started).toBeLessThan(1000);+ expect(logSpy).toHaveBeenCalledWith('[p1] boxed', { 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.tsindex 6bcc109ec..072fda694 100644--- a/src/core/plugins/sandbox-log-meta.spec.ts+++ b/src/core/plugins/sandbox-log-meta.spec.ts@@ -29,6 +29,26 @@ describe('countMetaValues', () => {expect(countMetaValues(new Set([1, 2, 3]), NO_LIMIT)).toBe(1 + 3);});+ // JSON.stringify writes a boxed primitive as the primitive it holds; its own properties are the characters of a+ // String wrapper, one per index, which are not values of their own.+ it('counts a String wrapper by its length without listing its characters', () => {+ const started = Date.now();+ 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(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);+ expect(countMetaValues(structuredClone(new String('x'.repeat(10_000_000))), 8192)).toBe(1 + 10_000_000);+ });++ it('counts a Number, Boolean or BigInt wrapper as one value', () => {+ expect(countMetaValues(new Number(42), NO_LIMIT)).toBe(1);+ expect(countMetaValues(new Boolean(false), NO_LIMIT)).toBe(1);+ expect(countMetaValues(Object(10n), NO_LIMIT)).toBe(1);+ });+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);});diff --git a/src/core/plugins/sandbox-log-meta.ts b/src/core/plugins/sandbox-log-meta.tsindex 7c2f955cb..67d51f56e 100644--- a/src/core/plugins/sandbox-log-meta.ts+++ b/src/core/plugins/sandbox-log-meta.ts@@ -3,10 +3,10 @@** 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 and a typed array by its 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`.+ * to be bounded before it runs. A shared reference is counted at every use, 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`.*/export function countMetaValues(meta: unknown, limit: number): number {let count = 0;@@ -23,6 +23,12 @@ export function countMetaValues(meta: unknown, limit: number): number {count += value.size;return;}+ // A String wrapper is written as its text, and its own properties are its characters, one per index. Its brand+ // is checked rather than instanceof, so a wrapper made in another realm counts too.+ if (Object.prototype.toString.call(value) === '[object String]') {+ count += (value as { length: number }).length;+ return;+ }// A cycle: JSON.stringify refuses it, so there is nothing more to count.if (open.has(value)) return;open.add(value);