From a39a39c4fa4899e4bb2be03366451ef25d7562a8 Mon Sep 17 00:00:00 2001 From: Companion <3773910+Companion@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:05:23 +0000 Subject: [PATCH] Unquote the host in the Forwarded header RFC 7239 allows Forwarded values to be quoted strings, and a host with a port has to be quoted because a colon is not allowed in a token. Proxies like Spring Cloud Gateway send host="example.com:8080", which kept its quotes, so the origin check failed and web sockets got a 403. --- src/node/http.ts | 6 ++++-- test/unit/node/http.test.ts | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/node/http.ts b/src/node/http.ts index 7a689f32e53a..35ad55d4e2a3 100644 --- a/src/node/http.ts +++ b/src/node/http.ts @@ -478,8 +478,10 @@ export function getHost(req: express.Request, withForwardedPort = false): string const parts = forwardedRaw.split(/[;,]/) for (let i = 0; i < parts.length; ++i) { const [key, value] = splitOnFirstEquals(parts[i]) - if (key.trim().toLowerCase() === "host" && value) { - return value.trim().toLowerCase() + // Values can be quoted, like host="example.com:8080" (RFC 7239). + const host = value?.replace(/^\s*"(.*)"\s*$/, "$1") + if (key.trim().toLowerCase() === "host" && host) { + return host.trim().toLowerCase() } } } diff --git a/test/unit/node/http.test.ts b/test/unit/node/http.test.ts index 5b247c248e91..9ecb574aa2cf 100644 --- a/test/unit/node/http.test.ts +++ b/test/unit/node/http.test.ts @@ -149,6 +149,7 @@ describe("http", () => { ["forwarded", `for=127.0.0.1, host=${test.host}, proto=http`], ["forwarded", `for=127.0.0.1;proto=http;host=${test.host}`], ["forwarded", `proto=http;host=${test.host}, for=127.0.0.1`], + ["forwarded", `for="127.0.0.1:8080";proto=http;host="${test.host}"`], ].forEach(([key, value]) => { it(`${test.origin} -> [${key}: ${value}]`, () => { const req = getMockReq({