Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): answer a retry only with a message from the asking chat

Yudhi Armyndharis9395b5c26d52
diff --git a/src/engine/adapters/baileys-lifecycle.ts b/src/engine/adapters/baileys-lifecycle.ts
index 3eb9782d8..0dd96193f 100644
--- a/src/engine/adapters/baileys-lifecycle.ts
+++ b/src/engine/adapters/baileys-lifecycle.ts
@@ -23,7 +23,7 @@ import {
CONNECTION_REPLACED_REASON,
LOGOUT_CLEANUP_FAILED_REASON,
} from '../terminal-engine-failure';
-import type { BaileysEvents } from './baileys-events';
+import { differentWaIds, type BaileysEvents } from './baileys-events';
import type { BaileysHistory } from './baileys-history';
import type { BaileysSessionStore } from './baileys-session-store';
@@ -134,6 +134,7 @@ export interface BaileysLifecycleHost {
readonly liveCalls: Map<string, { callFrom: string; expiresAt: number }>;
/** `628999:[email protected]` / `[email protected]` -> `628999`. */
extractPhone(id: string | undefined): string | null;
+ toNeutralJid(jid: string): string;
/** Persist contact records pushed by the socket (contacts.upsert/update, messaging-history.set). */
upsertContacts: BaileysSessionStore['upsertContacts'];
/** Persist chat records pushed by the socket (chats.upsert/update, messaging-history.set). */
@@ -373,12 +374,18 @@ export class BaileysLifecycle {
// implementation, WhatsApp's message-retry protocol — triggered whenever a recipient's client
// fails to decrypt on the first attempt — has nothing to resend, so the recipient is stuck on
// "waiting for this message" indefinitely instead of the retry resolving it within seconds.
- // Backed by the same messageStore used for reply/forward/react/delete-by-id.
+ // Backed by the same messageStore used for reply/forward/react/delete-by-id. Baileys relays the
+ // answer to key.remoteJid, and a retry receipt names its message by id alone, so a stored message
+ // from a provably different chat is refused: a forged receipt must not pull it into this one.
getMessage: async key => {
if (!key.id) {
return undefined;
}
const stored = await this.host.config.messageStore?.getMessage(this.host.config.dbSessionId, key.id);
+ const neutral = (jid: string): string => this.host.toNeutralJid(jid);
+ if (stored && differentWaIds([stored.key.remoteJid, stored.key.remoteJidAlt], [key.remoteJid], neutral)) {
+ return undefined;
+ }
return stored?.message ?? undefined;
},
logger: baileysLogger,
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 76b9b0780..19f256740 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -272,6 +272,26 @@ describe('BaileysAdapter lifecycle & status', () => {
expect(onReady).toHaveBeenCalledWith('628999', 'Me');
});
+ it('answers a decryption retry only with a stored message from the chat that asks for it', async () => {
+ await newAdapter().initialize(noopCallbacks({}));
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
+ const makeWASocket = jest.requireMock('@whiskeysockets/baileys').default as jest.Mock;
+ const [[{ getMessage }]] = makeWASocket.mock.calls as Array<
+ [{ getMessage: (key: { remoteJid?: string; id?: string }) => Promise<unknown> }]
+ >;
+ const content = { conversation: 'hi' };
+ fakeStore.getMessage.mockResolvedValue({
+ key: { remoteJid: '[email protected]', fromMe: true, id: 'M1' },
+ message: content,
+ });
+
+ await expect(getMessage({ remoteJid: '[email protected]', id: 'M1' })).resolves.toBe(content);
+ await expect(getMessage({ remoteJid: '[email protected]', id: 'M1' })).resolves.toBeUndefined();
+ await expect(getMessage({ remoteJid: '[email protected]', id: 'M1' })).resolves.toBeUndefined();
+ // A lid the session cannot map may be that same chat, so the retry is still answered.
+ await expect(getMessage({ remoteJid: '99887766@lid', id: 'M1' })).resolves.toBe(content);
+ });
+
it('on a logged-out close: DISCONNECTED, onDisconnected, and NO reconnect', async () => {
const rmSpy = jest.spyOn(fs.promises, 'rm').mockResolvedValue(undefined);
try {