Repository navigation
Conversation
The origin check compared the host header to the origin's host as plain strings, so `example.com:443` did not match `https://example.com`, and a proxy forwarding `$host` (no port) together with X-Forwarded-Port failed on any non-default port. Default ports (80 for http, 443 for https) are now treated as no port, and when X-Forwarded-Host has no port the port from X-Forwarded-Port is used. Everything else is still an exact match. getHost() is unchanged for the domain proxy. Fixes coder#6166
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6166
Summary
The origin check now treats a default port (
:443for https,:80for http) as no port, and usesX-Forwarded-PortwhenX-Forwarded-Hosthas no port. Everything else is still an exact match. This is the approach agreed in the issue discussion.Problem
authenticateOrigincompares the forwarded host to the origin's host as plain strings (src/node/http.ts#L406). Two reverse-proxy setups fail that comparison even though the request is same-origin, and the websocket then closes with 1006:https://example.comX-Forwarded-Host $host:$server_portexample.com:443https://example.com:8080X-Forwarded-Host $host+X-Forwarded-Port $server_portexample.comIn the second case nginx's
$hostdrops the port and sends it separately inX-Forwarded-Port, but code-server never reads that header. This is the standard config @smerschjohann suggested in the issue, so it should just work.Solution
hostMatchesOrigin()parses the host and treats the origin protocol's default port the same as no port.example.com:443matcheshttps://example.com, butexample.com:80does not (that's a different origin).X-Forwarded-Portfills in a missing port. When the host comes fromX-Forwarded-Hostand has no port, the first value ofX-Forwarded-Portis appended. A port already in the host wins, and non-numeric values are ignored.:8080, and different ports are still rejected.The host is parsed with a non-special URL scheme (
host://…), which keeps the port exactly as written (special schemes drop default ports) and handles bracketed IPv6 such as[::1]:8080.X-Forwarded-Portis only applied for the origin check (getHost(req, true)).getHost(req)is unchanged because the domain proxy matches its result against an anchored regex (code-{{port}}.example.com), and an appended port would break that.I didn't use
X-Forwarded-Portwith theHostorForwardedheaders, to keep the change to what was discussed.Forwardedcan already carry the port inhost=.Changes
src/node/http.tshostMatchesOrigin()(exact match with default-port normalization) replaces the string comparison. NewaddForwardedPort().getHost()gets an optionalwithForwardedPortflag, used only byauthenticateOrigintest/unit/node/http.test.tsx-forwarded-portblock, plus a test thatgetHost()still returns the host without the portBefore / After
How to test
./test/node_modules/.bin/jest --forceExit --coverage=false test/unit/node/http.test.tsTesting done
http.test.ts: 127/127 passing. The shared origin cases each run againstHost,X-Forwarded-Hostand threeForwardedheader variantsmain, 15 of the new tests fail (default-port andX-Forwarded-Portcases)prettier --check,eslintandtsc --noEmitcleanNotes for reviewers
trusted-originsmatching is unchanged and still runs before this check.X-Forwarded-Portis used, the same wayX-Forwarded-Hostis handled.X-Forwarded-Porton a websocket handshake, and the hostname still has to match exactly, so the header can't be used to let a different site through.