Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): fail closed when a conversation mapping belongs to another session (#920)

Yudhi Armyndharisfdc2b4da7b17
diff --git a/src/core/plugins/plugin-capability.spec.ts b/src/core/plugins/plugin-capability.spec.ts
index 3b8ec9911..8871feafb 100644
--- a/src/core/plugins/plugin-capability.spec.ts
+++ b/src/core/plugins/plugin-capability.spec.ts
@@ -13,6 +13,7 @@ import {
} from './plugin.interfaces';
import { MessageService } from '../../modules/message/message.service';
import { SessionService } from '../../modules/session/session.service';
+import { ConversationMappingService } from '../../modules/integration/conversation-mapping.service';
function makePlugin(
sessions?: string[],
@@ -282,3 +283,83 @@ describe('PluginLoaderService capability facade — ctx.net', () => {
await expect(ctx.net.fetch('https://api.example.com/x')).rejects.toBeInstanceOf(PluginCapabilityError);
});
});
+
+describe('PluginLoaderService capability facade — ctx.conversations', () => {
+ let loader: PluginLoaderService;
+ let mappingService: { getByProvider: jest.Mock };
+ let messageService: { sendText: jest.Mock; reply: jest.Mock };
+
+ beforeEach(() => {
+ mappingService = { getByProvider: jest.fn() };
+ messageService = {
+ sendText: jest.fn().mockResolvedValue({ messageId: 'wamid', timestamp: 1 }),
+ reply: jest.fn().mockResolvedValue({ messageId: 'wamid', timestamp: 1 }),
+ };
+ const moduleRef = {
+ get: jest
+ .fn()
+ .mockImplementation((token: unknown) =>
+ token === ConversationMappingService ? mappingService : messageService,
+ ),
+ };
+ const configService = { get: jest.fn().mockReturnValue(undefined) } as unknown as ConfigService;
+ const pluginStorage = {
+ createPluginStorage: jest.fn().mockReturnValue({}),
+ } as unknown as PluginStorageService;
+ loader = new PluginLoaderService(
+ configService,
+ new HookManager(),
+ pluginStorage,
+ moduleRef as unknown as ModuleRef,
+ );
+ });
+
+ function contextFor(plugin: PluginInstance): PluginContext {
+ return (loader as unknown as { createPluginContext: (p: PluginInstance) => PluginContext }).createPluginContext(
+ plugin,
+ );
+ }
+
+ const sendEnv = {
+ type: 'text' as const,
+ text: 'hi',
+ instanceId: 'inst-1',
+ source: { provider: 'chatwoot', externalConversationId: 'conv-42' },
+ };
+ const mapping = (sessionId: string) => ({
+ id: 'm-1',
+ sessionId,
+ chatId: '[email protected]',
+ pluginId: 'test-ext',
+ instanceId: 'inst-1',
+ providerConversationId: 'conv-42',
+ });
+
+ it('resolves chatId from a mapping on the SAME session as the envelope, then sends', async () => {
+ mappingService.getByProvider.mockResolvedValue(mapping('sess-1'));
+ const ctx = contextFor(makePlugin(['*'], ['conversation:send']));
+ await ctx.conversations.send({ ...sendEnv, sessionId: 'sess-1' });
+ expect(mappingService.getByProvider).toHaveBeenCalledWith('test-ext', 'inst-1', 'conv-42');
+ expect(messageService.sendText).toHaveBeenCalledWith('sess-1', { chatId: '[email protected]', text: 'hi' });
+ });
+
+ it('rejects a mapping owned by ANOTHER session and never sends (wrong-session send)', async () => {
+ // The plugin is activated for BOTH sessions, so the per-session activation gate passes — only the
+ // mapping's own sessionId stops a stale mapping (e.g. left over after a scope move) from routing
+ // sess-1's envelope out through sess-2's chat.
+ mappingService.getByProvider.mockResolvedValue(mapping('sess-2'));
+ const ctx = contextFor(makePlugin(['*'], ['conversation:send'], ['sess-1', 'sess-2']));
+ await expect(ctx.conversations.send({ ...sendEnv, sessionId: 'sess-1' })).rejects.toThrow(
+ /belongs to session sess-2, not sess-1/,
+ );
+ expect(messageService.sendText).not.toHaveBeenCalled();
+ expect(messageService.reply).not.toHaveBeenCalled();
+ });
+
+ 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]' });
+ expect(mappingService.getByProvider).not.toHaveBeenCalled();
+ expect(messageService.sendText).toHaveBeenCalledWith('sess-1', { chatId: '[email protected]', text: 'hi' });
+ });
+});
diff --git a/src/core/plugins/plugin-loader.service.ts b/src/core/plugins/plugin-loader.service.ts
index 6927dd51d..cc4757ee1 100644
--- a/src/core/plugins/plugin-loader.service.ts
+++ b/src/core/plugins/plugin-loader.service.ts
@@ -1265,6 +1265,16 @@ export class PluginLoaderService implements OnModuleInit, OnApplicationBootstrap
`Plugin ${plugin.manifest.id}: no conversation mapping for instance ${env.instanceId} / ${env.source.externalConversationId}`,
);
}
+ // Fail closed on a cross-session mapping: getByProvider keys on (pluginId, instanceId,
+ // providerConversationId) only, so a stale row can resolve to a chat owned by a DIFFERENT
+ // session than the envelope's. Parity with the assertSessionActive(m.sessionId) check on
+ // mappings.getByProvider below — never send through a session the mapping does not belong to.
+ // (mapping.sessionId is NOT NULL in the entity, so a plain inequality check suffices.)
+ if (mapping.sessionId !== env.sessionId) {
+ throw new PluginCapabilityError(
+ `Plugin ${plugin.manifest.id}: conversation mapping for instance ${env.instanceId} / ${env.source.externalConversationId} belongs to session ${mapping.sessionId}, not ${env.sessionId}`,
+ );
+ }
return mapping.chatId;
},
// Re-establish the in-flight hook context around the downstream send so an adapter that calls