Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): honor an error result in a sandboxed hook chain
Yudhi Armyndharise40be49235c9
diff --git a/src/core/plugins/sandbox/worker-hooks.spec.ts b/src/core/plugins/sandbox/worker-hooks.spec.tsindex eb77a2b85..803f8f2fb 100644--- a/src/core/plugins/sandbox/worker-hooks.spec.ts+++ b/src/core/plugins/sandbox/worker-hooks.spec.ts@@ -104,6 +104,56 @@ describe('WorkerHookRegistry', () => {expect(sent.find(m => m.kind === 'hook-result')).toMatchObject({ continue: true });});+ it('discards data a handler returns with an error and reports the first failure', async () => {+ const { sent, post } = collect();+ const reg = new WorkerHookRegistry(post);+ const message = { id: 'm1', chatId: '[email protected]', body: 'secret' };+ let seen: unknown;+ reg.register('message:received', () => ({ continue: true, data: { ...message, body: '[redacted]' } }), 10);+ reg.register('message:received', () => ({ continue: true, data: message, error: new Error('first failure') }), 20);+ reg.register('message:received', () => Promise.reject(new Error('second failure')), 30);+ reg.register(+ 'message:received',+ ({ data }) => {+ seen = data;+ return { continue: true };+ },+ 40,+ );++ await reg.handleHook({ kind: 'hook', id: 1, event: 'message:received', data: message, source: 'Engine' });++ expect(seen).toEqual({ ...message, body: '[redacted]' });+ expect(sent.find(m => m.kind === 'hook-result')).toMatchObject({+ continue: true,+ data: { ...message, body: '[redacted]' },+ error: 'first failure',+ });+ });++ it('keeps a veto that comes with an error but does not apply its data', async () => {+ const { sent, post } = collect();+ const reg = new WorkerHookRegistry(post);+ const message = { id: 'm1', chatId: '[email protected]', body: 'secret' };+ let laterRan = false;+ reg.register('message:received', () => ({ continue: true, data: { ...message, body: '[redacted]' } }), 10);+ reg.register('message:received', () => ({ continue: false, data: message, error: new Error('failed') }), 20);+ reg.register('message:received', () => {+ laterRan = true;+ return { continue: true };+ });++ await reg.handleHook({ kind: 'hook', id: 1, event: 'message:received', data: message, source: 'Engine' });++ expect(laterRan).toBe(false);+ expect(sent.find(m => m.kind === 'hook-result')).toEqual({+ kind: 'hook-result',+ id: 1,+ continue: false,+ data: { ...message, body: '[redacted]' },+ });+ });+it('reports a throwing handler to the host on the hook-result (later handlers still run)', async () => {const { sent, post } = collect();const reg = new WorkerHookRegistry(post);diff --git a/src/core/plugins/sandbox/worker-hooks.ts b/src/core/plugins/sandbox/worker-hooks.tsindex 0380e16d1..52f8b9b98 100644--- a/src/core/plugins/sandbox/worker-hooks.ts+++ b/src/core/plugins/sandbox/worker-hooks.ts@@ -24,6 +24,7 @@ export interface WorkerHookContext {export interface WorkerHookResult {continue: boolean;data?: unknown;+ error?: Error;}export type WorkerHookHandler = (ctx: WorkerHookContext) => Promise<WorkerHookResult> | WorkerHookResult;@@ -93,13 +94,17 @@ export class WorkerHookRegistry {source: message.source,timestamp: new Date(),});- // Same rule as the host chain: a result the event cannot use is skipped, so an earlier handler's- // rewrite (a redaction) survives a later handler of this plugin returning null.- if (result.data !== undefined && (accept?.(result.data) ?? true)) data = result.data;+ // Same rules as the host chain: a result that reports an error, or that the event cannot use, is+ // skipped, so an earlier handler's rewrite (a redaction) survives a later handler of this plugin+ // returning null or failing.+ if (result.error === undefined && result.data !== undefined && (accept?.(result.data) ?? true)) {+ data = result.data;+ }if (!result.continue) {shouldContinue = false;break;}+ if (result.error) throw result.error;} catch (error) {// A handler error must not break the chain (mirrors the host hook manager). Report the FIRST// failure to the host so a throwing hook is visible to the operator instead of failing open