Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): bound the text a worker reports in a result
Yudhi Armyndharis86ebdb1ba6f3
diff --git a/src/core/plugins/sandbox/plugin-worker-host.spec.ts b/src/core/plugins/sandbox/plugin-worker-host.spec.tsindex c9336e56a..92283a42d 100644--- a/src/core/plugins/sandbox/plugin-worker-host.spec.ts+++ b/src/core/plugins/sandbox/plugin-worker-host.spec.ts@@ -1218,3 +1218,170 @@ describe('PluginWorkerHost', () => {});});});++// The text a worker reports is whatever its code posts. The router keeps only a string, capped in length, and a+// fixed marker for anything else, before the value is used to build an Error, a log line or a health note.+describe('the text a worker reports in a result', () => {+ const MARKER = '[non-string worker value]';+ const tick = (): Promise<void> => new Promise(resolve => setImmediate(resolve));+ // Each level holds the level below twice: written out by String() it is 2 ** levels pieces.+ const diamond = (levels: number): unknown => {+ let value: unknown = [];+ for (let level = 0; level < levels; level++) value = [value, value];+ return structuredClone(value);+ };+ const withTextOf = (calls: { count: number }): unknown => ({+ toString: (): string => {+ calls.count++;+ return 'password=value-text';+ },+ });+ const webhook = () => ({+ instanceId: 'i',+ route: 'r',+ method: 'POST',+ headers: {},+ query: {},+ body: '{}',+ rawBody: '{}',+ verified: true,+ deliveryId: 'd1',+ timeoutMs: 1000,+ });+ const sentOf = <K extends HostToWorkerMessage['kind']>(ch: FakeChannel, kind: K): { id: number } =>+ ch.sent.find(message => message.kind === kind) as unknown as { id: number };++ // One scenario per kind: it posts the worker's reply carrying `value` as the text and returns what the host settled.+ const scenarios: Array<[string, (ch: FakeChannel, value: unknown) => Promise<string | undefined>]> = [+ [+ "an 'error' before ready",+ async (ch, value) => {+ const host = new PluginWorkerHost(ch);+ const loading = host.load('/p/index.js');+ ch.reply({ kind: 'error', error: value as string });+ return loading.then(+ () => undefined,+ (error: Error) => error.message,+ );+ },+ ],+ [+ 'a failed lifecycle-result',+ async (ch, value) => {+ const host = new PluginWorkerHost(ch);+ void host.load('/p/index.js');+ ch.reply({ kind: 'ready' });+ const running = host.runLifecycle('onEnable');+ ch.reply({ kind: 'lifecycle-result', id: lastLifecycle(ch).id, ok: false, error: value as string });+ return running.then(+ () => undefined,+ (error: Error) => error.message,+ );+ },+ ],+ [+ 'a hook-result with an error',+ async (ch, value) => {+ const host = new PluginWorkerHost(ch);+ const dispatched = host.dispatchHook({+ event: 'message:received',+ data: {},+ source: 'Engine',+ timeoutMs: 1000,+ });+ ch.reply({ kind: 'hook-result', id: sentOf(ch, 'hook').id, continue: true, error: value as string });+ return (await dispatched).error;+ },+ ],+ [+ 'a webhook-result with an error',+ async (ch, value) => {+ const host = new PluginWorkerHost(ch);+ const dispatched = host.dispatchWebhook(webhook());+ ch.reply({ kind: 'webhook-result', id: sentOf(ch, 'webhook').id, status: 500, error: value as string });+ return (await dispatched).error;+ },+ ],+ [+ 'a failed search-result',+ async (ch, value) => {+ const host = new PluginWorkerHost(ch);+ const dispatched = host.dispatchSearch({ query: { q: 'x' }, timeoutMs: 1000 });+ ch.reply({ kind: 'search-result', id: sentOf(ch, 'search').id, ok: false, error: value as string });+ const result = await dispatched;+ return result.ok ? undefined : result.error;+ },+ ],+ [+ 'a health-result with a message',+ async (ch, value) => {+ const host = new PluginWorkerHost(ch);+ const checked = host.healthCheck(1000);+ ch.reply({ kind: 'health-result', id: sentOf(ch, 'health-check').id, healthy: true, message: value as string });+ return (await checked).message;+ },+ ],+ ];++ it.each(scenarios)('keeps a fixed marker for a nested array shared at every level in %s', async (_name, run) => {+ const started = Date.now();+ const text = await run(new FakeChannel(), diamond(22));++ expect(Date.now() - started).toBeLessThan(500);+ expect(text).toBe(MARKER);+ });++ it.each(scenarios)('does not ask an object for its own text in %s', async (_name, run) => {+ const calls = { count: 0 };++ const text = await run(new FakeChannel(), withTextOf(calls));++ expect(text).toBe(MARKER);+ expect(calls.count).toBe(0);+ });++ it.each(scenarios)('keeps a string of 8192 characters and cuts a longer one in %s', async (_name, run) => {+ const kept = await run(new FakeChannel(), 'x'.repeat(8192));+ const cut = await run(new FakeChannel(), 'x'.repeat(100_000));+ const short = await run(new FakeChannel(), 'short text');++ expect(kept).toBe('x'.repeat(8192));+ expect(cut).toBe(`${'x'.repeat(8192)}…[truncated]`);+ expect(short).toBe('short text');+ });++ it('still resolves a webhook-result without an error as ok, with a null error read as none', async () => {+ const ch = new FakeChannel();+ const host = new PluginWorkerHost(ch);+ const dispatched = host.dispatchWebhook(webhook());++ ch.reply({ kind: 'webhook-result', id: sentOf(ch, 'webhook').id, status: 200, error: null as unknown as string });++ await expect(dispatched).resolves.toMatchObject({ ok: true, status: 200, error: undefined });+ });++ it('keeps a worker error that is not text out of the exit message too', () => {+ const ch = new FakeChannel();+ const onExit = jest.fn();+ new PluginWorkerHost(ch, undefined, undefined, undefined, undefined, undefined, undefined, undefined, onExit);++ ch.reply({ kind: 'error', error: diamond(22) as string });+ ch.crash(1);++ expect(onExit).toHaveBeenCalledWith(1, false, MARKER);+ });++ it('answers a capability request whose verb is not text with an error, without building text from it', async () => {+ const ch = new FakeChannel();+ const dispatcher = jest.fn();+ const calls = { count: 0 };+ new PluginWorkerHost(ch, dispatcher);++ ch.reply({ kind: 'cap', id: 7, verb: withTextOf(calls) as string, args: [] });+ await tick();++ expect(ch.last()).toEqual({ kind: 'cap-result', id: 7, ok: false, error: MARKER });+ expect(dispatcher).not.toHaveBeenCalled();+ expect(calls.count).toBe(0);+ });+});diff --git a/src/core/plugins/sandbox/plugin-worker-host.ts b/src/core/plugins/sandbox/plugin-worker-host.tsindex 20426474c..96d246aa3 100644--- a/src/core/plugins/sandbox/plugin-worker-host.ts+++ b/src/core/plugins/sandbox/plugin-worker-host.ts@@ -27,6 +27,19 @@ const SEND_CAP_TIMEOUT_FACTOR = 4;/** Longest worker-reported error kept for an exit message and the plugin's status. */const MAX_WORKER_ERROR_CHARS = 500;+/** Longest text a worker reports in a result that the host keeps or forwards, and what stands for a non-string. */+const MAX_WORKER_TEXT_CHARS = 8192;+const NON_STRING_WORKER_VALUE = '[non-string worker value]';++/**+ * The text of a field a worker filled in: a string, cut to a bounded length, or a fixed marker for anything else.+ * Plugin code can post any value, so nothing is turned into text (String(), a template, an Error message) before this.+ */+function workerText(value: unknown): string {+ if (typeof value !== 'string') return NON_STRING_WORKER_VALUE;+ return value.length > MAX_WORKER_TEXT_CHARS ? `${value.slice(0, MAX_WORKER_TEXT_CHARS)}…[truncated]` : value;+}+/*** Timed-out hook dispatches whose in-flight claim is still honoured, per worker. A handler that never* returns would otherwise keep one entry per dispatch for as long as the worker lives; past this many,@@ -467,8 +480,9 @@ export class PluginWorkerHost {});break;case 'error': {- this.lastWorkerError = String(message.error).replace(/\s+/g, ' ').slice(0, MAX_WORKER_ERROR_CHARS);- const error = new Error(message.error);+ const text = workerText(message.error);+ this.lastWorkerError = text.replace(/\s+/g, ' ').slice(0, MAX_WORKER_ERROR_CHARS);+ const error = new Error(text);this.drain(this.readyWaiters, w => {if (w.timer) clearTimeout(w.timer);w.reject(error);@@ -481,7 +495,7 @@ export class PluginWorkerHost {this.pending.delete(message.id);if (waiter.timer) clearTimeout(waiter.timer);if (message.ok) waiter.resolve();- else waiter.reject(new Error(message.error));+ else waiter.reject(new Error(workerText(message.error)));break;}case 'cap':@@ -503,7 +517,7 @@ export class PluginWorkerHost {clearTimeout(waiter.timer);const result: { continue: boolean; data?: unknown; error?: string } = { continue: message.continue };if (message.data !== undefined) result.data = message.data;- if (message.error !== undefined) result.error = message.error;+ if (message.error !== undefined) result.error = workerText(message.error);waiter.resolve(result);break;}@@ -517,7 +531,7 @@ export class PluginWorkerHost {status: message.status,headers: message.headers,body: message.body,- error: message.error,+ error: message.error == null ? undefined : workerText(message.error),});break;}@@ -526,7 +540,10 @@ export class PluginWorkerHost {if (!waiter) return;this.healthPending.delete(message.id);clearTimeout(waiter.timer);- waiter.resolve({ healthy: message.healthy, message: message.message });+ waiter.resolve({+ healthy: message.healthy,+ message: message.message === undefined ? undefined : workerText(message.message),+ });break;}case 'pong':@@ -544,13 +561,17 @@ export class PluginWorkerHost {this.searchPending.delete(message.id);clearTimeout(waiter.timer);if (message.ok) waiter.resolve({ ok: true, results: message.results });- else waiter.resolve({ ok: false, error: message.error });+ else waiter.resolve({ ok: false, error: workerText(message.error) });break;}}}private async handleCapRequest(message: Extract<WorkerToHostMessage, { kind: 'cap' }>): Promise<void> {+ if (typeof message.verb !== 'string') {+ this.channel.postMessage({ kind: 'cap-result', id: message.id, ok: false, error: NON_STRING_WORKER_VALUE });+ return;+ }if (this.maxInFlightCaps !== undefined && this.inFlightCaps >= this.maxInFlightCaps) {this.channel.postMessage({kind: 'cap-result',