Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

308 lines
16 KiB
Diff
Raw Permalink Normal View History

2022-03-22 15:07:14 -05:00
Add base path support
Some users will host code-server behind a path-rewriting reverse proxy, for
example domain.tld/my/base/path. This patch adds support for that since Code
assumes everything is on / by default.
To test this serve code-server behind a reverse proxy with a path like /code.
Index: code-server/lib/vscode/src/vs/base/common/network.ts
===================================================================
--- code-server.orig/lib/vscode/src/vs/base/common/network.ts
+++ code-server/lib/vscode/src/vs/base/common/network.ts
2026-11-05 09:00:37 +11:00
@@ -263,7 +263,9 @@ class RemoteAuthoritiesImpl {
2022-03-22 15:07:14 -05:00
return URI.from({
scheme: platform.isWeb ? this._preferredWebSchema : Schemas.vscodeRemoteResource,
2022-06-21 16:51:46 -05:00
authority: `${host}:${port}`,
- path: this._remoteResourcesPath,
2022-03-22 15:07:14 -05:00
+ path: platform.isWeb
2022-06-21 16:51:46 -05:00
+ ? (window.location.pathname + "/" + this._remoteResourcesPath).replace(/\/\/+/g, "/")
+ : this._remoteResourcesPath,
2022-03-22 15:07:14 -05:00
query
});
}
Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench-dev.html
===================================================================
--- code-server.orig/lib/vscode/src/vs/code/browser/workbench/workbench-dev.html
+++ code-server/lib/vscode/src/vs/code/browser/workbench/workbench-dev.html
@@ -11,8 +11,8 @@
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-title" content="Code">
- <link rel="apple-touch-icon" sizes="192x192" href="/_static/src/browser/media/pwa-icon-192.png" />
- <link rel="apple-touch-icon" sizes="512x512" href="/_static/src/browser/media/pwa-icon-512.png" />
+ <link rel="apple-touch-icon" sizes="192x192" href="{{BASE}}/_static/src/browser/media/pwa-icon-192.png" />
+ <link rel="apple-touch-icon" sizes="512x512" href="{{BASE}}/_static/src/browser/media/pwa-icon-512.png" />
<!-- Disable pinch zooming -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
2026-09-09 02:32:03 +10:00
@@ -29,9 +29,9 @@
<meta id="vscode-workbench-css-modules" data-settings="{{WORKBENCH_DEV_CSS_MODULES}}">
2022-03-22 15:07:14 -05:00
<!-- Workbench Icon/Manifest/CSS -->
- <link rel="icon" href="/_static/src/browser/media/favicon-dark-support.svg" />
2022-06-21 16:51:46 -05:00
- <link rel="alternate icon" href="/_static/src/browser/media/favicon.ico" type="image/x-icon" />
2022-03-22 15:07:14 -05:00
- <link rel="manifest" href="/manifest.json" crossorigin="use-credentials" />
+ <link rel="icon" href="{{BASE}}/_static/src/browser/media/favicon-dark-support.svg" />
2022-06-21 16:51:46 -05:00
+ <link rel="alternate icon" href="{{BASE}}/_static/src/browser/media/favicon.ico" type="image/x-icon" />
2022-03-22 15:07:14 -05:00
+ <link rel="manifest" href="{{VS_BASE}}/manifest.json" crossorigin="use-credentials" />
2024-11-01 20:32:25 +00:00
<style id="vscode-css-modules" type="text/css" media="screen"></style>
2022-03-22 15:07:14 -05:00
2026-10-08 17:54:47 +11:00
<!-- Startup fallback: shown only if the scripts below never run, e.g. because a proxy,
@@ -92,7 +92,7 @@
2024-11-01 20:32:25 +00:00
<!-- Startup (do not modify order of script tags!) -->
2026-09-09 02:32:03 +10:00
<script nonce="{{WORKBENCH_SCRIPT_NONCE}}">
- const baseUrl = new URL(document.getElementById('vscode-workbench-web-base-url').getAttribute('data-settings'), window.location.origin).toString();
+ const baseUrl = new URL(document.getElementById('vscode-workbench-web-base-url').getAttribute('data-settings'), window.location).toString();
2024-11-01 20:32:25 +00:00
globalThis._VSCODE_FILE_ROOT = baseUrl + '/out/';
</script>
2026-09-09 02:32:03 +10:00
<script nonce="{{WORKBENCH_SCRIPT_NONCE}}">
2022-03-22 15:07:14 -05:00
Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.html
===================================================================
--- code-server.orig/lib/vscode/src/vs/code/browser/workbench/workbench.html
+++ code-server/lib/vscode/src/vs/code/browser/workbench/workbench.html
@@ -11,8 +11,8 @@
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-title" content="Code">
- <link rel="apple-touch-icon" sizes="192x192" href="/_static/src/browser/media/pwa-icon-192.png" />
- <link rel="apple-touch-icon" sizes="512x512" href="/_static/src/browser/media/pwa-icon-512.png" />
+ <link rel="apple-touch-icon" sizes="192x192" href="{{BASE}}/_static/src/browser/media/pwa-icon-192.png" />
+ <link rel="apple-touch-icon" sizes="512x512" href="{{BASE}}/_static/src/browser/media/pwa-icon-512.png" />
<!-- Disable pinch zooming -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
2026-09-09 02:32:03 +10:00
@@ -25,9 +25,9 @@
2022-03-22 15:07:14 -05:00
<meta id="vscode-workbench-auth-session" data-settings="{{WORKBENCH_AUTH_SESSION}}">
<!-- Workbench Icon/Manifest/CSS -->
- <link rel="icon" href="/_static/src/browser/media/favicon-dark-support.svg" />
2022-06-21 16:51:46 -05:00
- <link rel="alternate icon" href="/_static/src/browser/media/favicon.ico" type="image/x-icon" />
2022-03-22 15:07:14 -05:00
- <link rel="manifest" href="/manifest.json" crossorigin="use-credentials" />
+ <link rel="icon" href="{{BASE}}/_static/src/browser/media/favicon-dark-support.svg" />
2022-06-21 16:51:46 -05:00
+ <link rel="alternate icon" href="{{BASE}}/_static/src/browser/media/favicon.ico" type="image/x-icon" />
2022-03-22 15:07:14 -05:00
+ <link rel="manifest" href="{{VS_BASE}}/manifest.json" crossorigin="use-credentials" />
2024-11-01 20:32:25 +00:00
<link rel="stylesheet" href="{{WORKBENCH_WEB_BASE_URL}}/out/vs/code/browser/workbench/workbench.css">
2022-03-22 15:07:14 -05:00
2026-10-08 17:54:47 +11:00
<!-- Startup fallback: shown only if the scripts below never run, e.g. because a proxy,
@@ -88,7 +88,7 @@
2024-08-15 21:33:21 -08:00
2024-11-01 20:32:25 +00:00
<!-- Startup (do not modify order of script tags!) -->
2026-09-09 02:32:03 +10:00
<script nonce="{{WORKBENCH_SCRIPT_NONCE}}">
- const baseUrl = new URL(document.getElementById('vscode-workbench-web-base-url').getAttribute('data-settings'), window.location.origin).toString();
+ const baseUrl = new URL(document.getElementById('vscode-workbench-web-base-url').getAttribute('data-settings'), window.location).toString();
2024-11-01 20:32:25 +00:00
globalThis._VSCODE_FILE_ROOT = baseUrl + '/out/';
</script>
2026-09-09 02:32:03 +10:00
<script nonce="{{WORKBENCH_SCRIPT_NONCE}}">
2022-03-22 15:07:14 -05:00
Index: code-server/lib/vscode/src/vs/platform/remote/browser/browserSocketFactory.ts
===================================================================
--- code-server.orig/lib/vscode/src/vs/platform/remote/browser/browserSocketFactory.ts
+++ code-server/lib/vscode/src/vs/platform/remote/browser/browserSocketFactory.ts
2025-11-18 21:21:41 +01:00
@@ -282,6 +282,7 @@ export class BrowserSocketFactory implem
2023-06-15 08:00:03 -08:00
connect({ host, port }: WebSocketRemoteConnection, path: string, query: string, debugLabel: string): Promise<ISocket> {
return new Promise<ISocket>((resolve, reject) => {
2023-12-15 22:38:01 +01:00
const webSocketSchema = (/^https:/.test(mainWindow.location.href) ? 'wss' : 'ws');
+ path = (mainWindow.location.pathname + "/" + path).replace(/\/\/+/g, "/")
2023-06-15 08:00:03 -08:00
const socket = this._webSocketFactory.create(`${webSocketSchema}://${(/:/.test(host) && !/\[/.test(host)) ? `[${host}]` : host}:${port}${path}?${query}&skipWebSocketFrames=false`, debugLabel);
2025-10-14 23:26:57 +02:00
const disposables = new DisposableStore();
disposables.add(socket.onError(reject));
2022-03-22 15:07:14 -05:00
Index: code-server/lib/vscode/src/vs/server/node/webClientServer.ts
===================================================================
--- code-server.orig/lib/vscode/src/vs/server/node/webClientServer.ts
+++ code-server/lib/vscode/src/vs/server/node/webClientServer.ts
2026-09-09 02:32:03 +10:00
@@ -146,7 +146,7 @@ export function createWorkbenchContentSe
'default-src \'self\';',
'img-src \'self\' https: data: blob:;',
'media-src \'self\';',
- `script-src 'self' 'unsafe-eval' ${nlsBaseUrl ?? ''} blob: 'nonce-${scriptNonce}' '${webWorkerExtensionHostIframeScriptSHA}' 'sha256-/r7rqQ+yrxt57sxLuQ6AMYcy/lUpvAIzHjIJt/OeLWU=' ${useTestResolver ? '' : `http://${remoteAuthority}`};`, // the sha is the same as in src/vs/workbench/services/extensions/worker/webWorkerExtensionHostIframe.html
+ `script-src 'self' 'unsafe-eval' ${nlsBaseUrl ?? ''} blob: 'nonce-${scriptNonce}' '${webWorkerExtensionHostIframeScriptSHA}' 'sha256-/r7rqQ+yrxt57sxLuQ6AMYcy/lUpvAIzHjIJt/OeLWU=' ${useTestResolver ? '' : ``};`, // the sha is the same as in src/vs/workbench/services/extensions/worker/webWorkerExtensionHostIframe.html
'child-src \'self\';',
`frame-src 'self' https://*.vscode-cdn.net data:;`,
'worker-src \'self\' data: blob:;',
@@ -307,8 +307,10 @@ export class WebClientServer {
2025-02-13 00:34:05 +01:00
};
// Prefix routes with basePath for clients
+ const rootBase = relativeRoot(getOriginalUrl(req))
+ const vscodeBase = relativePath(getOriginalUrl(req))
2026-09-09 02:32:03 +10:00
const forwardedPrefix = getFirstHeader('x-forwarded-prefix');
- const basePath = forwardedPrefix && isSafeBasePath(forwardedPrefix) ? forwardedPrefix : this._basePath;
+ const basePath = vscodeBase || (forwardedPrefix && isSafeBasePath(forwardedPrefix) ? forwardedPrefix : this._basePath);
2025-02-13 00:34:05 +01:00
2026-08-21 07:45:25 +10:00
const queryConnectionTokens = parsedUrl.searchParams.getAll(connectionTokenQueryName);
if (queryConnectionTokens.length === 1) {
2026-09-09 02:32:03 +10:00
@@ -345,10 +347,14 @@ export class WebClientServer {
2025-02-13 00:34:05 +01:00
};
2022-03-22 15:07:14 -05:00
2026-09-09 02:32:03 +10:00
const useTestResolver = (!this._environmentService.isBuilt && !!this._environmentService.args['use-test-resolver']);
2022-08-17 03:26:19 +02:00
+ // For now we are getting the remote authority from the client to avoid
+ // needing specific configuration for reverse proxies to work. Set this to
+ // something invalid to make sure we catch code that is using this value
+ // from the backend when it should not.
2025-02-13 00:34:05 +01:00
let remoteAuthority = (
2023-07-20 00:00:31 +02:00
useTestResolver
? 'test+test'
- : (getFirstHeader('x-original-host') || getFirstHeader('x-forwarded-host') || req.headers.host)
+ : 'remote'
);
2022-08-17 03:26:19 +02:00
if (!remoteAuthority) {
return serveError(req, res, 400, `Bad request.`);
2026-09-09 02:32:03 +10:00
@@ -395,6 +401,7 @@ export class WebClientServer {
2022-06-21 16:51:46 -05:00
2025-05-09 00:19:03 +02:00
const productConfiguration: Partial<Mutable<IProductConfiguration>> = {
2023-05-15 15:44:03 -08:00
codeServerVersion: this._productService.codeServerVersion,
2025-02-13 00:34:05 +01:00
+ rootEndpoint: rootBase,
2023-05-15 15:44:03 -08:00
embedderIdentifier: 'server-distro',
2026-07-31 06:25:04 +10:00
voiceWsUrl: this._productService.voiceWsUrl,
2024-06-07 01:02:13 +02:00
extensionsGallery: this._webExtensionResourceUrlTemplate && this._productService.extensionsGallery ? {
2026-09-09 02:32:03 +10:00
@@ -446,7 +453,9 @@ export class WebClientServer {
2025-02-13 00:34:05 +01:00
WORKBENCH_WEB_BASE_URL: staticRoute,
2024-08-15 21:33:21 -08:00
WORKBENCH_NLS_URL,
2026-09-09 02:32:03 +10:00
WORKBENCH_NLS_FALLBACK_URL: `${staticRoute}/out/nls.messages.js`,
- WORKBENCH_SCRIPT_NONCE: scriptNonce
+ WORKBENCH_SCRIPT_NONCE: scriptNonce,
2025-02-13 00:34:05 +01:00
+ BASE: rootBase,
+ VS_BASE: basePath,
2022-06-21 16:51:46 -05:00
};
2024-09-19 12:10:46 +02:00
// DEV ---------------------------------------------------------------------------------------
2026-09-09 02:32:03 +10:00
@@ -542,3 +551,70 @@ export class WebClientServer {
2022-11-09 15:10:03 -07:00
return void res.end(data);
2022-03-22 15:07:14 -05:00
}
}
+
+/**
+ * Remove extra slashes in a URL.
+ *
+ * This is meant to fill the job of `path.join` so you can concatenate paths and
+ * then normalize out any extra slashes.
+ *
+ * If you are using `path.join` you do not need this but note that `path` is for
+ * file system paths, not URLs.
+ */
+export const normalizeUrlPath = (url: string, keepTrailing = false): string => {
+ return url.replace(/\/\/+/g, "/").replace(/\/+$/, keepTrailing ? "/" : "")
+}
+
+/**
+ * Get the relative path that will get us to the root of the page. For each
+ * slash we need to go up a directory. Will not have a trailing slash.
+ *
+ * For example:
+ *
+ * / => .
+ * /foo => .
+ * /foo/ => ./..
+ * /foo/bar => ./..
+ * /foo/bar/ => ./../..
+ *
+ * All paths must be relative in order to work behind a reverse proxy since we
+ * we do not know the base path. Anything that needs to be absolute (for
+ * example cookies) must get the base path from the frontend.
+ *
+ * All relative paths must be prefixed with the relative root to ensure they
+ * work no matter the depth at which they happen to appear.
+ *
+ * For Express `req.originalUrl` should be used as they remove the base from the
+ * standard `url` property making it impossible to get the true depth.
+ */
+export const relativeRoot = (originalUrl: string): string => {
+ const depth = (originalUrl.split("?", 1)[0].match(/\//g) || []).length
+ return normalizeUrlPath("./" + (depth > 1 ? "../".repeat(depth - 1) : ""))
+}
+
+/**
+ * Get the relative path to the current resource.
+ *
+ * For example:
+ *
+ * / => .
+ * /foo => ./foo
+ * /foo/ => .
+ * /foo/bar => ./bar
+ * /foo/bar/ => .
+ */
+export const relativePath = (originalUrl: string): string => {
+ const parts = originalUrl.split("?", 1)[0].split("/")
+ return normalizeUrlPath("./" + parts[parts.length - 1])
+}
+
+/**
+ * code-server serves Code using Express. Express removes the base from the url
+ * and puts the original in `originalUrl` so we must use this to get the correct
+ * depth. Code is not aware it is behind Express so the types do not match. We
+ * may want to continue moving code into Code and eventually remove the Express
+ * wrapper or move the web server back into code-server.
+ */
+export const getOriginalUrl = (req: http.IncomingMessage): string => {
+ return (req as any).originalUrl || req.url
+}
Index: code-server/lib/vscode/src/vs/base/common/product.ts
===================================================================
--- code-server.orig/lib/vscode/src/vs/base/common/product.ts
+++ code-server/lib/vscode/src/vs/base/common/product.ts
2026-07-31 06:25:04 +10:00
@@ -98,6 +98,7 @@ export interface IDictationRuntimeProduc
2022-03-22 15:07:14 -05:00
export interface IProductConfiguration {
readonly codeServerVersion?: string
+ readonly rootEndpoint?: string
readonly version: string;
readonly date?: string;
Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
===================================================================
--- code-server.orig/lib/vscode/src/vs/code/browser/workbench/workbench.ts
+++ code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
2025-11-18 21:21:41 +01:00
@@ -339,7 +339,8 @@ class LocalStorageURLCallbackProvider ex
2023-09-20 15:33:28 -08:00
this.startListening();
}
2023-12-15 22:38:01 +01:00
- return URI.parse(mainWindow.location.href).with({ path: this._callbackRoute, query: queryParams.join('&') });
+ const path = (mainWindow.location.pathname + "/" + this._callbackRoute).replace(/\/\/+/g, "/");
+ return URI.parse(mainWindow.location.href).with({ path: path, query: queryParams.join('&') });
2022-03-22 15:07:14 -05:00
}
2023-09-20 15:33:28 -08:00
private startListening(): void {
2026-06-03 11:33:49 -08:00
@@ -590,17 +591,6 @@ class WorkspaceProvider implements IWork
2023-09-26 08:35:41 -08:00
}
}
-function readCookie(name: string): string | undefined {
- const cookies = document.cookie.split('; ');
- for (const cookie of cookies) {
- if (cookie.startsWith(name + '=')) {
- return cookie.substring(name.length + 1);
- }
- }
-
- return undefined;
-}
-
(function () {
2026-10-08 17:54:47 +11:00
// This script running at all means the page scripts were not blocked or rewritten.
@@ -614,8 +604,8 @@ function readCookie(name: string): strin
2022-03-22 15:07:14 -05:00
if (!configElement || !configElementAttribute) {
throw new Error('Missing web configuration element');
}
2022-06-21 16:51:46 -05:00
- const config: IWorkbenchConstructionOptions & { folderUri?: UriComponents; workspaceUri?: UriComponents; callbackRoute: string } = JSON.parse(configElementAttribute);
2023-09-26 08:35:41 -08:00
- const secretStorageKeyPath = readCookie('vscode-secret-key-path');
2022-06-21 16:51:46 -05:00
+ const config: IWorkbenchConstructionOptions & { folderUri?: UriComponents; workspaceUri?: UriComponents; callbackRoute: string } = { ...JSON.parse(configElementAttribute), remoteAuthority: location.host }
2023-09-26 08:35:41 -08:00
+ const secretStorageKeyPath = (window.location.pathname + "/mint-key").replace(/\/\/+/g, "/");
2023-09-20 15:33:28 -08:00
const secretStorageCrypto = secretStorageKeyPath && ServerKeyedAESCrypto.supported()
? new ServerKeyedAESCrypto(secretStorageKeyPath) : new TransparentCrypto();
2023-09-26 08:35:41 -08:00
2022-11-09 15:10:03 -07:00
Index: code-server/lib/vscode/src/vs/platform/extensionResourceLoader/common/extensionResourceLoader.ts
===================================================================
2022-11-09 15:10:03 -07:00
--- code-server.orig/lib/vscode/src/vs/platform/extensionResourceLoader/common/extensionResourceLoader.ts
+++ code-server/lib/vscode/src/vs/platform/extensionResourceLoader/common/extensionResourceLoader.ts
2025-04-04 20:22:23 +02:00
@@ -120,7 +120,7 @@ export abstract class AbstractExtensionR
2023-06-15 08:00:03 -08:00
: version,
path: 'extension'
}));
- return this._isWebExtensionResourceEndPoint(uri) ? uri.with({ scheme: RemoteAuthorities.getPreferredWebSchema() }) : uri;
+ return this._isWebExtensionResourceEndPoint(uri) ? URI.joinPath(URI.parse(window.location.href), uri.path) : uri;
}
return undefined;
}