Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
docs(security): qualify DNS-rebind pinning behind an HTTP proxy
Yudhi Armyndharisdc99234ce94c
diff --git a/.env.example b/.env.exampleindex 99fceffbd..bc3793bf7 100644--- a/.env.example+++ b/.env.example@@ -490,6 +490,8 @@ WEBHOOK_SSRF_PROTECT=true# so the destination sees the same address as the rest of that session's traffic. Set to false when# the proxy is a WhatsApp-only route that cannot reach arbitrary media hosts: those fetches then# leave from the gateway's own address again. Sessions without a proxy are unaffected either way.+# Through an http or https proxy the proxy resolves the URL's host itself, so the SSRF guard's+# DNS-rebind pinning does not apply there; a socks proxy or false keeps it.# SESSION_PROXY_URL_FETCH=true# ---------------------------------------------------------------------------diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 443f2ac41..c737c74c7 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -568,8 +568,11 @@ from the gateway's own address instead, for a proxy that only routes to WhatsAppeither way: the scheme and the destination are checked before any socket is opened, and the checked addressesare what a SOCKS proxy is asked to connect to, in resolver order, so a proxy that routes only one addressfamily still reaches a dual-stack host (a `socks4` proxy is always given an IPv4 one, the only family that-protocol carries). Behind an HTTP or HTTPS proxy the destination is named in the `CONNECT` line and resolved-by the proxy, so the connection there cannot be pinned to the address that was vetted.+protocol carries). Behind an HTTP or HTTPS proxy the destination is named to the proxy (in the `CONNECT` line+for an https URL, in the request line for an http one) and resolved by the proxy, so the connection there+cannot be pinned to the address that was vetted: a name that passes the check and then re-resolves to an+internal address reaches it, if the proxy can. Use a SOCKS proxy, or `SESSION_PROXY_URL_FETCH=false`, to keep+that DNS-rebinding protection.**Response** `201`@@ -1630,7 +1633,9 @@ internal loopback attacks", no patched release). Since the URL comes from messaginfluences what this server fetches, so the gateway supplies its own generator instead: it fetchesthrough the same SSRF guard used elsewhere, which validates the destination **and pins theconnection to the vetted address**, closing the DNS-rebinding window a validate-then-delegate-approach would leave open. `WEBHOOK_SSRF_PROTECT` and `SSRF_ALLOWED_HOSTS` apply, so a deployment+approach would leave open. The pin holds on a direct fetch and through a SOCKS session proxy; behind+an HTTP or HTTPS session proxy the proxy resolves the name, so that window stays open there (see+"Per-session egress proxy" above). `WEBHOOK_SSRF_PROTECT` and `SSRF_ALLOWED_HOSTS` apply, so a deploymentthat intentionally allows an internal host keeps that behaviour. A refused, slow or broken siteyields no preview — never a failed send.diff --git a/src/common/media/load-remote-media.ts b/src/common/media/load-remote-media.tsindex 59ced29b8..78a9456fd 100644--- a/src/common/media/load-remote-media.ts+++ b/src/common/media/load-remote-media.ts@@ -23,8 +23,9 @@ function positiveIntFromEnv(name: string, fallback: number): number {/*** Fetch remote media as a Buffer for sending, with an SSRF host guard, a byte cap, and a timeout.* The guard runs BEFORE any network call, so an internal/reserved URL throws `SsrfBlockedError`- * and no outbound socket is opened. It goes through `withSafeFetch`, which pins the connection to- * the vetted IP and refuses redirects (the guard only validated the original host — a followed 3xx+ * and no outbound socket is opened. It goes through `withSafeFetch`, which pins a direct or SOCKS+ * connection to the vetted IP (an HTTP/HTTPS session proxy resolves the name itself, so nothing is+ * pinned there) and refuses redirects (the guard only validated the original host — a followed 3xx* could reach an internal target). The cap is enforced while streaming (Content-Length may be absent* or wrong) to bound memory use.*@@ -48,7 +49,7 @@ export async function loadRemoteMediaBuffer(const timeoutMs = positiveIntFromEnv('MEDIA_DOWNLOAD_TIMEOUT_MS', DEFAULT_MEDIA_TIMEOUT_MS);// Always guarded (media SSRF is independent of the webhook opt-out); withSafeFetch validates the- // host, pins the connection to the vetted IP, and refuses redirects. The streaming cap runs inside+ // host, pins a direct or SOCKS connection to the vetted IP, and refuses redirects. The streaming cap runs inside// the callback so the connection stays open for the body read and is torn down right after.const proxyUrl = urlFetchProxy(sessionProxyUrl);return withSafeFetch(diff --git a/src/common/security/proxy-dispatcher.ts b/src/common/security/proxy-dispatcher.tsindex 6555c78a0..47d6f1ec3 100644--- a/src/common/security/proxy-dispatcher.ts+++ b/src/common/security/proxy-dispatcher.ts@@ -17,8 +17,9 @@ export interface ProxyDispatcherOptions {* dialled in order, because it is family-mixed: a dual-stack host commonly resolves AAAA first,* and neither a SOCKS4 proxy (which has no IPv6 form) nor an IPv4-only SOCKS5 proxy can reach* that first entry, while the direct path just falls through to the next address. An HTTP/HTTPS- * proxy is asked for the destination by NAME in the CONNECT line and resolves it itself, so there- * the value is ignored: pinning through such a proxy is not expressible in the protocol.+ * proxy is asked for the destination by NAME (in the CONNECT line for an https destination, in the+ * absolute-form request line for an http one) and resolves it itself, so there the value is+ * ignored: pinning through such a proxy is not expressible in the protocol.*/pinnedAddresses?: string[];}diff --git a/src/common/security/ssrf-guard.ts b/src/common/security/ssrf-guard.tsindex 24ae6cd5e..4a0378a5a 100644--- a/src/common/security/ssrf-guard.ts+++ b/src/common/security/ssrf-guard.ts@@ -464,9 +464,10 @@ function nextRedirectHopInit(init: RequestInit, status: number, nextUrl: string,* - Destination PINNING survives only through SOCKS, which carries the destination address in the* request. The whole vetted list goes over, to be dialled in order, so the address-family failover* the direct path gets from happy-eyeballs is not lost to a proxy that can route only one of them.- * An HTTP/HTTPS proxy is handed the destination by name in the CONNECT line and resolves it with- * its own resolver, so the vetted address cannot be expressed and a DNS rebind between check and- * connect is not structurally preventable.+ * An HTTP/HTTPS proxy is handed the destination by name (the CONNECT line for an https+ * destination, the absolute-form request line for an http one) and resolves it with its own+ * resolver, so the vetted address cannot be expressed and a DNS rebind between check and connect+ * is not structurally preventable.** Without a proxy the behaviour is byte-identical to before: the pinned `Agent` for a vetted* hostname, and no dispatcher at all for an IP literal or an unguarded fetch.@@ -480,12 +481,13 @@ function requestDispatcher(proxyUrl: string | undefined, target: LookupAddress[]/*** Perform an SSRF-safe fetch and hand the response to `use`, then tear down the per-request- * connection. The host is validated and resolved ONCE; the connection is pinned to the vetted IP(s)- * via an undici dispatcher so it cannot be re-resolved to an internal address between check and- * connect (DNS-rebinding TOCTOU). The original hostname is preserved for TLS SNI and the Host header,- * so virtual hosting and certificate validation are unaffected, and ALL vetted addresses are offered- * so A-record failover still works (behind a SOCKS proxy the same list is dialled in order, and- * behind an HTTP/HTTPS proxy the CONNECT line carries the name for the proxy to resolve). Redirects+ * connection. The host is validated and resolved ONCE; a direct or SOCKS-proxied connection is+ * pinned to the vetted IP(s) via an undici dispatcher so it cannot be re-resolved to an internal+ * address between check and connect (DNS-rebinding TOCTOU). The original hostname is preserved for+ * TLS SNI and the Host header, so virtual hosting and certificate validation are unaffected, and ALL+ * vetted addresses are offered so A-record failover still works (behind a SOCKS proxy the same list+ * is dialled in order). Behind an HTTP/HTTPS proxy nothing is pinned: the name goes to the proxy,+ * which resolves it itself, so that path keeps the check but not the rebind protection. Redirects* are refused (the guard only validated the original host).** `use` must read everything it needs from the response before returning — the dispatcher (and itsdiff --git a/src/engine/adapters/safe-link-preview.ts b/src/engine/adapters/safe-link-preview.tsindex dddf01941..ccac5795e 100644--- a/src/engine/adapters/safe-link-preview.ts+++ b/src/engine/adapters/safe-link-preview.ts@@ -26,10 +26,12 @@ export interface SafeUrlInfo {* request — so that generator is never reached: Baileys spreads the caller's send options last* (messages-send.js:1086), so passing this as `getUrlInfo` there wins over its hardcoded one.*- * `withSafeFetch` validates the destination and then PINS the connection to the vetted addresses, so- * a hostname that resolves publicly once and to `127.0.0.1` a moment later cannot be used to reach- * the loopback interface — the rebinding window that a validate-then-hand-off approach would leave- * open. It also honours the deployment's own `WEBHOOK_SSRF_PROTECT` / `SSRF_ALLOWED_HOSTS` settings,+ * `withSafeFetch` validates the destination and then PINS a direct or SOCKS-proxied connection to the+ * vetted addresses, so a hostname that resolves publicly once and to `127.0.0.1` a moment later cannot+ * be used to reach the loopback interface, the rebinding window that a validate-then-hand-off+ * approach would leave open. Behind an HTTP/HTTPS session proxy the proxy resolves the name itself,+ * so that window stays open there (SESSION_PROXY_URL_FETCH=false or a SOCKS proxy closes it).+ * It also honours the deployment's own `WEBHOOK_SSRF_PROTECT` / `SSRF_ALLOWED_HOSTS` settings,* so an operator who intentionally allows an internal host keeps that behaviour here too.** Returns undefined rather than throwing on any failure: a preview is decoration, and a site that isdiff --git a/src/engine/adapters/wwebjs-messaging.ts b/src/engine/adapters/wwebjs-messaging.tsindex 83af06347..e4edb5a78 100644--- a/src/engine/adapters/wwebjs-messaging.ts+++ b/src/engine/adapters/wwebjs-messaging.ts@@ -83,8 +83,9 @@ export function isHttpUrl(value: string): boolean {* existing MIME-detection behavior.*/export async function loadRemoteMedia(url: string, sessionProxyUrl: string | undefined): Promise<MessageMedia> {- // Fetch through the SSRF-pinned path: it validates the host, pins the connection to the vetted IP- // (so a DNS rebind can't redirect it to an internal target between check and connect), caps bytes,+ // Fetch through the SSRF-guarded path: it validates the host, pins a direct or SOCKS connection to+ // the vetted IP (so a DNS rebind can't redirect it to an internal target between check and connect;+ // an HTTP/HTTPS session proxy resolves the name itself, so nothing is pinned there), caps bytes,// and refuses redirects. We then build the MessageMedia from the returned bytes — NOT via// MessageMedia.fromUrl, whose bundled node-fetch performs its own unpinned DNS re-resolution.// `sessionProxyUrl` routes the fetch through this session's egress proxy (#1626); the browser's