Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): drop an RSVP sent outside its event's chat

Yudhi Armyndharis3ebae8b2c222
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.ts
index 006093324..1283ca7e2 100644
--- a/src/engine/adapters/baileys-events.spec.ts
+++ b/src/engine/adapters/baileys-events.spec.ts
@@ -794,4 +794,128 @@ describe('BaileysEvents event RSVPs (event.response)', () => {
expect(onMessageAck).toHaveBeenCalledTimes(1);
expect(onEventResponse).not.toHaveBeenCalled();
});
+
+ describe('which chat an RSVP may answer for', () => {
+ const otherGroup = '[email protected]';
+ const storedIn =
+ (remoteJid: string, extra: Record<string, unknown> = {}) =>
+ () =>
+ Promise.resolve({
+ key: { id: 'EVT1', remoteJid, fromMe: false, ...extra },
+ message: { eventMessage: { name: 'Game night' } },
+ } as unknown as WAMessage);
+
+ it("drops an RSVP sent in another chat than its event's, which Baileys decrypts without comparing chats", async () => {
+ const onEventResponse = jest.fn();
+ const logger = createLogger('BaileysEventsSpec');
+ const warn = jest.spyOn(logger, 'warn').mockImplementation(() => undefined);
+ const events = new BaileysEvents(
+ makeHost({ getOnEventResponse: () => onEventResponse, getStoredMessage: storedIn(otherGroup), logger }),
+ );
+
+ // The event lives in otherGroup; the default RSVP envelope arrived in `group`.
+ events.handleMessagesUpdate([
+ { key: { id: 'EVT1', remoteJid: otherGroup }, update: { eventResponses: [rsvp()] } },
+ ] as never);
+ await flush();
+
+ expect(onEventResponse).not.toHaveBeenCalled();
+ expect(warn).toHaveBeenCalledTimes(1);
+ });
+
+ it('reports an RSVP whose stored event sits in the chat the answer arrived in', async () => {
+ const onEventResponse = jest.fn();
+ const events = new BaileysEvents(
+ makeHost({ getOnEventResponse: () => onEventResponse, getStoredMessage: storedIn(group) }),
+ );
+
+ events.handleMessagesUpdate([{ key: eventKey, update: { eventResponses: [rsvp()] } }] as never);
+ await flush();
+
+ expect(onEventResponse).toHaveBeenCalledWith(expect.objectContaining({ chatId: group, eventName: 'Game night' }));
+ });
+
+ it("reports a guest's 1:1 answer to the account's own event, kept in the guest's chat", async () => {
+ const onEventResponse = jest.fn();
+ const guest = '[email protected]';
+ const events = new BaileysEvents(
+ makeHost({ getOnEventResponse: () => onEventResponse, getStoredMessage: storedIn(guest, { fromMe: true }) }),
+ );
+
+ events.handleMessagesUpdate([
+ {
+ key: { id: 'EVT1', remoteJid: '[email protected]', fromMe: false },
+ update: {
+ eventResponses: [rsvp({ eventResponseMessageKey: { id: 'RSVP6', remoteJid: guest, fromMe: false } })],
+ },
+ },
+ ] as never);
+ await flush();
+
+ expect(onEventResponse).toHaveBeenCalledWith(expect.objectContaining({ chatId: guest, responderId: guest }));
+ });
+
+ it("compares a lid chat with its phone number once the key's pair is known", async () => {
+ const onEventResponse = jest.fn();
+ const lid = '99887766@lid';
+ const phone = '[email protected]';
+ let paired = false;
+ const events = new BaileysEvents(
+ makeHost({
+ getOnEventResponse: () => onEventResponse,
+ getStoredMessage: storedIn(phone, { fromMe: true }),
+ recordKeyLidMappings: () => {
+ paired = true;
+ },
+ toNeutralJid: jid => (paired && jid === lid ? phone : jid),
+ }),
+ );
+
+ events.handleMessagesUpdate([
+ {
+ key: { id: 'EVT1', remoteJid: '[email protected]', fromMe: false },
+ update: {
+ eventResponses: [
+ rsvp({ eventResponseMessageKey: { id: 'RSVP7', remoteJid: lid, remoteJidAlt: phone, fromMe: false } }),
+ ],
+ },
+ },
+ ] as never);
+ await flush();
+
+ expect(onEventResponse).toHaveBeenCalledWith(expect.objectContaining({ chatId: phone, responderId: phone }));
+ });
+ });
+
+ it("learns the RSVP key's lid and phone pair before naming the responder", async () => {
+ const onEventResponse = jest.fn();
+ const lid = '99887766@lid';
+ const phone = '[email protected]';
+ let paired = false;
+ const events = new BaileysEvents(
+ makeHost({
+ getOnEventResponse: () => onEventResponse,
+ recordKeyLidMappings: () => {
+ paired = true;
+ },
+ toNeutralJid: jid => (paired && jid === lid ? phone : jid),
+ }),
+ );
+
+ events.handleMessagesUpdate([
+ {
+ key: eventKey,
+ update: {
+ eventResponses: [
+ rsvp({
+ eventResponseMessageKey: { id: 'RSVP8', remoteJid: group, participant: lid, participantAlt: phone },
+ }),
+ ],
+ },
+ },
+ ] as never);
+ await flush();
+
+ expect(onEventResponse).toHaveBeenCalledWith(expect.objectContaining({ responderId: phone }));
+ });
});
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index ade86bf47..23b7a2312 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -933,6 +933,30 @@ export class BaileysEvents {
const callback = this.host.getOnEventResponse();
const envelope = rsvp.eventResponseMessageKey;
if (!callback || !eventKey?.id || !eventKey.remoteJid || !envelope?.id) return;
+ // Learned before any id below is canonicalized, as an inbound message does, so a guest answering
+ // for the first time is not reported by lid once and by phone number afterwards.
+ this.host.recordKeyLidMappings(envelope);
+ // Baileys decrypts an RSVP against whichever stored event its creation key names and compares no
+ // chats, so a member of two groups can answer in one an event posted in the other. The answer is
+ // reported for the chat it arrived in, so one sent outside its event's chat is dropped, as an edit,
+ // revoke or reaction aimed at a message of another chat is. Fails open like those: a store that
+ // cannot be read, or ids that cannot be compared, report the RSVP as before.
+ const stored = await this.readStoredMessage(eventKey.id, 'reading the event an RSVP answers');
+ if (
+ stored &&
+ differentWaIds(
+ [stored.key.remoteJid, stored.key.remoteJidAlt],
+ [envelope.remoteJid, envelope.remoteJidAlt],
+ jid => this.host.toNeutralJid(jid),
+ )
+ ) {
+ this.host.logger.warn('Dropping an event response sent in a chat other than its event', {
+ msgId: eventKey.id,
+ responseMessageId: envelope.id,
+ remoteJid: envelope.remoteJid,
+ });
+ return;
+ }
const fromMe = envelope.fromMe === true;
// The creation key is the guest's view of the event (in a 1:1 chat its remoteJid is this
// account), so the chat comes from the RSVP's own key, which Baileys has put on our side.
@@ -964,7 +988,6 @@ export class BaileysEvents {
...(known.pushName ? { pushName: known.pushName } : {}),
};
}
- const stored = await this.readStoredMessage(eventKey.id, 'reading the event an RSVP answers');
if (stored?.message) {
const b = await this.host.loadLib();
const name = b.normalizeMessageContent(stored.message)?.eventMessage?.name;