Title: Fix origin check with default ports and X-Forwarded-Port by harshbhx · Pull Request #8059 · coder/code-server · GitHub
Open Graph Title: Fix origin check with default ports and X-Forwarded-Port by harshbhx · Pull Request #8059 · coder/code-server
X Title: Fix origin check with default ports and X-Forwarded-Port by harshbhx · Pull Request #8059 · coder/code-server
Description: Fixes #6166 Summary The origin check now treats a default port (:443 for https, :80 for http) as no port, and uses X-Forwarded-Port when X-Forwarded-Host has no port. Everything else is still an exact match. This is the approach agreed in the issue discussion. Problem authenticateOrigin compares 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: Origin Proxy config Host code-server sees Result today https://example.com X-Forwarded-Host $host:$server_port example.com:443 ❌ rejected https://example.com:8080 X-Forwarded-Host $host + X-Forwarded-Port $server_port example.com ❌ rejected In the second case nginx's $host drops the port and sends it separately in X-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 Default ports are normalized. hostMatchesOrigin() parses the host and treats the origin protocol's default port the same as no port. example.com:443 matches https://example.com, but example.com:80 does not (that's a different origin). X-Forwarded-Port fills in a missing port. When the host comes from X-Forwarded-Host and has no port, the first value of X-Forwarded-Port is appended. A port already in the host wins, and non-numeric values are ignored. No other leniency. A host without a port still doesn't match an origin on :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-Port is 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-Port with the Host or Forwarded headers, to keep the change to what was discussed. Forwarded can already carry the port in host=. Changes File Change src/node/http.ts New hostMatchesOrigin() (exact match with default-port normalization) replaces the string comparison. New addForwardedPort(). getHost() gets an optional withForwardedPort flag, used only by authenticateOrigin test/unit/node/http.test.ts New origin cases (default ports, wrong-protocol default port, missing port, IPv6). New x-forwarded-port block, plus a test that getHost() still returns the host without the port Before / After Origin: https://example.com X-Forwarded-Host: example.com:443 before: 403 host "example.com:443" does not match origin "example.com" after: allowed Origin: https://example.com:8080 X-Forwarded-Host: example.com X-Forwarded-Port: 8080 before: 403 host "example.com" does not match origin "example.com:8080" after: allowed Origin: https://example.com:8080 X-Forwarded-Host: example.com (no X-Forwarded-Port) before: 403 after: 403 (unchanged, no port is assumed) How to test ./test/node_modules/.bin/jest --forceExit --coverage=false test/unit/node/http.test.ts Optionally, put code-server behind nginx on a non-default port with: proxy_set_header Host $host; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; The workbench websocket should connect instead of closing with 1006. Testing done http.test.ts: 127/127 passing. The shared origin cases each run against Host, X-Forwarded-Host and three Forwarded header variants Against main, 15 of the new tests fail (default-port and X-Forwarded-Port cases) Against a looser "ignore the port when the host has none" version, 10 fail, so the stricter behaviour is pinned down too Full unit suite: 409/409 passing prettier --check, eslint and tsc --noEmit clean Not tested behind a real nginx proxy Notes for reviewers trusted-origins matching is unchanged and still runs before this check. Only the first value of a comma-separated X-Forwarded-Port is used, the same way X-Forwarded-Host is handled. Browsers can't set X-Forwarded-Port on a websocket handshake, and the hostname still has to match exactly, so the header can't be used to let a different site through.
Open Graph Description: Fixes #6166 Summary The origin check now treats a default port (:443 for https, :80 for http) as no port, and uses X-Forwarded-Port when X-Forwarded-Host has no port. Everything else is still an ex...
X Description: Fixes #6166 Summary The origin check now treats a default port (:443 for https, :80 for http) as no port, and uses X-Forwarded-Port when X-Forwarded-Host has no port. Everything else is still an ex...
Opengraph URL: https://github.com/coder/code-server/pull/8059
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:8b9cf937-04b3-bd25-5fd8-4403b8256554 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AC36:1D4BD6:5411DA:760418:6B0445A8 |
| html-safe-nonce | fb1485cbd2fb149961f349af9d37206f2da2d1660e893c0389340ebd5cc3a508 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzM2OjFENEJENjo1NDExREE6NzYwNDE4OjZCMDQ0NUE4IiwidmlzaXRvcl9pZCI6Ijg4Mzg2MjE3ODU4NzM0NjY3OTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b5d5841f7a999e9fe8979e20cb39c06c4eae8e6a35bb75dfc98b052b5a91d46f |
| hovercard-subject-tag | pull_request:4879669652 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/coder/code-server/pull/8059/files |
| twitter:image | https://avatars.githubusercontent.com/u/155092207?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/155092207?s=400&v=4 |
| og:image:alt | Fixes #6166 Summary The origin check now treats a default port (:443 for https, :80 for http) as no port, and uses X-Forwarded-Port when X-Forwarded-Host has no port. Everything else is still an ex... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | d4495c3631faba668036f176d1f5946901c5172efd73a6edb984029e7d4f8e46 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/coder/code-server git https://github.com/coder/code-server.git |
| octolytics-dimension-user_id | 95932066 |
| octolytics-dimension-user_login | coder |
| octolytics-dimension-repository_id | 172953845 |
| octolytics-dimension-repository_nwo | coder/code-server |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 172953845 |
| octolytics-dimension-repository_network_root_nwo | coder/code-server |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9ac4337fb7c6a5846cdfdb1a04f154d3c33ebc77 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width