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:
2 files changed
+36
-2
No files matched your search
+8
-1
@@ -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
|
||||
|
||||
Reference in new issue
Block a user