Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): count log metadata values before measuring its text
Yudhi Armyndharis7f292f731aef
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.tsindex 739bbeff0..1321fe7b1 100644--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts@@ -787,22 +787,62 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {});});- it('still reports the exact length of a meta that is large but finite, up to 100000 values', async () => {+ it('still reports the exact length of a meta that has no more values than the size limit', async () => {const loader = makeLoader();seed(loader, { builtIn: false, instance: null });await loader.enablePlugin('p1');const logSpy = jest.spyOn(loggerOf(loader), 'log').mockImplementation(() => undefined);- // The array is one value and each entry another: 99999 entries make 100000 values, the most that is walked.- loader.capturedOnLog!('log', 'walked', ones(99_999) as unknown as Record<string, unknown>);- loader.capturedOnLog!('log', 'stopped', ones(100_000) as unknown as Record<string, unknown>);+ // The array is one value and each entry another, and the limit is 8192 values: a text of more values than+ // that is longer than the size limit, so the walk stops there and no length is known.+ loader.capturedOnLog!('log', 'walked', ones(8191) as unknown as Record<string, unknown>);+ loader.capturedOnLog!('log', 'stopped', ones(8192) as unknown as Record<string, unknown>);expect(logSpy).toHaveBeenNthCalledWith(1, '[p1] walked', {metaTruncated: true,- metaLength: 99_999 + 99_998 + 2,+ metaLength: 8191 + 8190 + 2,pluginId: 'p1',});expect(logSpy).toHaveBeenNthCalledWith(2, '[p1] stopped', { metaTruncated: true, pluginId: 'p1' });});++ it('replaces a typed array of 20 million elements 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();+ loader.capturedOnLog!('log', 'buffer', { b: new Uint8Array(20_000_000) });++ expect(Date.now() - started).toBeLessThan(1000);+ expect(logSpy).toHaveBeenCalledWith('[p1] buffer', { metaTruncated: true, pluginId: 'p1' });+ });++ it('counts a Map or a Set by its size', async () => {+ const loader = makeLoader();+ seed(loader, { builtIn: false, instance: null });+ await loader.enablePlugin('p1');+ const logSpy = jest.spyOn(loggerOf(loader), 'log').mockImplementation(() => undefined);++ loader.capturedOnLog!('log', 'map', { m: new Map(ones(9000).map((one, key) => [key, one])) });+ loader.capturedOnLog!('log', 'set', { s: new Set(ones(9000).map((_, key) => key)) });++ expect(logSpy).toHaveBeenNthCalledWith(1, '[p1] map', { metaTruncated: true, pluginId: 'p1' });+ expect(logSpy).toHaveBeenNthCalledWith(2, '[p1] set', { metaTruncated: true, pluginId: 'p1' });+ });++ it('still drops a circular meta without a marker, as it cannot be written as JSON', async () => {+ const loader = makeLoader();+ seed(loader, { builtIn: false, instance: null });+ await loader.enablePlugin('p1');+ const logSpy = jest.spyOn(loggerOf(loader), 'log').mockImplementation(() => undefined);+ const circular: Record<string, unknown> = { id: 1 };+ circular.self = circular;++ loader.capturedOnLog!('log', 'circular', circular);++ expect(logSpy).toHaveBeenCalledWith('[p1] circular', { pluginId: 'p1' });+ });});});diff --git a/src/core/plugins/plugin-sandbox-bridge.ts b/src/core/plugins/plugin-sandbox-bridge.tsindex 5ca299ba9..4b49f250c 100644--- a/src/core/plugins/plugin-sandbox-bridge.ts+++ b/src/core/plugins/plugin-sandbox-bridge.ts@@ -11,6 +11,7 @@ import { PluginWorkerHost } from './sandbox/plugin-worker-host';import { dispatchCapabilityVerb } from './sandbox/capability-router';import { PluginLogLevel } from './sandbox/protocol';import { shouldDispatchToPlugin } from './handover-gate';+import { countMetaValues } from './sandbox-log-meta';import { makeOnWebhookSubscribe } from './webhook-subscribe.util';import { registerPluginSearchProvider, unregisterPluginSearchProvider } from './search-provider-registration.util';import { INGRESS_DISPATCH_TIMEOUT_MS } from '../../modules/integration/integration.constants';@@ -46,10 +47,6 @@ const SANDBOX_HOOK_ERROR_LOG_INTERVAL_MS = 60000;const SANDBOX_LOG_MAX_PER_WINDOW = 200;const SANDBOX_LOG_WINDOW_MS = 10000;const SANDBOX_LOG_MAX_MESSAGE_LENGTH = 8192;-// A meta is measured by writing it out as JSON, and a shared reference is written out again at every use, so a meta-// that is small to hold can be enormous as text. The walk stops after this many values; a meta that long is over the-// size limit anyway and is replaced by a marker without a length.-const SANDBOX_LOG_MAX_META_VALUES = 100_000;/** Per-enable log-relay rate-limit window, shared by the onLog relay and the onWorkerExit final flush. */interface SandboxLogRelayState {@@ -592,17 +589,19 @@ export class PluginSandboxBridge {let boundedMeta = meta;if (meta !== undefined) {const reason = typeof meta?.error === 'string' ? { error: truncate(meta.error) } : undefined;- let values = 0;- try {- const metaLength = JSON.stringify(meta, (_key, value: unknown) => {- if (++values > SANDBOX_LOG_MAX_META_VALUES) throw new RangeError('meta is too large to measure');- return value;- }).length;- if (metaLength > SANDBOX_LOG_MAX_MESSAGE_LENGTH) boundedMeta = { ...reason, metaTruncated: true, metaLength };- } catch {- // Past the walk limit the meta is over the size limit; otherwise it is not serializable (circular or BigInt),- // so it cannot be logged as JSON anyway.- boundedMeta = values > SANDBOX_LOG_MAX_META_VALUES ? { ...reason, metaTruncated: true } : reason;+ // More values than the size limit write a longer text, so the text is only built, to learn its length, for a+ // meta with fewer: building it for any other could take seconds (a shared reference is written out at every+ // use) or exhaust the heap (a typed array lists a key per element).+ if (countMetaValues(meta, SANDBOX_LOG_MAX_MESSAGE_LENGTH) > SANDBOX_LOG_MAX_MESSAGE_LENGTH) {+ boundedMeta = { ...reason, metaTruncated: true };+ } else {+ try {+ const metaLength = JSON.stringify(meta).length;+ if (metaLength > SANDBOX_LOG_MAX_MESSAGE_LENGTH)+ boundedMeta = { ...reason, metaTruncated: true, metaLength };+ } catch {+ boundedMeta = reason; // not serializable (circular or BigInt), so it cannot be logged as JSON anyway+ }}}// The level comes off the wire unchecked (plugin code can post to parentPort directly), and thediff --git a/src/core/plugins/sandbox-log-meta.spec.ts b/src/core/plugins/sandbox-log-meta.spec.tsnew file mode 100644index 000000000..6bcc109ec--- /dev/null+++ b/src/core/plugins/sandbox-log-meta.spec.ts@@ -0,0 +1,66 @@+import { countMetaValues } from './sandbox-log-meta';++describe('countMetaValues', () => {+ const NO_LIMIT = Number.MAX_SAFE_INTEGER;+ const fanOut = (levels: number): Record<string, unknown> => {+ let node: Record<string, unknown> = { leaf: 1 };+ for (let level = 0; level < levels; level++) {+ node = Object.fromEntries(Array.from({ length: 10 }, (_, key) => [`k${key}`, node]));+ }+ return node;+ };++ it('counts every value, containers included', () => {+ expect(countMetaValues('text', NO_LIMIT)).toBe(1);+ expect(countMetaValues(null, NO_LIMIT)).toBe(1);+ expect(countMetaValues({ a: 1, b: [1, 2], c: { d: 'x' } }, NO_LIMIT)).toBe(1 + 1 + 1 + 2 + 1 + 1);+ });++ 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));+ });++ it('counts a typed array by its length, and a Map or a Set by its size', () => {+ expect(countMetaValues(new Uint8Array(100), NO_LIMIT)).toBe(1 + 100);+ expect(countMetaValues(Buffer.alloc(7), NO_LIMIT)).toBe(1 + 7);+ expect(countMetaValues(new Map([[1, 2]]), NO_LIMIT)).toBe(1 + 1);+ expect(countMetaValues(new Set([1, 2, 3]), NO_LIMIT)).toBe(1 + 3);+ });++ 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);+ });++ it('does not follow a cycle', () => {+ const circular: Record<string, unknown> = { id: 1 };+ circular.self = circular;++ expect(countMetaValues(circular, 8192)).toBe(1 + 1 + 1);+ });++ it('stops once the count passes the limit, however large the text would be', () => {+ const started = Date.now();+ const count = countMetaValues(fanOut(9), 8192);++ expect(Date.now() - started).toBeLessThan(1000);+ expect(count).toBe(8193);+ });++ it('counts a typed array without visiting its elements', () => {+ const started = Date.now();+ const count = countMetaValues({ b: new Uint8Array(20_000_000) }, 8192);++ expect(Date.now() - started).toBeLessThan(1000);+ expect(count).toBe(1 + 1 + 20_000_000);+ });++ it('handles nesting far deeper than the call stack allows', () => {+ let deep: Record<string, unknown> = {};+ 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);+ });+});diff --git a/src/core/plugins/sandbox-log-meta.ts b/src/core/plugins/sandbox-log-meta.tsnew file mode 100644index 000000000..7c2f955cb--- /dev/null+++ b/src/core/plugins/sandbox-log-meta.ts@@ -0,0 +1,42 @@+/**+ * Counts the values JSON.stringify would write for a worker's log metadata, and stops once the count passes `limit`.+ *+ * 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`.+ */+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 }> = [];+ const visit = (value: unknown): void => {+ count++;+ if (typeof value !== 'object' || value === null) return;+ if (ArrayBuffer.isView(value)) {+ count += (value as { length?: number }).length ?? 0;+ return;+ }+ if (value instanceof Map || value instanceof Set) {+ count += value.size;+ return;+ }+ // 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 });+ };+ 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++]);+ } else {+ open.delete(frame.node);+ frames.pop();+ }+ }+ return count;+}