Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): check an edit's author against an original still in flight

Yudhi Armyndharisd8a76139ba16
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.ts
index 43786ad43..b64253966 100644
--- a/src/engine/adapters/baileys-events.spec.ts
+++ b/src/engine/adapters/baileys-events.spec.ts
@@ -1201,6 +1201,80 @@ describe('BaileysEvents encrypted edits and reactions', () => {
expect(onMessageEdited).toHaveBeenCalledWith(expect.objectContaining({ messageId: 'ORIG1', body: 'new text' }));
});
+ it("drops another member's encrypted edit of an original that finishes while the guard reads the store", async () => {
+ // Held reads of the target, in order: the original's own repeat check, the reveal's read, the guard's.
+ const reads: Array<(stored: WAMessage | null) => void> = [];
+ const getStoredMessage = (id: string): Promise<WAMessage | null> =>
+ id === 'ORIG1' && reads.length < 3 ? new Promise(resolve => reads.push(resolve)) : Promise.resolve(null);
+ const { events, onMessageEdited, recordMessageEdit } = setup({ getStoredMessage });
+ // Another member of the group, who holds the message's secret as a recipient of it, seals an edit.
+ const forged = editEnvelope({
+ participant: reactor,
+ sealed: seal('Message Edit', { target: 'ORIG1', author, modifier: reactor }, editPlain),
+ });
+
+ events.handleMessagesUpsert({ type: 'notify', messages: [original()] });
+ await flush();
+ events.handleMessagesUpsert({ type: 'notify', messages: [forged] });
+ await flush();
+ expect(reads).toHaveLength(2);
+
+ reads[1](null); // the reveal reads nothing and decrypts from the copy in flight
+ await flush();
+ expect(reads).toHaveLength(3); // the guard's read is out now
+ reads[0](null); // the original finishes and leaves the in-flight map while it is
+ await flush();
+ reads[2](null);
+ await flush();
+
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ expect(recordMessageEdit).not.toHaveBeenCalled();
+ });
+
+ it('holds a plain edit from another member to the same rule when the original finishes during the read', async () => {
+ const reads: Array<(stored: WAMessage | null) => void> = [];
+ const getStoredMessage = (id: string): Promise<WAMessage | null> =>
+ id === 'ORIG1' && reads.length < 2 ? new Promise(resolve => reads.push(resolve)) : Promise.resolve(null);
+ const { events, onMessageEdited } = setup({ getStoredMessage });
+ const plain = {
+ key: { id: 'ENV2', remoteJid: group, participant: reactor, fromMe: false },
+ messageTimestamp: 1_786_000_100,
+ message: { protocolMessage: editPlain.protocolMessage },
+ } as unknown as WAMessage;
+
+ events.handleMessagesUpsert({ type: 'notify', messages: [original()] });
+ await flush();
+ events.handleMessagesUpsert({ type: 'notify', messages: [plain] });
+ await flush();
+ expect(reads).toHaveLength(2); // the original's check and the guard's read
+ reads[0](null);
+ await flush();
+ reads[1](null);
+ await flush();
+
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ });
+
+ it("still applies the author's own encrypted edit in the same interleaving", async () => {
+ const reads: Array<(stored: WAMessage | null) => void> = [];
+ const getStoredMessage = (id: string): Promise<WAMessage | null> =>
+ id === 'ORIG1' && reads.length < 3 ? new Promise(resolve => reads.push(resolve)) : Promise.resolve(null);
+ const { events, onMessageEdited } = setup({ getStoredMessage });
+
+ events.handleMessagesUpsert({ type: 'notify', messages: [original()] });
+ await flush();
+ events.handleMessagesUpsert({ type: 'notify', messages: [editEnvelope()] });
+ await flush();
+ reads[1](null);
+ await flush();
+ reads[0](null);
+ await flush();
+ reads[2](null);
+ await flush();
+
+ expect(onMessageEdited).toHaveBeenCalledWith(expect.objectContaining({ messageId: 'ORIG1', body: 'new text' }));
+ });
+
describe('leaves an encrypted edit unapplied when', () => {
it.each([
['another member sealed it for the message', () => editEnvelope({ participant: reactor })],
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index f753165d5..b3e9a9e1d 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -371,11 +371,11 @@ export class BaileysEvents {
async applyHistoryRevoke(target: WAMessageKey, envelope: WAMessageKey): Promise<WAMessageKey | undefined> {
if (!target.id) return undefined;
const generation = this.storeGeneration;
- const stored = await this.readStoredMessage(target.id, 'checking what a history revoke targets');
+ const found = await this.readTarget(target.id, 'checking what a history revoke targets');
if (generation !== this.storeGeneration) return undefined;
- const original = stored?.key ?? this.inboundInFlight.get(target.id)?.key ?? target;
+ const original = found?.key ?? target;
if (!this.mayChange(original, envelope, true)) return undefined;
- if (stored || this.inboundInFlight.has(target.id)) this.markDeletedForEveryone(target.id);
+ if (found) this.markDeletedForEveryone(target.id);
this.changeStoredMessage(target.id, copy =>
this.mayChange(copy.key, envelope, true) ? { ...copy, message: null } : null,
);
@@ -807,6 +807,22 @@ export class BaileysEvents {
* which is the price paid here deliberately, because a message nobody ever hears about cannot be
* recovered at all. The persist side of the same store is already written this way.
*/
+ /**
+ * What an edit, revoke or reaction targets: the stored copy, or the one still being processed. The
+ * in-flight copy is taken BEFORE the store is read, because an original that finishes while the read is
+ * out has left the map and may have been written after the read began, which would find it in neither.
+ */
+ private async readTarget(
+ messageId: string,
+ purpose: string,
+ ): Promise<{ key: WAMessageKey; message: WAMessage['message'] } | null> {
+ const processing = this.inboundInFlight.get(messageId);
+ const stored = await this.readStoredMessage(messageId, purpose);
+ if (stored) return { key: stored.key, message: stored.message };
+ const inFlight = processing ?? this.inboundInFlight.get(messageId);
+ return inFlight ? { key: inFlight.key, message: inFlight.message } : null;
+ }
+
private async readStoredMessage(messageId: string, purpose: string): Promise<WAMessage | null> {
try {
return (await this.host.getStoredMessage(messageId)) ?? null;
@@ -852,15 +868,7 @@ export class BaileysEvents {
const editType = b.proto.Message.SecretEncryptedMessage.SecretEncType.MESSAGE_EDIT;
if (type !== editType && type !== 'MESSAGE_EDIT') return undefined;
}
- // Looked up before the store is read: an original that finishes while the read is out leaves the
- // in-flight map and, if its row was written after the read began, is in neither place.
- const processing = this.inboundInFlight.get(targetId);
- const stored = await this.readStoredMessage(
- targetId,
- 'reading the message an encrypted edit or reaction targets',
- );
- const inFlight = processing ?? this.inboundInFlight.get(targetId);
- const original = stored ?? (inFlight ? { key: inFlight.key, message: inFlight.message } : undefined);
+ const original = await this.readTarget(targetId, 'reading the message an encrypted edit or reaction targets');
const secret = messageSecretOf(original?.message);
if (!original || !secret) {
this.host.logger.debug('Cannot read an encrypted edit or reaction: the message it targets is not stored', {
@@ -946,8 +954,7 @@ export class BaileysEvents {
// A message still being processed has no stored copy yet, but the projector may already hold it
// (PostgreSQL), so its own key is checked instead.
const original = targetId
- ? ((await this.readStoredMessage(targetId, 'checking what an edit, revoke or reaction targets'))?.key ??
- this.inboundInFlight.get(targetId)?.key)
+ ? (await this.readTarget(targetId, 'checking what an edit, revoke or reaction targets'))?.key
: undefined;
if (!original) return false;
const broadcast = kind === 'reaction' && !!original.remoteJid?.endsWith('@broadcast');