Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): reject an unknown handover state from in-process plugins
Yudhi Armyndharis2573cb68617f
diff --git a/src/core/plugins/plugin-capability-context.ts b/src/core/plugins/plugin-capability-context.tsindex cc3e81b78..4a4ea9aad 100644--- a/src/core/plugins/plugin-capability-context.ts+++ b/src/core/plugins/plugin-capability-context.ts@@ -442,6 +442,13 @@ export class PluginCapabilityContext {// it reuses CONVERSATION_SEND rather than adding a new permission.this.assertPermission(plugin.manifest, PluginCapabilityPermission.CONVERSATION_SEND);this.assertSessionActive(plugin, key.sessionId);+ // The sandbox router checks this too; an in-process plain-JS plugin reaches here unchecked,+ // and any other value would be stored and silently never hold the chat.+ if (state !== 'bot' && state !== 'human' && state !== 'closed') {+ throw new PluginCapabilityError(+ `Plugin ${plugin.manifest.id}: handover state must be 'bot', 'human' or 'closed'`,+ );+ }const mapping = await this.hostServices.getConversationMappingPort().get({sessionId: key.sessionId,chatId: key.chatId,diff --git a/src/core/plugins/plugin-capability.spec.ts b/src/core/plugins/plugin-capability.spec.tsindex a845a160f..4716fbb94 100644--- a/src/core/plugins/plugin-capability.spec.ts+++ b/src/core/plugins/plugin-capability.spec.ts@@ -349,11 +349,11 @@ describe('PluginLoaderService capability facade — ctx.net', () => {describe('PluginLoaderService capability facade — ctx.conversations', () => {let loader: PluginLoaderService;- let mappingService: { getByProvider: jest.Mock };+ let mappingService: { getByProvider: jest.Mock; get: jest.Mock; setHandover: jest.Mock };let messageService: { sendText: jest.Mock; reply: jest.Mock };beforeEach(() => {- mappingService = { getByProvider: jest.fn() };+ mappingService = { getByProvider: jest.fn(), get: jest.fn(), setHandover: jest.fn() };messageService = {sendText: jest.fn().mockResolvedValue({ messageId: 'wamid', timestamp: 1 }),reply: jest.fn().mockResolvedValue({ messageId: 'wamid', timestamp: 1 }),@@ -421,6 +421,22 @@ describe('PluginLoaderService capability facade — ctx.conversations', () => {expect(messageService.reply).not.toHaveBeenCalled();});+ it('rejects a handover state outside bot/human/closed before any mapping lookup', async () => {+ mappingService.get.mockResolvedValue(mapping('sess-1'));+ mappingService.setHandover.mockResolvedValue(undefined);+ const ctx = contextFor(makePlugin(['*'], ['conversation:send']));+ const key = { sessionId: 'sess-1', chatId: '[email protected]', instanceId: 'inst-1' };++ await expect(ctx.handover.set(key, 'Human' as never)).rejects.toThrow(+ "Plugin test-ext: handover state must be 'bot', 'human' or 'closed'",+ );+ expect(mappingService.get).not.toHaveBeenCalled();+ expect(mappingService.setHandover).not.toHaveBeenCalled();++ await ctx.handover.set(key, 'human');+ expect(mappingService.setHandover).toHaveBeenCalledWith('m-1', 'human');+ });+it('sends with an explicit chatId without consulting the mapping service', async () => {const ctx = contextFor(makePlugin(['*'], ['conversation:send']));await ctx.conversations.send({ type: 'text', text: 'hi', sessionId: 'sess-1', chatId: '[email protected]' });