Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): reject an invalid handover state from a sandboxed plugin

Yudhi Armyndharis88c95bbd36c3
diff --git a/src/core/plugins/sandbox/capability-router.ts b/src/core/plugins/sandbox/capability-router.ts
index 254f80f84..bc9083694 100644
--- a/src/core/plugins/sandbox/capability-router.ts
+++ b/src/core/plugins/sandbox/capability-router.ts
@@ -81,8 +81,15 @@ export async function dispatchCapabilityVerb(
return context.net.fetch(s(0), args[1] as Parameters<typeof context.net.fetch>[1]);
case 'conversation.send':
return context.conversations.send(args[0] as ConversationSendEnvelope);
- case 'handover.set':
- return context.handover.set(mappingKey(0), args[1] as HandoverState);
+ case 'handover.set': {
+ // Only 'human'/'closed' hold a chat, so any other value would be stored and silently never take over.
+ const key = mappingKey(0);
+ const state = args[1];
+ if (state !== 'bot' && state !== 'human' && state !== 'closed') {
+ throw new Error(`Capability ${verb}: argument 1 must be 'bot', 'human' or 'closed'`);
+ }
+ return context.handover.set(key, state);
+ }
case 'mappings.upsert':
return context.mappings.upsert(mappingKey(0), s(1));
case 'mappings.get':
diff --git a/src/core/plugins/sandbox/handover-set.spec.ts b/src/core/plugins/sandbox/handover-set.spec.ts
index 08c512c58..2c8a8b906 100644
--- a/src/core/plugins/sandbox/handover-set.spec.ts
+++ b/src/core/plugins/sandbox/handover-set.spec.ts
@@ -6,3 +6,14 @@ it('routes handover.set to context.handover.set with (key, state)', async () =>
await dispatchCapabilityVerb({ handover: { set } } as never, 'handover.set', [key, 'human']);
expect(set).toHaveBeenCalledWith(key, 'human');
});
+
+it('rejects a handover.set state outside bot/human/closed before it reaches the context', async () => {
+ const set = jest.fn().mockResolvedValue(undefined);
+ const key = { sessionId: 's', chatId: 'c', instanceId: 'i' };
+ for (const state of ['Human', 'human ', null, 1]) {
+ await expect(dispatchCapabilityVerb({ handover: { set } } as never, 'handover.set', [key, state])).rejects.toThrow(
+ "Capability handover.set: argument 1 must be 'bot', 'human' or 'closed'",
+ );
+ }
+ expect(set).not.toHaveBeenCalled();
+});