Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

@code-asher code-asher Dec 2, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we are also stripping spaces when unquoting.

Are spaces also valid? I looked through rfc 7239 and I see the part about quotes but should something like host= "test.com" be converted to test.com, or should it remain test.com ?

And, if that is the case, then I suppose host= test.com without quotes should also become test.com? Or maybe not because it is implicitly host=" test.com " which means the spaces should be kept.

The only mention of whitespace seems to be between identifiers which I think means between each key=value and not within the value itself but I am not 100% sure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces around = or inside the value are not valid
rfc 7239 section 4 has forwarded-pair = token "=" value and value = token / quoted-string so there is no whitespace around = and a token cannot hold spaces so host= test.com is just invalid and not an implicit quoted string
the only whitespace it allows is the optional whitespace around commas from the list rule in rfc 7230 section 7 so host="example.com" , proto=https gives the value "example.com" with a trailing space which is why the outer \s* is there and why the value was already trimmed before this pr
inside quotes the spaces belong to the quoted string but section 5.3 says the unquoted host must match the host abnf which has no whitespace so host=" test.com " is not a valid host either
so all three of your examples are invalid and they all come out as test.com because i kept the existing trim the same as x-forwarded-host
happy to make it strict and only strip the quotes if you prefer

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right I am dumb we were already doing trim(), so this is not new behavior. My bad! I think this is good as it is. Thank you!

if (key.trim().toLowerCase() === "host" && host) {
return host.trim().toLowerCase()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/unit/node/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down