Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): guard ctx.messages writes against dead sessions (PluginCapabilityError, no orphan row)
rmyndharis74bbf2341633
diff --git a/src/core/plugins/plugin-capability.spec.ts b/src/core/plugins/plugin-capability.spec.tsindex 1994cf4b0..a907b4bf8 100644--- a/src/core/plugins/plugin-capability.spec.ts+++ b/src/core/plugins/plugin-capability.spec.ts@@ -29,6 +29,7 @@ function makePlugin(sessions?: string[]): PluginInstance {describe('PluginLoaderService capability facade — ctx.messages', () => {let loader: PluginLoaderService;let messageService: { sendText: jest.Mock; reply: jest.Mock };+ let sessionService: { getEngine: jest.Mock };let moduleRef: { get: jest.Mock };beforeEach(() => {@@ -36,7 +37,12 @@ describe('PluginLoaderService capability facade — ctx.messages', () => {sendText: jest.fn().mockResolvedValue({ messageId: 'wamid', timestamp: 1 }),reply: jest.fn().mockResolvedValue({ messageId: 'wamid', timestamp: 1 }),};- moduleRef = { get: jest.fn().mockReturnValue(messageService) };+ sessionService = { getEngine: jest.fn().mockReturnValue({}) }; // truthy live engine+ moduleRef = {+ get: jest+ .fn()+ .mockImplementation((token: unknown) => (token === SessionService ? sessionService : messageService)),+ };const configService = { get: jest.fn().mockReturnValue(undefined) } as unknown as ConfigService;const pluginStorage = {createPluginStorage: jest.fn().mockReturnValue({}),@@ -87,6 +93,13 @@ describe('PluginLoaderService capability facade — ctx.messages', () => {expect(moduleRef.get).not.toHaveBeenCalled();expect(messageService.sendText).not.toHaveBeenCalled();});++ it('rejects sendText with PluginCapabilityError when the session has no active engine', async () => {+ sessionService.getEngine.mockReturnValue(undefined);+ const ctx = contextFor(makePlugin(['*']));+ await expect(ctx.messages.sendText('dead-session', '[email protected]', 'hi')).rejects.toBeInstanceOf(PluginCapabilityError);+ expect(messageService.sendText).not.toHaveBeenCalled();+ });});describe('PluginLoaderService capability facade — ctx.engine', () => {diff --git a/src/core/plugins/plugin-loader.service.ts b/src/core/plugins/plugin-loader.service.tsindex d1fed5ca2..c7ca33960 100644--- a/src/core/plugins/plugin-loader.service.ts+++ b/src/core/plugins/plugin-loader.service.ts@@ -357,11 +357,14 @@ export class PluginLoaderService implements OnModuleInit {},messages: {sendText: async (sessionId, chatId, text) => {- this.assertSessionAllowed(plugin.manifest, sessionId);+ // Validate scope + that the session has a live engine BEFORE MessageService persists a+ // pending row: a dead/unstarted session must fail with PluginCapabilityError, not a raw+ // TypeError + orphaned row. resolveEngine also runs assertSessionAllowed.+ this.resolveEngine(plugin.manifest, sessionId);return this.getMessageService().sendText(sessionId, { chatId, text });},reply: async (sessionId, chatId, quotedMessageId, text) => {- this.assertSessionAllowed(plugin.manifest, sessionId);+ this.resolveEngine(plugin.manifest, sessionId);return this.getMessageService().reply(sessionId, { chatId, quotedMessageId, text });},} satisfies PluginMessagingCapability,