Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): judge a change after the delivery it targets is stored
Yudhi Armyndharis06a1d5d1a4d3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4790238b4..65dfbe9bc 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -72,7 +72,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Bound the inbound media a PostgreSQL session holds before storing it to `INBOUND_MEDIA_CONCURRENCY` payloads, keeping each chat's arrival order.- Drop a Baileys edit, revoke or reaction from another chat, an edit from the wrong author, or a one-to-one revoke from the other side, aimed at a message still being downloaded, on SQLite too.- Drop a Baileys message that reuses the id of one still being downloaded under another chat or sender, so it cannot take the id or stand in for the original when a later delete is judged.-- Drop the stored copy of a Baileys message that reuses the id of a message the gateway already holds under another chat or sender, so that message's later edit, delete or reaction, once the delivery has been handled, is no longer ignored as foreign to it.+- Drop the stored copy of a Baileys message that reuses the id of a message the gateway already holds under another chat or sender, and hold a change that reaches it while that delivery is still being processed, so that message's edit, delete or reaction is no longer ignored as foreign to it.- Warn at storage export when a file is larger than the per-entry import cap `STORAGE_IMPORT_MAX_BYTES`.- Refuse a `PORT` with surrounding whitespace at boot instead of binding a port the image healthcheck cannot probe.- Skip or cancel the Baileys version lookup of a session stopped while it connects, so no proxy dispatcher outlives it.diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.tsindex 1c9867f4a..2aefdc6f7 100644--- a/src/engine/adapters/baileys-events.spec.ts+++ b/src/engine/adapters/baileys-events.spec.ts@@ -1294,6 +1294,69 @@ describe('BaileysEvents encrypted edits and reactions', () => {expect(onMessageEdited).toHaveBeenCalledWith(expect.objectContaining({ messageId: 'ORIG1', body: 'new text' }));});+ describe("another member's encrypted edit of an original still being processed", () => {+ // The original's own repeat check is the first read of the store. The store keeps what is put into it, so+ // a read made after the original was processed finds its copy.+ const withHeldReads = (held: number[], answered: Promise<{ conflict?: boolean } | undefined>) => {+ const opens = new Map<number, () => void>();+ const gates = new Map<number, Promise<void>>();+ for (const index of held) gates.set(index, new Promise<void>(resolve => opens.set(index, resolve)));+ let copy: WAMessage | null = null;+ let reads = 0;+ const setups = setup({+ getStoredMessage: async (id: string) => {+ if (id !== 'ORIG1') return null;+ const seen = copy;+ await gates.get(++reads);+ return seen;+ },+ putStoredMessage: (msg: WAMessage) => {+ copy = msg;+ return Promise.resolve();+ },+ getOnMessage: () => jest.fn(() => ({ settled: answered })),+ });+ return { ...setups, open: (index: number) => opens.get(index)!() };+ };+ const forged = (): WAMessage =>+ editEnvelope({+ participant: reactor,+ sealed: seal('Message Edit', { target: 'ORIG1', author, modifier: reactor }, editPlain),+ });++ it('is held until the original is stored, then left to the session while the original has no answer', async () => {+ const { events, onMessageEdited, open } = withHeldReads([1], new Promise(() => undefined));++ events.handleMessagesUpsert({ type: 'notify', messages: [original()] });+ await flush();+ events.handleMessagesUpsert({ type: 'notify', messages: [forged()] });+ await flush();+ expect(onMessageEdited).not.toHaveBeenCalled();++ open(1);+ await flush();+ expect(onMessageEdited).toHaveBeenCalledWith(expect.objectContaining({ messageId: 'ORIG1', body: 'new text' }));+ });++ it('is dropped once the original is stored and answered as the message', async () => {+ // Reads, in order: the original's repeat check, the reveal's, the guard's, and the guard's second.+ const answered = Promise.resolve({ conflict: false });+ const { events, onMessageEdited, recordMessageEdit, open } = withHeldReads([1, 4], answered);++ events.handleMessagesUpsert({ type: 'notify', messages: [original()] });+ await flush();+ events.handleMessagesUpsert({ type: 'notify', messages: [forged()] });+ await flush();+ open(1);+ await flush();+ open(4);+ await flush();++ expect(onMessageEdited).not.toHaveBeenCalled();+ expect(recordMessageEdit).not.toHaveBeenCalled();+ });+ });+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.tsindex 3d081be45..6ab5b9ad3 100644--- a/src/engine/adapters/baileys-events.ts+++ b/src/engine/adapters/baileys-events.ts@@ -336,11 +336,17 @@ export class BaileysEvents {* copy of such an id may be a duplicate the session is about to call another message's (the real one* lives in its table only), so a change that reads as foreign to it is the session's to judge, which* it does against the row (see targetsForeignMessage). An entry lasts at most OUTCOME_HOLD_MS. It begins- * when the delivery is handed on: a change that arrives while the delivery is still being processed- * (limiter wait, media download) is judged against the key it arrived under, as before.+ * when the delivery is handed on. A change that arrives while the delivery is still being processed+ * (limiter wait, repeat check, media download) and reads as foreign to its key waits for that processing+ * to end, then is judged again with this hold in place.*/private readonly outcomesPending = new Map<string, number>();+ /** How many changes wait for a delivery to be processed (see awaitDelivery); the rest are judged at once. */+ private changesParked = 0;++ private static readonly CHANGES_PARKED_LIMIT = 64;+/*** How many conflicting copies have been removed from the store. A read of the store that was out* while one went may have returned that copy, which is not what the store holds any more (see@@ -626,7 +632,9 @@ export class BaileysEvents {const pm = normalizedRoot?.protocolMessage;if (pm?.type === b.proto.Message.ProtocolMessage.Type.REVOKE) {// A group admin may revoke anyone's message, so only the chat is checked there.- if (await this.targetsForeignMessage(pm.key?.id, msg.key, !remoteJid.endsWith('@g.us'))) return;+ if (await this.targetsForeignMessage(pm.key?.id, msg.key, !remoteJid.endsWith('@g.us'), undefined, opts)) {+ return;+ }const from = msg.key.fromMe === true ? this.host.normalizedSelfJid() : chatJid;const to = msg.key.fromMe === true ? chatJid : this.host.normalizedSelfJid();const revoked: RevokedMessage = {@@ -659,7 +667,7 @@ export class BaileysEvents {return;}if (pm?.type === b.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT) {- if (await this.targetsForeignMessage(pm.key?.id, msg.key, true)) return;+ if (await this.targetsForeignMessage(pm.key?.id, msg.key, true, undefined, opts)) return;// MESSAGE_EDIT wraps the message's latest content. Normalize that INNER content separately// so captions, type, PTT, media presence and mentions describe the edited value rather than// the outer protocol envelope.@@ -723,7 +731,7 @@ export class BaileysEvents {// --- reactionMessage: don't emit onMessage ---if (contentType === 'reactionMessage') {const rm = normalizedRoot?.reactionMessage;- if (await this.targetsForeignMessage(rm?.key?.id, msg.key, false, 'reaction')) return;+ if (await this.targetsForeignMessage(rm?.key?.id, msg.key, false, 'reaction', opts)) return;const event: ReactionEvent = {messageId: rm?.key?.id ?? '',chatId: this.host.toNeutralJid(chatJid),@@ -926,16 +934,17 @@ export class BaileysEvents {* 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.+ * `processing` settles once that copy's processing has, and is set only when the copy is the in-flight one.*/private async readTarget(messageId: string,purpose: string,- ): Promise<{ key: WAMessageKey; message: WAMessage['message'] } | null> {+ ): Promise<{ key: WAMessageKey; message: WAMessage['message']; processing?: Promise<void> } | 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;+ return inFlight ? { key: inFlight.key, message: inFlight.message, processing: inFlight.done } : null;}/**@@ -1061,8 +1070,11 @@ export class BaileysEvents {* or, with `checkAuthor`, one somebody else sent. WhatsApp clients ignore such a message, but Baileys* emits it as-is and the projector updates the stored row by id alone, so a contact who knows an id* could rewrite or erase that message. A message still being processed is checked against the key- * it arrived under. Fails open: with no original at all, or ids that cannot be compared (see- * {@link differentWaIds}), the event goes through as it always has.+ * it arrived under, but only once its processing has ended when that key reads the change as foreign:+ * the delivery may be a duplicate of a message the session holds under another chat, and until it has+ * been stored its key proves nothing about which of the two the change names (see awaitDelivery).+ * Fails open: with no original at all, or ids that cannot be compared (see {@link differentWaIds}),+ * the event goes through as it always has.** A reaction to a message the account sent to a broadcast list is exempt: Baileys files the* own-device copy under the list jid (`<id>@broadcast`), while each recipient reacts from their 1:1@@ -1076,22 +1088,43 @@ export class BaileysEvents {key: WAMessageKey,checkAuthor: boolean,kind?: 'reaction',+ inbound?: { ticket?: InboundTicket; yieldSlot?: <R>(fn: () => Promise<R>) => Promise<R> },): Promise<boolean> {if (!targetId) return false;- for (let attempt = 0; ; attempt++) {+ // The key of the delivery in flight a wait was for, and the removals counted when it began.+ let waitedFor: { key: WAMessageKey; removals: number } | undefined;+ let reread = false;+ for (;;) {const removals = this.copyRemovals;// 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 = (await this.readTarget(targetId, 'checking what an edit, revoke or reaction targets'))?.key;+ const found = await this.readTarget(targetId, 'checking what an edit, revoke or reaction targets');+ // Nothing to read once the delivery was processed means it was never stored (no store, a failed+ // write), so the key it arrived under still stands, unless a conflicting copy went meanwhile: then+ // the delivery was not the message the id names.+ const original =+ found?.key ?? (waitedFor && this.copyRemovals === waitedFor.removals ? waitedFor.key : undefined);if (!original || !this.readsAsForeign(original, key, checkAuthor, kind)) return false;// The session has not said yet whether the delivery that stored this copy is the message it holds// under the id or a duplicate of another one. Its own judgement stands in: it holds the message and// checks the change against its key and against the stored row, and the stored copy does not.if (this.outcomesPending.has(targetId)) return false;+ // No copy is stored yet, only the key of the delivery being processed, and the real message may be+ // one the store never held (the session has it by row), so this key cannot say which of the two the+ // change names. Judged again once the delivery has been stored and handed on, which is when the+ // hold above and the removal of a conflicting copy answer it. Only a delivery the session reports+ // on counts: a status post is held by no entry there, and an own send gets no receipt.+ if (!waitedFor && found?.processing && original.remoteJid !== 'status@broadcast' && original.fromMe !== true) {+ waitedFor = { key: original, removals };+ if (await this.awaitDelivery(found.processing, inbound)) continue;+ }// A conflicting copy went while the store was read, and the read may have returned it: it ends the// hold only once removed, so the read is the one that can no longer be told from the real copy. Read// again, with the copy gone, rather than drop the real author's change against it.- if (attempt === 0 && this.copyRemovals !== removals) continue;+ if (!reread && this.copyRemovals !== removals) {+ reread = true;+ continue;+ }this.host.logger.warn('Dropping an edit, revoke or reaction aimed at a message from another chat or author', {msgId: key.id ?? 'unknown',targetId,@@ -1101,6 +1134,43 @@ export class BaileysEvents {}}+ /**+ * Park a change until the delivery it reads as foreign to has been processed: at most three media+ * download deadlines (a wait for room in the session, then the download), and never more than+ * CHANGES_PARKED_LIMIT changes at once. Resolves true once it did wait, false when it was not parked.+ * A parked change holds nothing the delivery needs. It gives up its inbound slot, which the delivery+ * may have to take back after waiting for room, and its place in its chat's order, which a change+ * never uses (it is not announced as a message), so it neither starves the delivery nor holds back the+ * messages that follow it in the chat.+ */+ private async awaitDelivery(+ done: Promise<void>,+ inbound?: { ticket?: InboundTicket; yieldSlot?: <R>(fn: () => Promise<R>) => Promise<R> },+ ): Promise<boolean> {+ if (this.changesParked >= BaileysEvents.CHANGES_PARKED_LIMIT) return false;+ this.changesParked++;+ try {+ inbound?.ticket?.drop();+ const wait = (): Promise<void> =>+ new Promise<void>(resolve => {+ let wake: (() => void) | null = resolve;+ // Whichever comes first; the reaction left on a delivery that never settles holds nothing.+ const end = (): void => {+ clearTimeout(timer);+ wake?.();+ wake = null;+ };+ const timer = setTimeout(end, 3 * inboundMediaTimeoutMs());+ timer.unref();+ done.then(end, end);+ });+ await (inbound?.yieldSlot ? inbound.yieldSlot(wait) : wait());+ return true;+ } finally {+ this.changesParked--;+ }+ }+/** Whether a change arriving under `key` names a message other than the one `original` is. */private readsAsForeign(original: WAMessageKey, key: WAMessageKey, checkAuthor: boolean, kind?: 'reaction'): boolean {const broadcast = kind === 'reaction' && !!original.remoteJid?.endsWith('@broadcast');diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 256005caf..bcf3a2221 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -162,6 +162,7 @@ import { NotFoundException, BadRequestException, ForbiddenException, HttpExceptiimport { SocksProxyAgent } from 'socks-proxy-agent';import { Dispatcher1Wrapper } from 'undici';import { BaileysAdapter, createProxyAgent } from './baileys.adapter';+import { BaileysEvents } from './baileys-events';import {EditedMessage,EngineStatus,@@ -9421,6 +9422,292 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'expect(onMessageEdited).toHaveBeenCalledTimes(1); // now judged against the copy again});+ // The reusing delivery is not stored, and its receipt does not exist, until its processing (the limiter+ // queue, the repeat check, a media download) is over. A change that arrives meanwhile can only be judged+ // against the key the delivery arrived under, which says nothing of which message the id names.+ describe('a change that arrives while the delivery is still being processed', () => {+ const THIRD = '[email protected]';+ const dropped = 'Dropping an edit, revoke or reaction aimed at a message from another chat or author';+ const WAIT_MS = 3 * 30_000;+ type Kind = 'edit' | 'delete' | 'reaction';+ const kinds: Kind[] = ['edit', 'delete', 'reaction'];++ /**+ * Holds the reads of the store the given calls make (counted from 1, in the order they are made), each+ * answering what the copy was when it began. The first read is the delivery's repeat check.+ */+ const holdReads = (indexes: number[]): ((index: number) => void) => {+ const opens = new Map<number, () => void>();+ const gates = new Map<number, Promise<void>>();+ for (const index of indexes) gates.set(index, new Promise<void>(resolve => opens.set(index, resolve)));+ let call = 0;+ fakeStore.getMessage.mockImplementation(async (_session: string, id: string) => {+ const seen = structuredClone(copies.get(id) ?? null);+ await gates.get(++call);+ return seen;+ });+ return index => opens.get(index)!();+ };++ const changeFrom = (kind: Kind, remoteJid: string): void => {+ const key = { id: 'CHANGE_ENV', remoteJid, fromMe: false };+ if (kind === 'edit') {+ fireProtocol(key, { key: { id: 'REUSED' }, type: 14, editedMessage: { conversation: 'the real edit' } });+ } else if (kind === 'delete') {+ fireProtocol(key, { key: { id: 'REUSED' }, type: 0 });+ } else {+ fakeSock.fire('messages.upsert', {+ type: 'notify',+ messages: [+ {+ key,+ message: { reactionMessage: { key: { id: 'REUSED' }, text: '👍' } },+ messageTimestamp: 1700000101,+ },+ ],+ });+ }+ };++ const sessionCallbacks = () => ({+ onMessageEdited: jest.fn(),+ onMessageRevoked: jest.fn(),+ onMessageReaction: jest.fn(),+ });+ const handedOn = (callbacks: ReturnType<typeof sessionCallbacks>, kind: Kind): jest.Mock =>+ ({+ edit: callbacks.onMessageEdited,+ delete: callbacks.onMessageRevoked,+ reaction: callbacks.onMessageReaction,+ })[kind];++ it.each(kinds)('holds a real %s, and hands it to the session once the delivery is stored', async kind => {+ const answered = answer();+ const callbacks = sessionCallbacks();+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), ...callbacks });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ const open = holdReads([1]);+ deliverReuse();+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);++ changeFrom(kind, PEER);+ await settleAll();+ expect(handedOn(callbacks, kind)).not.toHaveBeenCalled();+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());++ open(1);+ await settleAll();+ expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);+ expect(handedOn(callbacks, kind)).toHaveBeenCalledTimes(1);++ answered.resolve({ conflict: true });+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);+ });++ it.each(kinds)('lands a real %s when the session reports the conflict before it is judged again', async kind => {+ const answered = answer();+ const callbacks = sessionCallbacks();+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), ...callbacks });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ // The change's second read, made once the delivery is stored, still sees the copy the answer removes.+ const open = holdReads([1, 3]);+ deliverReuse();+ await settleAll();+ changeFrom(kind, PEER);+ await settleAll();+ open(1);+ await settleAll();+ expect(handedOn(callbacks, kind)).not.toHaveBeenCalled();++ answered.resolve({ conflict: true });+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);+ open(3);+ await settleAll();++ expect(handedOn(callbacks, kind)).toHaveBeenCalledTimes(1);+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());+ });++ it.each(kinds.flatMap(kind => [PEER, THIRD].map(from => [kind, from] as const)))(+ 'drops a %s from %s once the delivery is stored and the session found it was the message',+ async (kind, from) => {+ const answered = answer();+ const callbacks = sessionCallbacks();+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), ...callbacks });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ const open = holdReads([1, 3]);+ deliverReuse();+ await settleAll();+ changeFrom(kind, from);+ await settleAll();+ open(1);+ await settleAll();++ // The answer comes before the change is judged against the stored copy again.+ answered.resolve({ conflict: false });+ await settleAll();+ open(3);+ await settleAll();++ expect(handedOn(callbacks, kind)).not.toHaveBeenCalled();+ expect(warn).toHaveBeenCalledWith(dropped, expect.objectContaining({ targetId: 'REUSED' }));+ expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);+ },+ );++ it('leaves a change from a third chat to the session while it has not answered, which judges it by its row', async () => {+ const callbacks = sessionCallbacks();+ const answered = answer();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), ...callbacks });+ const open = holdReads([1]);+ deliverReuse();+ await settleAll();+ changeFrom('edit', THIRD);+ await settleAll();+ open(1);+ await settleAll();++ expect(callbacks.onMessageEdited).toHaveBeenCalledTimes(1);+ });++ it('does not hold an edit from the chat the delivery arrived in', async () => {+ const callbacks = sessionCallbacks();+ await startWith({ onMessage: jest.fn(() => ({ settled: answer().promise })), ...callbacks });+ holdReads([1]);+ deliverReuse();+ await settleAll();++ fireProtocol(+ { id: 'OWN_EDIT_ENV', remoteJid: OTHER, fromMe: false },+ { key: { id: 'REUSED' }, type: 14, editedMessage: { conversation: 'late, edited' } },+ );+ await settleAll();++ expect(callbacks.onMessageEdited).toHaveBeenCalledTimes(1);+ });++ it.each([+ ['a status post', { remoteJid: 'status@broadcast', participant: OTHER, fromMe: false }],+ ['a message the account sent', { remoteJid: OTHER, fromMe: true }],+ ])('judges a change at once against %s still being processed, as no answer can come for it', async (_, key) => {+ const callbacks = sessionCallbacks();+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answer().promise })), ...callbacks });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ holdReads([1]);+ deliverReuse(key);+ await settleAll();++ changeFrom('edit', PEER);+ await settleAll();++ expect(callbacks.onMessageEdited).not.toHaveBeenCalled();+ expect(warn).toHaveBeenCalledWith(dropped, expect.objectContaining({ targetId: 'REUSED' }));+ });++ it('gives up its place in the chat order while it waits, as no change is ever announced there', async () => {+ const ticket = () => ({ reserve: jest.fn(() => false), holdUntil: jest.fn(), drop: jest.fn(), revoked: false });+ const delivery = ticket();+ const change = ticket();+ const tickets = [delivery, change];+ await startWith({+ admitInbound: jest.fn(() => tickets.shift()),+ onMessage: jest.fn(() => ({ settled: answer().promise })),+ onMessageEdited: jest.fn(),+ });+ const open = holdReads([1]);+ deliverReuse();+ await settleAll();+ realEdit();+ await settleAll();++ expect(change.drop).toHaveBeenCalledTimes(1);+ expect(delivery.drop).not.toHaveBeenCalled();+ open(1);+ await settleAll();+ });++ describe('that does not finish', () => {+ beforeEach(() => {+ jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate'] });+ });++ afterEach(() => {+ jest.useRealTimers();+ });++ it('drops the change after three download deadlines, and nothing waits on the delivery then', async () => {+ const callbacks = sessionCallbacks();+ const adapter = await startWith({+ onMessage: jest.fn(() => ({ settled: answer().promise })),+ ...callbacks,+ });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ const idle = jest.getTimerCount();+ const open = holdReads([1]);+ deliverReuse();+ await settleAll();+ realEdit();+ await settleAll();++ jest.advanceTimersByTime(WAIT_MS - 1);+ await settleAll();+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());++ jest.advanceTimersByTime(1);+ await settleAll();+ expect(warn).toHaveBeenCalledWith(dropped, expect.objectContaining({ targetId: 'REUSED' }));+ expect(callbacks.onMessageEdited).not.toHaveBeenCalled();++ // The delivery finishes after all, and its timer for the change is already gone.+ open(1);+ await settleAll();+ expect(callbacks.onMessageEdited).not.toHaveBeenCalled();+ expect(jest.getTimerCount()).toBe(idle + 1); // the delivery's own hold on its answer+ });++ it('parks only so many changes, judging the rest at once, and frees every timer', async () => {+ const limit = (BaileysEvents as unknown as { CHANGES_PARKED_LIMIT: number }).CHANGES_PARKED_LIMIT;+ const callbacks = sessionCallbacks();+ const answered = answer();+ const adapter = await startWith({+ onMessage: jest.fn(() => ({ settled: answered.promise })),+ ...callbacks,+ });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ const idle = jest.getTimerCount();+ const open = holdReads([1]);+ deliverReuse();+ await settleAll();++ const flood = limit + 6;+ fakeSock.fire('messages.upsert', {+ type: 'notify',+ messages: Array.from({ length: flood }, (_, i) => ({+ key: { id: `FLOOD_${i}`, remoteJid: PEER, fromMe: false },+ message: {+ protocolMessage: { key: { id: 'REUSED' }, type: 14, editedMessage: { conversation: `edit ${i}` } },+ },+ messageTimestamp: 1700000100,+ })),+ });+ await settleAll();+ expect(warn.mock.calls.filter(([message]) => message === dropped)).toHaveLength(6);+ expect(jest.getTimerCount() - idle).toBe(limit);++ open(1);+ await settleAll();+ expect(callbacks.onMessageEdited).toHaveBeenCalledTimes(limit);++ answered.resolve({ conflict: true });+ await settleAll();+ expect(jest.getTimerCount()).toBe(idle);+ });+ });+ });+describe('an answer that does not come', () => {const HOLD_MS = 5 * 60_000;const edited = 'Dropping an edit, revoke or reaction aimed at a message from another chat or author';diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.tsindex 5e0515e61..55638922a 100644--- a/src/engine/adapters/inbound-hold.spec.ts+++ b/src/engine/adapters/inbound-hold.spec.ts@@ -36,15 +36,16 @@ const sleep = (ms: number): Promise<void> => new Promise(resolve => setTimeout(r/*** The real projector over a stand-in `messages` table whose inserts a test can stall per message. With- * `unique`, an insert of an id the table already holds fails as the real table's unique index does.+ * `unique`, an insert of an id the table already holds fails as the real table's unique index does. With+ * `sqlite`, it is wired as SQLite is: no message is admitted before its emit.*/-function heldProjector(options: { unique?: boolean } = {}) {+function heldProjector(options: { unique?: boolean; sqlite?: boolean } = {}) {const rows: Row[] = [];const hookStarts: string[] = [];const hookedBodies: Record<string, string> = {};const stalls = new Map<string, Promise<void>>();const repo = {- manager: { connection: { options: { type: 'postgres' } } },+ manager: { connection: { options: { type: options.sqlite ? 'better-sqlite3' : 'postgres' } } },find: jest.fn().mockResolvedValue([]),findOne: jest.fn(({ where }: { where: Row }) =>Promise.resolve(rows.find(r => r.waMessageId === where.waMessageId) ?? null),@@ -97,11 +98,15 @@ function heldProjector(options: { unique?: boolean } = {}) {);// The same table SessionEngineEventWiring builds on PostgreSQL.const callbacksFor = (e: IWhatsAppEngine): EngineEventCallbacks => ({- admitInbound: a => projector.admitInbound('s1', e, a),+ // Only PostgreSQL holds a message from its arrival.+ ...(options.sqlite+ ? {}+ : { admitInbound: (a: Parameters<MessageProjector['admitInbound']>[2]) => projector.admitInbound('s1', e, a) }),onMessage: (m, t) => projector.handleInboundMessage('s1', e, m, t),onMessageCreate: (m, t) => projector.handleOwnSendEcho('s1', e, m, t),- onMessageRevoked: m => projector.handleMessageRevoked('s1', e, m),+ onMessageRevoked: (m, actor) => projector.handleMessageRevoked('s1', e, m, actor),onMessageEdited: m => projector.applyMessageEditQueued('s1', m),+ onMessageReaction: r => projector.applyReactionQueued('s1', r),});const callbacks = callbacksFor(engine);const stall = (id: string): (() => void) => {@@ -113,6 +118,8 @@ function heldProjector(options: { unique?: boolean } = {}) {(webhookService.dispatch.mock.calls as Array<[string, string, Row]>).filter(c => c[1] === 'message.received').map(c => String(c[2].id));+ const announcedAs = (event: string): Row[] =>+ (webhookService.dispatch.mock.calls as Array<[string, string, Row]>).filter(c => c[1] === event).map(c => c[2]);return {projector,rows,@@ -123,6 +130,7 @@ function heldProjector(options: { unique?: boolean } = {}) {engines,stall,announced,+ announcedAs,ingested,inserted: () => rows.map(r => r.waMessageId),};@@ -421,8 +429,8 @@ describe('Baileys inbound messages held for storage', () => {});it.each([- ['from another chat', { remoteJid: '[email protected]', fromMe: false }],- ['from the wrong author', { remoteJid: '[email protected]', fromMe: true }],+ ['from another chat', { remoteJid: '[email protected]', fromMe: false }],+ ['from the wrong author', { remoteJid: '[email protected]', fromMe: true }],])('ignores a delete and an edit %s aimed at media waiting for room', async (_, envelope) => {process.env.INBOUND_MEDIA_CONCURRENCY = '1';const h = heldProjector();@@ -431,8 +439,8 @@ describe('Baileys inbound messages held for storage', () => {upsert(events,media('H', '[email protected]'),- media('MREV', '[email protected]', { caption: 'real caption' }),- media('MEDIT', '[email protected]', { caption: 'real caption 2' }),+ media('MREV', '[email protected]', { caption: 'real caption' }),+ media('MEDIT', '[email protected]', { caption: 'real caption 2' }),);await flush();const target = (id: string) => ({ id, remoteJid: envelope.remoteJid, fromMe: false });@@ -473,8 +481,9 @@ describe('Baileys inbound messages held for storage', () => {let release: () => void;let callbacks: EngineEventCallbacks;if (admitted) {+ // The session judges what the engine hands it, as it does in production.release = h.stall('H');- callbacks = { ...h.callbacks, onMessageReaction };+ callbacks = h.callbacks;} else {// No admission callback, as SQLite wires the engine: M is held only by its own download.release = () => download.resolve();@@ -482,7 +491,7 @@ describe('Baileys inbound messages held for storage', () => {callbacks = { onMessage: jest.fn(), onMessageReaction };}const { events } = build(4, callbacks);- const chat = '[email protected]';+ const chat = '[email protected]';upsert(events, ...(admitted ? [media('H', '[email protected]')] : []), media('M', chat));await flush();const react = (id: string, remoteJid: string, text: string): WAMessage => ({@@ -490,12 +499,13 @@ describe('Baileys inbound messages held for storage', () => {messageTimestamp: 1_700_000_001,message: { reactionMessage: { key: { id: 'M', remoteJid, fromMe: false }, text } },});- upsert(events, react('R1', '[email protected]', 'forged'), react('R2', chat, 'genuine'));+ upsert(events, react('R1', '[email protected]', 'forged'), react('R2', chat, 'genuine'));await flush();release();await flush();- expect(reactions).toEqual([expect.objectContaining({ messageId: 'M', chatId: chat, reaction: 'genuine' })]);+ const reached = admitted ? h.announcedAs('message.reaction') : reactions;+ expect(reached).toEqual([expect.objectContaining({ messageId: 'M', chatId: chat, reaction: 'genuine' })]);});it('neither ingests nor emits a status post deleted while it waits for room', async () => {@@ -761,6 +771,195 @@ describe('Baileys inbound messages held for storage', () => {expect(h.rows).toEqual([seeded()]);});++ // The delivery is not stored, and the session has no receipt for it, until its processing is over (the+ // limiter queue, the repeat check, a download). A change that arrives meanwhile is judged against the key+ // the delivery arrived under, which cannot say which message the id names.+ describe('while the delivery is still being processed', () => {+ type Kind = 'edit' | 'delete' | 'reaction';+ const CHAT_C = '[email protected]';++ /**+ * A store whose reads of X wait until released, counted from 1 in the order they are made. Each answers+ * what the store held when it began; the first is the delivery's repeat check.+ */+ const holdingReads = (held: number[]) => {+ const store = memoryStore();+ const read = store.getStoredMessage;+ const gates = new Map<number, ReturnType<typeof deferred<void>>>();+ for (const n of held) gates.set(n, deferred<void>());+ let reads = 0;+ store.getStoredMessage = async (id: string) => {+ if (id !== 'X') return read(id);+ const n = ++reads;+ const seen = await read(id);+ await gates.get(n)?.promise;+ return seen;+ };+ return { store, open: (n: number) => gates.get(n)!.resolve() };+ };++ const change = (kind: Kind, chat: string, envelopeId: string): WAMessage => {+ if (kind === 'edit') return editFrom(chat, envelopeId, 'fixed');+ const target = { id: 'X', remoteJid: chat, fromMe: false };+ return {+ key: { id: envelopeId, remoteJid: chat, fromMe: false },+ messageTimestamp: 1_700_000_002,+ message:+ kind === 'delete'+ ? { protocolMessage: { type: 0, key: target } }+ : { reactionMessage: { key: target, text: 'ok' } },+ };+ };++ /** What the change has done to the session: the stored body, the stored type, or the reactions announced. */+ const effect = (h: ReturnType<typeof heldProjector>, kind: Kind): unknown => {+ const row = h.rows.find(r => r.waMessageId === 'X');+ if (kind === 'edit') return row?.body;+ if (kind === 'delete') return row?.type;+ return h.announcedAs('message.reaction').map(r => r.messageId);+ };++ /** The seeded row says 'the real one' and has no type; a delivery that is the message stores 'text X'. */+ const untouched = (kind: Kind, seededRow: boolean): unknown => {+ if (kind === 'reaction') return [];+ if (kind === 'edit') return seededRow ? 'the real one' : 'text X';+ return seededRow ? undefined : 'text';+ };+ const landed: Record<Kind, unknown> = { edit: 'fixed', delete: 'revoked', reaction: ['X'] };+ const kinds = ['edit', 'delete', 'reaction'] as const;++ describe.each([+ ['PostgreSQL', false],+ ['SQLite', true],+ ])('on %s', (_, sqlite) => {+ it.each(kinds.flatMap(kind => [true, false].map(stall => [kind, stall] as const)))(+ 'lands the real %s once the delivery is refused (answer held: %s)',+ async (kind, stall) => {+ const h = heldProjector({ unique: true, sqlite });+ h.rows.push(seeded());+ const release = stall ? h.stall('X') : undefined;+ const { store, open } = holdingReads([1]);+ const { events } = build(4, h.callbacks, store);++ upsert(events, text('X', CHAT_B));+ await flush();+ upsert(events, change(kind, CHAT_A, 'REAL_ENV'));+ await flush();+ expect(effect(h, kind)).toEqual(untouched(kind, true));++ open(1);+ await flush();+ release?.();+ await flush();++ expect(effect(h, kind)).toEqual(landed[kind]);+ expect(store.copies.has('X')).toBe(false);+ expect(h.announced()).toEqual([]);+ },+ );++ // The delivery's answer is pending when the change is handed on, so only the session can drop it:+ // by the entry it holds the delivery under, and by the row once there is one.+ it.each(kinds.flatMap(kind => [true, false].map(conflicts => [kind, conflicts] as const)))(+ 'leaves a forged %s from a third chat to the session, which drops it (delivery conflicts: %s)',+ async (kind, conflicts) => {+ const h = heldProjector({ unique: true, sqlite });+ // Without a row of another message, the delivery is the message the id names.+ if (conflicts) h.rows.push(seeded());+ const release = h.stall('X');+ const { store, open } = holdingReads([1]);+ const { events } = build(4, h.callbacks, store);++ upsert(events, text('X', CHAT_B));+ await flush();+ upsert(events, change(kind, CHAT_C, 'FORGED_ENV'));+ await flush();+ open(1);+ await flush();+ expect(h.announcedAs('message.edited')).toEqual([]);+ expect(h.announcedAs('message.revoked')).toEqual([]);+ release();+ await flush();++ expect(effect(h, kind)).toEqual(untouched(kind, conflicts));+ expect(h.announcedAs('message.edited')).toEqual([]);+ expect(h.announcedAs('message.revoked')).toEqual([]);+ },+ );++ // The answer comes before the change is judged again, so the adapter has the copy (the delivery is+ // the message) or nothing (it conflicted and the copy was removed) to judge it by.+ it.each(kinds.flatMap(kind => [true, false].map(conflicts => [kind, conflicts] as const)))(+ 'drops a forged %s from a third chat judged after the answer (delivery conflicts: %s)',+ async (kind, conflicts) => {+ const h = heldProjector({ unique: true, sqlite });+ if (conflicts) h.rows.push(seeded());+ const { store, open } = holdingReads([1, 3]);+ const { events } = build(4, h.callbacks, store);++ upsert(events, text('X', CHAT_B));+ await flush();+ upsert(events, change(kind, CHAT_C, 'FORGED_ENV'));+ await flush();+ open(1);+ await flush();+ open(3);+ await flush();++ expect(effect(h, kind)).toEqual(untouched(kind, conflicts));+ expect(h.announcedAs('message.edited')).toEqual([]);+ expect(h.announcedAs('message.revoked')).toEqual([]);+ // The copy of a delivery that is the message stays; a conflicting one is gone.+ expect(store.copies.has('X')).toBe(!conflicts);+ },+ );+ });++ it('lets the delivery take its limiter slot back when its room frees, while the change waits', async () => {+ process.env.INBOUND_MEDIA_CONCURRENCY = '1';+ process.env.MEDIA_DOWNLOAD_TIMEOUT_MS = '1000';+ const h = heldProjector({ unique: true });+ h.rows.push(seeded());+ const { events, emitted } = build(1, h.callbacks, memoryStore());+ const release = h.stall('H');++ upsert(events, media('H', '[email protected]'));+ await flush();+ // X asks for the room H holds until its row is written, and gives its only limiter slot up meanwhile.+ upsert(events, media('X', CHAT_B));+ await flush();+ upsert(events, change('edit', CHAT_A, 'REAL_ENV'));+ await flush();+ expect(emitted).toEqual(['H']);+ expect(h.rows).toEqual([seeded()]);++ release();+ await flush();+ expect(emitted).toEqual(['H', 'X']);+ expect(h.rows.find(r => r.waMessageId === 'X')).toEqual(+ expect.objectContaining({ chatId: CHAT_A, body: 'fixed' }),+ );+ });++ it('does not hold back the later messages of its own chat', async () => {+ const h = heldProjector({ unique: true });+ h.rows.push(seeded());+ const { store, open } = holdingReads([1]);+ const { events } = build(4, h.callbacks, store);++ upsert(events, text('X', CHAT_B));+ await flush();+ upsert(events, change('edit', CHAT_A, 'REAL_ENV'), text('F', CHAT_A));+ await flush();++ expect(h.hookStarts).toContain('F');+ expect(h.inserted()).toContain('F');+ open(1);+ await flush();+ expect(h.rows[0]).toEqual(expect.objectContaining({ waMessageId: 'X', body: 'fixed' }));+ });+ });});});