Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): close the preview generator on the reply and edit paths

Yudhi Armyndharisced1413e4796
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1d5e2ebab..71d4e5996 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The filter builder's three selects, the status image picker and the templates session picker expose accessible names, so a screen reader no longer announces unnamed comboboxes on those surfaces.
- The plugin session picker exposes an accessible name, and a required array field's asterisk renders in the error colour. The caption lives outside `.form-group`, so the rule that colours the mark never matched it.
- Baileys forwards `mentions` on an audio send. The route accepts the field and whatsapp-web.js sent it, so the same request tagged group participants on one engine and silently did not on the other.
+- Baileys no longer fetches URLs through the library's own preview generator on the reply and edit routes. Only the text-send path installed the vetted generator, so a reply or an edit containing a link reached `link-preview-js`, which carries an unfixed SSRF advisory, with a caller-supplied URL.
- Replying with a quoted id that does not belong to the target chat is refused on Baileys with the same `404` whatsapp-web.js already answered. It was the last stored-message path with no chat check, so a reply could quote another conversation.
- Forwarding a message whose id is not in `fromChatId` is refused on Baileys with the same `404` whatsapp-web.js already answered. The parameter was accepted and then ignored, so any stored id forwarded from any claimed source.
- The Baileys engine resolves its WhatsApp Web version through a fallback chain instead of one call: an operator pin (`BAILEYS_WA_VERSION`), the two library endpoints, a disk cache of the last known-good version, then a built-in default. Each remote tier is bounded and rides the session proxy, and a stale answer is neither cached nor used. Thanks @giovanni-orciuolo.
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index a491698f2..e5b32a15c 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -500,7 +500,13 @@ export class BaileysMessaging {
// The destination is resolved like any other send: a lid-migrated contact rejects PN-addressed
// sends with ack error 463 (see toDeliverableJid).
const jid = await this.toDeliverableJid(chatId);
- const sent = await this.sock().sendMessage(jid, { text: body, edit: target.key });
+ // Same guard as sendContent: an edit carries text, so without it the library would fetch
+ // every URL in the new body through its own vulnerable generator.
+ const sent = await this.sock().sendMessage(
+ jid,
+ this.previewSafe({ text: body, edit: target.key }),
+ this.previewSafeOptions({ text: body, edit: target.key }),
+ );
return { id: sent?.key?.id ?? messageId, timestamp: this.host.toUnixSeconds(sent?.messageTimestamp) };
}
@@ -559,16 +565,42 @@ export class BaileysMessaging {
}
/** Send a Baileys content object and shape the result like the other sends. */
+ /**
+ * Keep the library's own preview generator unreachable, and keep it from firing at all.
+ *
+ * `generateWAMessageContent` calls the generator whenever the content carries `text` and no
+ * explicit `linkPreview` (Utils/messages.js), and the default generator delegates to
+ * `link-preview-js`, which carries an unfixed SSRF advisory. sendTextMessage guards both halves
+ * itself; every OTHER text-bearing send goes through here, and used to guard neither, so a reply
+ * or an edit containing a URL made the gateway fetch it through the vulnerable path.
+ *
+ * `linkPreview: null` is Baileys' explicit "no preview", which matches the documented engine
+ * default. A caller that set one already keeps it.
+ */
+ private previewSafe(content: AnyMessageContent): AnyMessageContent {
+ if (!('text' in content) || 'linkPreview' in content) return content;
+ return { ...content, linkPreview: null };
+ }
+
+ /**
+ * Options carrying the vetted generator, so the library's own is never selected. Added only for
+ * text-bearing content: media sends never reach the generator, and leaving their options untouched
+ * keeps the two-argument sendMessage call they already make.
+ */
+ private previewSafeOptions(content: AnyMessageContent, options?: MiscMessageGenerationOptions) {
+ if (!('text' in content)) return options;
+ return { ...(options ?? {}), getUrlInfo: (text: string) => generateSafeLinkPreview(text) };
+ }
+
private async sendContent(
chatId: string,
content: AnyMessageContent,
options?: MiscMessageGenerationOptions,
): Promise<MessageResult> {
const jid = await this.toDeliverableJid(chatId);
- const merged = this.withEphemeral(jid, options);
- const sent = merged
- ? await this.sock().sendMessage(jid, content, merged)
- : await this.sock().sendMessage(jid, content);
+ const safe = this.previewSafe(content);
+ const merged = this.previewSafeOptions(safe, this.withEphemeral(jid, options));
+ const sent = merged ? await this.sock().sendMessage(jid, safe, merged) : await this.sock().sendMessage(jid, safe);
if (sent) {
void this.host.putStoredMessage(sent)?.catch(err =>
this.host.logger.warn('Failed to persist sent message to store', {
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 68afec8c0..abc620263 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -2588,8 +2588,8 @@ describe('BaileysAdapter store-backed ops', () => {
expect(fakeStore.getMessage).toHaveBeenCalledWith('db-uuid-1', 'TARGET');
expect(fakeSock.sendMessage).toHaveBeenCalledWith(
- { text: 'my reply' },
- { quoted: stored },
+ { text: 'my reply', linkPreview: null },
+ expect.objectContaining({ quoted: stored }),
);
});
@@ -2723,8 +2723,8 @@ describe('BaileysAdapter store-backed ops', () => {
await adapter.replyToMessage('[email protected]', 'TARGET', 'my reply');
expect(fakeSock.sendMessage).toHaveBeenCalledWith(
- { text: 'my reply' },
- { quoted: stored, ephemeralExpiration: 604800 },
+ { text: 'my reply', linkPreview: null },
+ expect.objectContaining({ quoted: stored, ephemeralExpiration: 604800 }),
);
});
@@ -2762,10 +2762,15 @@ describe('BaileysAdapter store-backed ops', () => {
fakeSock.sendMessage.mockResolvedValue({ key: { ...ownStored.key }, messageTimestamp: 1700000010 });
const adapter = await ready();
const res = await adapter.editMessage('[email protected]', 'TARGET', 'edited body');
- expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', {
- text: 'edited body',
- edit: ownStored.key,
- });
+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(
+ {
+ text: 'edited body',
+ edit: ownStored.key,
+ linkPreview: null,
+ },
+ expect.objectContaining({ getUrlInfo: expect.any(Function) as unknown }) as unknown,
+ );
expect(res).toEqual({ id: 'TARGET', timestamp: 1700000010 });
});
@@ -2812,7 +2817,15 @@ describe('BaileysAdapter store-backed ops', () => {
fakeSock.sendMessage.mockResolvedValue(undefined);
const adapter = await ready();
await expect(adapter.editMessage('[email protected]', 'TARGET', 'x')).resolves.toBeDefined();
- expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', { text: 'x', edit: ownStored.key });
+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(
+ {
+ text: 'x',
+ edit: ownStored.key,
+ linkPreview: null,
+ },
+ expect.objectContaining({ getUrlInfo: expect.any(Function) as unknown }) as unknown,
+ );
});
it('editMessage sends to the LID-resolved deliverable jid (463 tctoken fix, same as the send path)', async () => {
@@ -2822,7 +2835,15 @@ describe('BaileysAdapter store-backed ops', () => {
const adapter = await ready();
await adapter.editMessage('[email protected]', 'TARGET', 'edited body');
expect(fakeSock.signalRepository.lidMapping.getLIDForPN).toHaveBeenCalledWith('[email protected]');
- expect(fakeSock.sendMessage).toHaveBeenCalledWith('484848@lid', { text: 'edited body', edit: ownStored.key });
+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(
+ '484848@lid',
+ {
+ text: 'edited body',
+ edit: ownStored.key,
+ linkPreview: null,
+ },
+ expect.objectContaining({ getUrlInfo: expect.any(Function) as unknown }) as unknown,
+ );
});
it('addLabelToChat wires 1:1 to sock.addChatLabel(chatId, labelId)', async () => {