Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): route the initial sync downloads through the session proxy

Yudhi Armyndharis76b3ac7b5436
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6eaabd5b8..0b6c72fda 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The amd64 image ships Chrome for Testing 153.0.8010.36 instead of 146.0.7680.31, picking up the browser security fixes released since (the arm64 image uses the Debian chromium package).
- The `session.qr` WebSocket event reaches only OPERATOR and ADMIN keys, matching `GET /api/sessions/{sessionId}/qr`; a VIEWER key subscribed by name or through a wildcard no longer receives the pairing QR.
- An integration ingress route verified with `shared-secret` no longer stores the instance secret from its declared header; the value is redacted in the persisted event, the queued job, the dead-letter row and the `ingress:error` hook payload.
-- Baileys sessions with an HTTP, HTTPS or SOCKS5 proxy download inbound media through the proxy instead of connecting direct, and look up the WhatsApp Web version through it instead of always falling back; with a SOCKS4 proxy both are skipped, and inbound media arrives as the omitted marker.
+- Baileys sessions with an HTTP, HTTPS or SOCKS5 proxy fetch through the proxy instead of connecting direct: inbound media, the WhatsApp Web version lookup, the history-sync and app-state payloads of the initial sync, and a product card's image URL. With a SOCKS4 proxy, which the HTTP client cannot use, inbound media is skipped and arrives as the omitted marker, the version lookup falls back to the bundled version, and the initial-sync payloads and product image are still fetched directly.
### Added
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index f980d7b93..191574502 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -551,10 +551,13 @@ network cannot reach WhatsApp directly. Set `proxyUrl`/`proxyType` on the same r
> WebSocket never connects, **no QR code is ever delivered**, and `POST /api/sessions/:sessionId/start`
> returns `504 Gateway Timeout` after ~30s. Leave `proxyUrl` unset unless you genuinely need a proxy.
-On the Baileys engine a `socks4` proxy carries the WebSocket and media uploads only: inbound media is not
-downloaded (it arrives as the omitted marker) and the WhatsApp Web version is not looked up remotely, so
-neither can leave outside the proxy. With `socks5`, `http` or `https` both also go through the proxy. Fetches
-of a URL you pass in, such as a catalog product `imageUrl` or an opt-in link preview, still connect directly.
+On the Baileys engine, with `socks5`, `http` or `https`, the proxy also carries everything the engine fetches
+over HTTP: inbound media, the WhatsApp Web version lookup, the history-sync and app-state payloads of the
+initial sync, and a product card's `imageUrl`. A `socks4` proxy carries the WebSocket and media uploads only,
+because the HTTP client has no SOCKS4 transport: inbound media is not downloaded (it arrives as the omitted
+marker) and the WhatsApp Web version is not looked up remotely, while the initial-sync payloads and a product
+card's `imageUrl` are still fetched directly. On every proxy scheme, a media URL you pass to a send or to
+`POST /api/media/convert` is fetched by the gateway itself, directly, not through the session proxy.
**Response** `201`
diff --git a/src/engine/adapters/baileys-lifecycle.ts b/src/engine/adapters/baileys-lifecycle.ts
index f03bd6c2e..4a66a6b86 100644
--- a/src/engine/adapters/baileys-lifecycle.ts
+++ b/src/engine/adapters/baileys-lifecycle.ts
@@ -110,11 +110,11 @@ export function createProxyAgent(proxyUrl: string, connectTimeoutMs = PROXY_CONN
}
/**
- * Build the global-fetch dispatcher for a session egress proxy, used by Baileys' media download and
- * version lookup. Global fetch accepts only an undici dispatcher, never a Node http Agent, and Node's
- * bundled undici predates the handler API of the installed one, so the agent is wrapped to speak the
- * older interface. undici has no SOCKS4 transport: null tells the caller to skip the request rather
- * than send it direct.
+ * Build the global-fetch dispatcher for a session egress proxy, used by Baileys' media download, the
+ * version lookup and the socket's own `options` fetch init. Global fetch accepts only an undici
+ * dispatcher, never a Node http Agent, and Node's bundled undici predates the handler API of the
+ * installed one, so the agent is wrapped to speak the older interface. undici has no SOCKS4 transport:
+ * null tells the caller to skip the request rather than send it direct.
*/
export function createProxyDispatcher(proxyUrl: string): Dispatcher | null {
const { protocol, username, password } = new URL(proxyUrl);
@@ -358,6 +358,7 @@ export class BaileysLifecycle {
}
}
+ const fetchDispatcher = this.fetchDispatcher();
const sock = b.default({
auth: state,
version,
@@ -367,6 +368,12 @@ export class BaileysLifecycle {
// Media downloads use fetchDispatcher() instead, since Baileys fetches them with global fetch.
agent: proxyAgent,
fetchAgent: proxyAgent,
+ // The same dispatcher for the fetches Baileys runs off this config itself: the history-sync
+ // payload, the app-state external blobs, and a URL handed to a send (a product card image).
+ // Without it they leave direct from the host IP even on a proxied session. `{}` is Baileys' own
+ // default for the key, and is what an unproxied session gets, as does a SOCKS4 one: it has no
+ // fetch transport at all, so these keep going direct there as they always have.
+ options: (fetchDispatcher ? { dispatcher: fetchDispatcher } : {}) as RequestInit,
// Enable the initial sync. Baileys defaults `shouldSyncHistoryMessage` to `() => !!syncFullHistory`,
// so leaving both unset disables ALL history + app-state sync - no contacts, chats, recent history,
// or lid->phone mappings ever arrive (the address-book app-state sync only runs once history sync is
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index b61ed168f..768d7be33 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -5196,11 +5196,25 @@ describe('BaileysAdapter proxy support', () => {
expect(jest.requireMock('@whiskeysockets/baileys').fetchLatestBaileysVersion).not.toHaveBeenCalled();
});
- it('leaves agent/fetchAgent unset without a proxyUrl', async () => {
+ it('hands the socket config a fetch dispatcher, for the downloads Baileys runs itself', async () => {
+ await proxied('http://user:[email protected]:8080').initialize(noopCallbacks());
+ const options = lastSocketConfig().options as { dispatcher?: unknown };
+ expect(options.dispatcher).toBeInstanceOf(Dispatcher1Wrapper);
+ });
+
+ it('leaves the socket fetch options at the library default for a socks4 proxy', async () => {
+ // Global fetch has no SOCKS4 transport, so there is nothing to hand it; these downloads keep the
+ // direct behaviour they have always had rather than being broken outright.
+ await proxied('socks4://proxy.example:1080').initialize(noopCallbacks());
+ expect(lastSocketConfig().options).toEqual({});
+ });
+
+ it('leaves agent/fetchAgent unset and the fetch options at the library default without a proxyUrl', async () => {
await newAdapter().initialize(noopCallbacks());
const cfg = lastSocketConfig();
expect(cfg.agent).toBeUndefined();
expect(cfg.fetchAgent).toBeUndefined();
+ expect(cfg.options).toEqual({});
});
it('fails closed: an unusable proxy value fails initialize instead of connecting direct', async () => {