Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): bound the sandbox log relay's size check

Yudhi Armyndharised128939d96f
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
index cbe7c7b29..739bbeff0 100644
--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
@@ -742,4 +742,67 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {
expect(longMeta.error).toBe(`${'e'.repeat(8192)}…[truncated]`);
expect(errorSpy).toHaveBeenNthCalledWith(4, '[p1] nullmeta', 'undefined', { pluginId: 'p1' });
});
+
+ // postMessage keeps shared references shared, so a meta that is small to hold writes out, at every use, as JSON
+ // text of any size: ten keys over the same child, eight levels deep, is a hundred million leaves.
+ describe('a meta whose shared references write out to an enormous text', () => {
+ 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;
+ };
+ const ones = (length: number): number[] => new Array<number>(length).fill(1);
+
+ it('is replaced by a size marker at once, without being written out', 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', 'fan-out', fanOut(8));
+
+ expect(Date.now() - started).toBeLessThan(1000);
+ expect(logSpy).toHaveBeenCalledWith('[p1] fan-out', { metaTruncated: true, pluginId: 'p1' });
+ });
+
+ it('keeps the error text of a worker error log', async () => {
+ const loader = makeLoader();
+ seed(loader, { builtIn: false, instance: null });
+ await loader.enablePlugin('p1');
+ const errorSpy = jest
+ .spyOn((loader as unknown as { logger: { error: jest.Mock } }).logger, 'error')
+ .mockImplementation(() => undefined);
+
+ const started = Date.now();
+ loader.capturedOnLog!('error', 'fan-out', { error: 'boom', fan: fanOut(8) });
+
+ expect(Date.now() - started).toBeLessThan(1000);
+ expect(errorSpy).toHaveBeenCalledWith('[p1] fan-out', 'undefined', {
+ error: 'boom',
+ metaTruncated: true,
+ pluginId: 'p1',
+ });
+ });
+
+ it('still reports the exact length of a meta that is large but finite, up to 100000 values', 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>);
+
+ expect(logSpy).toHaveBeenNthCalledWith(1, '[p1] walked', {
+ metaTruncated: true,
+ metaLength: 99_999 + 99_998 + 2,
+ pluginId: 'p1',
+ });
+ expect(logSpy).toHaveBeenNthCalledWith(2, '[p1] stopped', { metaTruncated: true, pluginId: 'p1' });
+ });
+ });
});
diff --git a/src/core/plugins/plugin-sandbox-bridge.ts b/src/core/plugins/plugin-sandbox-bridge.ts
index 61fabedce..5ca299ba9 100644
--- a/src/core/plugins/plugin-sandbox-bridge.ts
+++ b/src/core/plugins/plugin-sandbox-bridge.ts
@@ -46,6 +46,10 @@ 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 {
@@ -588,11 +592,17 @@ 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).length;
+ 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 {
- boundedMeta = reason; // not serializable (circular or BigInt), so it cannot be logged as JSON anyway
+ // 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;
}
}
// The level comes off the wire unchecked (plugin code can post to parentPort directly), and the