fix(fetch): gate x-forwarded-for on trustProxy in app.fetch

The Web-Standards fetch adapter read x-forwarded-for unconditionally to
seed ctx.ip, unlike Node's deriveIp which only honours it when
trustProxy is enabled. A client could spoof ctx.ip on any handler
relying on it. Mirror deriveIp's first-hop semantics and only trust
the header when trustProxy is set; otherwise ctx.ip is ''.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-09 14:45:27 -05:00
1 parent 1fee7de543
commit 43a4316a29
2 files changed
+36 -2

No files matched your search

+8 -1
View File
@@ -143,7 +143,14 @@ export function buildFetch(routes: RouteDef[], opts: HttpOptions | undefined) {
headers,
body,
secure,
ip: headers['x-forwarded-for'] ?? '',
// Mirrors Node's `deriveIp` (src/http/request.ts): only trust `x-forwarded-for` when the proxy is
// trusted, and take its first hop — an untrusted XFF is client-spoofable, and there's no socket
// peer address to fall back to on the fetch path, so the untrusted case is simply ''.
ip: opts?.trustProxy
? String(headers['x-forwarded-for'] ?? '')
.split(',')[0]
.trim()
: '',
});
// Mirrors the Node adapter, where `result.preflight` is written through the writeHead patch and