Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): read the proxy dispatcher only for a download that can run

Yudhi Armyndharis30f42a2d57b5
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index e2d0ef2cf..ade86bf47 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -1421,9 +1421,6 @@ export class BaileysEvents {
msg: WAMessage,
maxBytes: number,
): Promise<Buffer | { overflowBytes: number } | null> {
- // A proxied session must not fetch media around its proxy (#859). Baileys reads the dispatcher
- // from the nested `options`; a top-level one is ignored.
- const dispatcher = this.host.getFetchDispatcher();
// Hold the stream handle in the outer scope so the timeout can destroy it. A genuine
// download/read error still rejects (propagating to the caller's catch as before).
let stream: (AsyncIterable<Buffer> & { destroy?: () => void }) | undefined;
@@ -1454,6 +1451,10 @@ export class BaileysEvents {
};
assertWhatsAppSource(msg);
const sock = this.host.getSocket();
+ // A proxied session must not fetch media around its proxy (#859). Baileys reads the dispatcher
+ // from the nested `options`; a top-level one is ignored. Read after the socket: once the session
+ // is torn down getSocket() throws, and a dispatcher built then would never be closed.
+ const dispatcher = this.host.getFetchDispatcher();
stream = (await b.downloadMediaMessage(
msg,
'stream',
diff --git a/src/engine/adapters/baileys-media-abort.spec.ts b/src/engine/adapters/baileys-media-abort.spec.ts
index f4fad5254..546390476 100644
--- a/src/engine/adapters/baileys-media-abort.spec.ts
+++ b/src/engine/adapters/baileys-media-abort.spec.ts
@@ -24,7 +24,10 @@ function imageMessage(id: string, fileLength: number): WAMessage {
};
}
-function build(dispatcher?: object): { events: BaileysEvents; warns: string[] } {
+function build(
+ dispatcher?: object,
+ overrides: Partial<BaileysEventsHost> = {},
+): { events: BaileysEvents; warns: string[] } {
const warns: string[] = [];
const events = new BaileysEvents({
getSocket: () => ({ updateMediaMessage: jest.fn() }) as unknown as WASocket,
@@ -56,6 +59,7 @@ function build(dispatcher?: object): { events: BaileysEvents; warns: string[] }
getOnCall: () => undefined,
getOnPresenceUpdate: () => undefined,
getOnCallOutcome: () => undefined,
+ ...overrides,
} as unknown as BaileysEventsHost);
return { events, warns };
}
@@ -200,4 +204,22 @@ describe('BaileysEvents media download through a session proxy', () => {
expect(downloadMediaMessage).toHaveBeenCalledWith(expect.anything(), 'stream', {}, expect.anything());
});
+
+ // A message still queued when the session is torn down gets its turn afterwards. Reading the
+ // dispatcher first would rebuild one that the teardown had just closed, for nothing to close again.
+ it('builds no proxy dispatcher for a download whose session is already torn down', async () => {
+ const getFetchDispatcher = jest.fn(() => undefined);
+ const { events } = build(undefined, {
+ getSocket: () => {
+ throw new Error('Engine is not ready');
+ },
+ getFetchDispatcher,
+ });
+
+ const incoming = await events.mapMessage(imageMessage('LATE', 3), 'imageMessage');
+
+ expect(incoming.media).toMatchObject({ omitted: true });
+ expect(downloadMediaMessage).not.toHaveBeenCalled();
+ expect(getFetchDispatcher).not.toHaveBeenCalled();
+ });
});