Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): address a status revoke to the status recipients

Yudhi Armyndharis89724405e1b1
diff --git a/src/engine/adapters/baileys-status-own-send.spec.ts b/src/engine/adapters/baileys-status-own-send.spec.ts
index 37a30e3a6..3a4732052 100644
--- a/src/engine/adapters/baileys-status-own-send.spec.ts
+++ b/src/engine/adapters/baileys-status-own-send.spec.ts
@@ -1,5 +1,6 @@
import { BaileysStatus, type BaileysStatusHost } from './baileys-status';
import type { WASocket } from '@whiskeysockets/baileys';
+import { EngineRefusedError } from '../../common/errors/engine-refused.error';
/**
* A status post and a status revoke both go through `sock.sendMessage` directly rather than the
@@ -39,22 +40,64 @@ describe('BaileysStatus records the ids of the statuses this session sends', ()
});
it('remembers the id of a status revoke', async () => {
- const sendMessage = jest.fn().mockResolvedValue({ key: { id: 'REVOKE-1' } });
+ const sendMessage = jest
+ .fn()
+ .mockResolvedValueOnce({ key: { id: 'STATUS-1' } })
+ .mockResolvedValueOnce({ key: { id: 'REVOKE-1' } });
const { status, remembered } = makeStatus(sendMessage);
+ await status.postTextStatus('hello', { recipients: ['[email protected]'] });
await status.deleteStatus('STATUS-1');
- expect(remembered).toEqual(['REVOKE-1']);
+ expect(remembered).toEqual(['STATUS-1', 'REVOKE-1']);
});
it('survives a send that echoes nothing back, and hands on the nothing it found', async () => {
// The library can resolve a send with no message object at all. Reading the id off it must not
// throw, and the registry is what decides that an absent id is not worth remembering.
- const sendMessage = jest.fn().mockResolvedValue(undefined);
+ const sendMessage = jest
+ .fn()
+ .mockResolvedValueOnce({ key: { id: 'STATUS-1' } })
+ .mockResolvedValue(undefined);
const { status, remembered } = makeStatus(sendMessage);
+ await status.postTextStatus('hello', { recipients: ['[email protected]'] });
await expect(status.deleteStatus('STATUS-1')).resolves.not.toThrow();
- expect(remembered).toEqual([undefined]);
+ expect(remembered).toEqual(['STATUS-1', undefined]);
+ });
+});
+
+describe('BaileysStatus addresses a status revoke to the status recipients', () => {
+ // Baileys sends a status stanza to exactly its statusJidList, the revoke included: without one the
+ // revoke reaches nobody, the send still resolves, and the status stays up for every viewer.
+ it('sends the revoke to the recipients the status was posted to', async () => {
+ const sendMessage = jest.fn().mockResolvedValue({ key: { id: 'STATUS-1' } });
+ const { status } = makeStatus(sendMessage);
+ await status.postTextStatus('hello', { recipients: ['[email protected]', '[email protected]'] });
+
+ await status.deleteStatus('STATUS-1');
+
+ expect(sendMessage).toHaveBeenLastCalledWith(
+ 'status@broadcast',
+ { delete: { remoteJid: 'status@broadcast', fromMe: true, id: 'STATUS-1', participant: '[email protected]' } },
+ { statusJidList: ['[email protected]', '[email protected]'] },
+ );
+ });
+
+ it('refuses a status whose recipients it does not know rather than revoke it for nobody', async () => {
+ const sendMessage = jest.fn().mockResolvedValue({ key: { id: 'STATUS-1' } });
+ const { status } = makeStatus(sendMessage);
+ await status.postTextStatus('hello', { recipients: ['[email protected]'] });
+ const now = Date.now();
+ const clock = jest.spyOn(Date, 'now');
+ try {
+ await expect(status.deleteStatus('POSTED-ELSEWHERE')).rejects.toBeInstanceOf(EngineRefusedError);
+ clock.mockReturnValue(now + 25 * 3_600_000); // the posted one has expired by now
+ await expect(status.deleteStatus('STATUS-1')).rejects.toBeInstanceOf(EngineRefusedError);
+ } finally {
+ clock.mockRestore();
+ }
+ expect(sendMessage).toHaveBeenCalledTimes(1); // the post alone
});
});
diff --git a/src/engine/adapters/baileys-status.ts b/src/engine/adapters/baileys-status.ts
index 59c7b8b19..4516a11d7 100644
--- a/src/engine/adapters/baileys-status.ts
+++ b/src/engine/adapters/baileys-status.ts
@@ -2,6 +2,10 @@ import type { AnyMessageContent, WAMessage, WASocket } from '@whiskeysockets/bai
import { MediaInput, StatusPostOptions, StatusResult } from '../interfaces/whatsapp-engine.interface';
import { BadRequestException } from '@nestjs/common';
import { resolveMediaBuffer } from './baileys-messaging';
+import { EngineRefusedError } from '../../common/errors/engine-refused.error';
+
+/** How long WhatsApp keeps a status up. */
+const STATUS_TTL_MS = 24 * 3_600_000;
/**
* Status-domain operations extracted from BaileysAdapter. The adapter keeps the public
@@ -23,6 +27,9 @@ export interface BaileysStatusHost {
}
export class BaileysStatus {
+ /** The recipients of each status posted here, until it expires, oldest first. */
+ private readonly audiences = new Map<string, { jids: string[]; expiresAt: number }>();
+
constructor(private readonly host: BaileysStatusHost) {}
/** Post-ensureReady socket handle. */
@@ -71,17 +78,32 @@ export class BaileysStatus {
* key must be constructed from statusId alone (no messageStore lookup). The participant is the
* engine-dialect self JID (`<me>@s.whatsapp.net`). The revoke shape is empirically UNVERIFIED — the
* live spike only tested posting; if WhatsApp rejects it, fall back to EngineNotSupportedError.
+ *
+ * Baileys sends a status stanza, the revoke included, to exactly its `statusJidList`: without one
+ * the revoke reaches nobody, yet the send resolves and the status stays up for every viewer. Only
+ * the recipients of a status this adapter posted are known, so any other id is refused.
*/
async deleteStatus(statusId: string): Promise<void> {
this.host.ensureReady();
- const sent = await this.sock().sendMessage('status@broadcast', {
- delete: {
- remoteJid: 'status@broadcast',
- fromMe: true,
- id: statusId,
- participant: this.host.toEngineJid(this.host.normalizedSelfJid()),
+ const audience = this.audiences.get(statusId);
+ if (!audience || audience.expiresAt <= Date.now()) {
+ throw new EngineRefusedError(
+ `status ${statusId} was not posted by this session in the last 24 hours, so its recipients are ` +
+ 'unknown and the revoke cannot be addressed to them',
+ );
+ }
+ const sent = await this.sock().sendMessage(
+ 'status@broadcast',
+ {
+ delete: {
+ remoteJid: 'status@broadcast',
+ fromMe: true,
+ id: statusId,
+ participant: this.host.toEngineJid(this.host.normalizedSelfJid()),
+ },
},
- });
+ { statusJidList: audience.jids },
+ );
this.host.rememberOwnSend(sent?.key?.id);
}
@@ -106,6 +128,12 @@ export class BaileysStatus {
font: options.font,
});
this.host.rememberOwnSend(sent?.key?.id);
+ const now = Date.now();
+ for (const [id, audience] of this.audiences) {
+ if (audience.expiresAt > now) break;
+ this.audiences.delete(id);
+ }
+ if (sent?.key?.id) this.audiences.set(sent.key.id, { jids: statusJidList, expiresAt: now + STATUS_TTL_MS });
return this.toStatusResult(sent);
}
@@ -115,7 +143,7 @@ export class BaileysStatus {
return {
statusId: sent?.key?.id ?? '',
timestamp: ts,
- expiresAt: new Date(ts.getTime() + 24 * 3_600_000),
+ expiresAt: new Date(ts.getTime() + STATUS_TTL_MS),
};
}
}
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 97e2f8496..a21a2fcd8 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -6057,15 +6057,20 @@ describe('BaileysAdapter status posting', () => {
it('deleteStatus revokes by constructing the key from statusId (no store lookup)', async () => {
fakeSock.sendMessage.mockResolvedValue({ key: { id: 'STATUS1' } });
const adapter = await ready();
+ await adapter.postTextStatus('hello', { recipients: ['[email protected]'] });
await adapter.deleteStatus('STATUS1');
- expect(fakeSock.sendMessage).toHaveBeenCalledWith('status@broadcast', {
- delete: {
- remoteJid: 'status@broadcast',
- fromMe: true,
- id: 'STATUS1',
- participant: '[email protected]',
+ expect(fakeSock.sendMessage).toHaveBeenLastCalledWith(
+ 'status@broadcast',
+ {
+ delete: {
+ remoteJid: 'status@broadcast',
+ fromMe: true,
+ id: 'STATUS1',
+ participant: '[email protected]',
+ },
},
- });
+ { statusJidList: ['[email protected]'] },
+ );
expect(fakeStore.getMessage).not.toHaveBeenCalled();
});
});