Skip to content

Fix CSP behind reverse proxy #4476

Closed
Closed
@jsjoeio

Description

@jsjoeio

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:

  1. install Caddy (brew install caddy)
  2. start code-server locally with yarn watch
  3. 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
}
  1. navigate to http://localhost:8082/code/ (trailing slash is important!)
  2. 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)
image

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions