Repository navigation
Unquote the host in the Forwarded header - #8155
Conversation
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.
57e5073 to
a39a39c
Compare
| 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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
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