Description
What's the problem I'm solving?
With the latest changes to use the upstream server in code-server, we're now running into Content-Security-Policy issues with our reverse proxy. The specific code we're talking about lives here.
When using a reverse proxy (i.e. Caddy/NGINX), the update endpoint is incorrect because it has the wrong base path and protocol.
What are some ways to go about it?
The most ideal way to solve this is to use relative endpoints. This way, code-server doesn't care about the base path or protocol. It only knows and uses relative paths.
How will things change/what is the need for maintenance?
I am not sure if there is historical context I may be missing which would explain why we didn't use a relative path from the start (or rather why upstream didn't). Patching this in vscode
means it's something we'll need to keep an eye on as we continue staying in line with upstream.
I can't imagine this introducing any breaking changes but it could potentially break in specific environments if they don't support using relative paths for some reason 🤔 (though none specifically come to mind).
How to reproduce
Asher outlined steps below to reproduce this issue locally. To do so, follow these steps:
- install Caddy (
brew install caddy
) - start code-server locally with
yarn watch
- serve it at as base path like
/code
with Caddy
http://localhost:8082/code/* {
uri strip_prefix /code
reverse_proxy 127.0.0.1:8080
}
- navigate to http://localhost:8082/code/ (trailing slash is important!)
- observe the update endpoint error in the browser console OR see the Request URL in the network tab (look for a request to
http://localhost:8082/update/check
)
Content Security Policy: The page’s settings blocked the loading of a resource at http://localhost:8080/update/check (“connect-src”).
Expected
It makes a request to the correct URL: http://localhost:8082/code/update/check
Actual
It makes a request to the wrong URL: http://localhost:8082/update/check
(missing /code
)
Investigation Notes
Based on what I can tell, this is the line we need to change:
return new URL(path.join('/', pathPrefix, pathname), remoteAuthority);
Notice the hard-coded '/'
: that assumes we're always serving from the root but if we're using a reverse proxy and serving from a path like /code
then this doesn't return the correct URL.