Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): find an original that finishes during an encrypted read

Yudhi Armyndharis012714a8a2b7
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.ts
index 1af5bb9cb..43786ad43 100644
--- a/src/engine/adapters/baileys-events.spec.ts
+++ b/src/engine/adapters/baileys-events.spec.ts
@@ -1178,6 +1178,29 @@ describe('BaileysEvents encrypted edits and reactions', () => {
await flush();
});
+ it('reads the edit of an original that finishes while the edit is still reading the store', async () => {
+ // The edit's read of the store is issued while the original is in flight, and answers (null: the row
+ // was written after the read began) only once the original has finished and left the in-flight map.
+ const reads: Array<(stored: WAMessage | null) => void> = [];
+ // Only these two reads are held; any later read of the id (the guard that follows) answers at once.
+ 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 });
+
+ events.handleMessagesUpsert({ type: 'notify', messages: [original()] });
+ await flush();
+ events.handleMessagesUpsert({ type: 'notify', messages: [editEnvelope()] });
+ await flush();
+ expect(reads).toHaveLength(2);
+
+ reads[0](null); // the original's own repeat-delivery check answers: it is stored and done
+ await flush();
+ reads[1](null); // now the edit's read answers, finding no row and no in-flight entry
+ 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 ff17ecb24..f753165d5 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -852,11 +852,14 @@ 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 = this.inboundInFlight.get(targetId);
+ const inFlight = processing ?? this.inboundInFlight.get(targetId);
const original = stored ?? (inFlight ? { key: inFlight.key, message: inFlight.message } : undefined);
const secret = messageSecretOf(original?.message);
if (!original || !secret) {