Skip to content

Unquote the host in the Forwarded header - #8155

Merged
code-asher merged 1 commit into
coder:mainfrom
Companion:fix-quoted-forwarded-host
Dec 3, 2026
Merged

code-asher merged 1 commit into
coder:mainfrom
Companion:fix-quoted-forwarded-host

Conversation

@Companion

Copy link
Copy Markdown
Contributor

fixes #7053

rfc 7239 lets forwarded values be quoted and a host with a port has to be so spring cloud gateway sends host="example.com:8080"
code-server kept the quotes so the origin check failed and web sockets got a 403 now the quotes are stripped before the check

@Companion
Companion requested a review from a team as a code owner November 24, 2026 04:08
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.
@Companion
Companion force-pushed the fix-quoted-forwarded-host branch from 57e5073 to a39a39c Compare November 24, 2026 07:55

@code-asher code-asher left a comment •

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.

Thank you! I just have a thought about the spaces

Comment thread src/node/http.ts
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!

@code-asher
code-asher merged commit bf6cdf3 into coder:main Dec 3, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpringCloud Gateway

2 participants