Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(mcp): reject non-http(s) media urls in the send tools
Yudhi Armyndharis6e2c4c685573
diff --git a/src/core/agent-tools/tools/message.tools.spec.ts b/src/core/agent-tools/tools/message.tools.spec.tsindex b7d9c56e6..c37efc30e 100644--- a/src/core/agent-tools/tools/message.tools.spec.ts+++ b/src/core/agent-tools/tools/message.tools.spec.ts@@ -361,6 +361,26 @@ describe('messageTools', () => {expect(sendSticker).toHaveBeenCalledWith('s1', expect.objectContaining({ mentions: ['[email protected]'] }));});+ it('rejects a media url that is not http(s)', async () => {+ // Both engines decode a non-http(s) string as base64, so an ftp:// URL would send garbage bytes.+ const sendImage = jest.fn().mockResolvedValue({ id: 'm1' });+ const tools = makeTools({ sendImage } as unknown as MessageService);++ await expect(+ run(tools.get('MessageSendImage')!, { sessionId: 's1', chatId: '[email protected]', url: 'ftp://example.com/a.jpg' }),+ ).rejects.toMatchObject({+ response: { message: [expect.stringContaining('http(s)') as unknown as string] },+ });+ expect(sendImage).not.toHaveBeenCalled();++ await run(tools.get('MessageSendImage')!, {+ sessionId: 's1',+ chatId: '[email protected]',+ url: 'http://media_server:8080/a.jpg',+ });+ expect(sendImage).toHaveBeenCalledWith('s1', expect.objectContaining({ url: 'http://media_server:8080/a.jpg' }));+ });+it('MessageForward delegates to forward', async () => {const forward = jest.fn().mockResolvedValue({ id: 'm2' });const tools = makeTools({ forward } as unknown as MessageService);diff --git a/src/core/agent-tools/tools/message.tools.ts b/src/core/agent-tools/tools/message.tools.tsindex 54bc2d5e5..675923023 100644--- a/src/core/agent-tools/tools/message.tools.ts+++ b/src/core/agent-tools/tools/message.tools.ts@@ -1,4 +1,5 @@import { z } from 'zod';+import { isMediaUrl, MEDIA_URL_MESSAGE } from '../../../common/media/media-url';import { ApiKeyRole } from '../../../modules/auth/entities/api-key.entity';import type { MessageService } from '../../../modules/message/message.service';import {@@ -33,6 +34,13 @@ const quotedMessageIdSchema = z'the serialized message id, Baileys the raw key id of a message it has already stored.',);+/**+ * The same media url rule as the REST routes: both engines fetch only a string that starts with+ * http(s):// and decode anything else as base64, so any other value would go out as garbage bytes.+ */+const mediaUrl = (label: string) =>+ z.string().refine(isMediaUrl, { error: MEDIA_URL_MESSAGE }).optional().describe(`${label} URL (http/https)`);+/*** Mirrors the REST `mentions` field. The element rule and both caps come from the DTO rather than* being restated here: a tool handler calls the service directly, so the ValidationPipe never runs@@ -169,7 +177,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {inputSchema: z.object({sessionId,chatId: z.string().describe('Chat JID'),- url: z.string().url().optional().describe('Image URL (http/https)'),+ url: mediaUrl('Image'),base64: z.string().optional().describe('Base64-encoded image data'),mimetype: z.string().optional().describe('MIME type (required when using base64)'),filename: z.string().max(255).optional(),@@ -198,7 +206,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {inputSchema: z.object({sessionId,chatId: z.string().describe('Chat JID'),- url: z.string().url().optional().describe('Video URL (http/https)'),+ url: mediaUrl('Video'),base64: z.string().optional().describe('Base64-encoded video data'),mimetype: z.string().optional().describe('MIME type (required when using base64)'),filename: z.string().max(255).optional(),@@ -227,7 +235,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {inputSchema: z.object({sessionId,chatId: z.string().describe('Chat JID'),- url: z.string().url().optional().describe('Audio URL (http/https)'),+ url: mediaUrl('Audio'),base64: z.string().optional().describe('Base64-encoded audio data'),mimetype: z.string().optional().describe('MIME type (required when using base64)'),filename: z.string().max(255).optional(),@@ -258,7 +266,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {inputSchema: z.object({sessionId,chatId: z.string().describe('Chat JID'),- url: z.string().url().optional().describe('Document URL (http/https)'),+ url: mediaUrl('Document'),base64: z.string().optional().describe('Base64-encoded document data'),mimetype: z.string().optional().describe('MIME type (required when using base64)'),filename: z.string().max(255).optional(),@@ -337,7 +345,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {inputSchema: z.object({sessionId,chatId: z.string().describe('Chat JID'),- url: z.string().url().optional().describe('Sticker URL (http/https)'),+ url: mediaUrl('Sticker'),base64: z.string().optional().describe('Base64-encoded sticker data'),mimetype: z.string().optional().describe('MIME type (required when using base64)'),filename: z.string().max(255).optional(),