Skip to content

Commit ef5eba4

Browse files
authored
Preserve path, query, and fragment in proxy URL rewrite (#8012)
1 parent 20a260f commit ef5eba4

3 files changed

Lines changed: 81 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ Code v99.99.999
2222

2323
## Unreleased
2424

25+
### Fixed
26+
27+
- Preserve the original path, query parameters, and fragment when rewriting
28+
localhost URLs through the port proxy (#7668).
29+
2530
## [4.137.0](https://github.com/coder/code-server/releases/tag/v4.137.0) - 2026-09-11
2631

2732
Code v1.137.0

patches/proxy-uri.diff

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ This has e2e tests.
1313
For the `asExternalUri` changes, you'll need to test manually by:
1414
1. running code-server with the test extension
1515
2. Command Palette > code-server: asExternalUri test
16-
3. input a url like http://localhost:3000
17-
4. it should show a notification and show output as <code-server>/proxy/3000
16+
3. input a url like http://localhost:3000/my/path?token=abc#section
17+
4. it should show a notification and show output as
18+
<code-server>/proxy/3000/my/path?token%3Dabc#section
1819

1920
Do the same thing but set `VSCODE_PROXY_URI: "https://{{port}}-main-workspace-name-user-name.coder.com"`
2021
and the output should replace `{{port}}` with port used in input url.
2122

23+
The rewritten URI must preserve the original path, query, and fragment (see
24+
#7668). Append the original path to the proxy path using URI components so
25+
encoded characters are not decoded or encoded twice. This also works with
26+
proxy templates that do not have a trailing slash.
27+
2228
This also enables the forwared ports view panel by default.
2329

2430
Lastly, it adds a tunnelProvider so that ports are forwarded using code-server's
@@ -104,7 +110,7 @@ Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
104110
import type { IURLCallbackProvider } from '../../../workbench/services/url/browser/urlService.js';
105111
import { create } from '../../../workbench/workbench.web.main.internal.js';
106112

107-
@@ -612,6 +613,39 @@ class WorkspaceProvider implements IWork
113+
@@ -612,6 +613,44 @@ class WorkspaceProvider implements IWork
108114
settingsSyncOptions: config.settingsSyncOptions ? { enabled: config.settingsSyncOptions.enabled, } : undefined,
109115
workspaceProvider: WorkspaceProvider.create(config),
110116
urlCallbackProvider: new LocalStorageURLCallbackProvider(config.callbackRoute),
@@ -116,7 +122,12 @@ Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
116122
+ const renderedTemplate = config.productConfiguration.proxyEndpointTemplate
117123
+ .replace('{{port}}', localhostMatch.port.toString())
118124
+ .replace('{{host}}', window.location.host)
119-
+ resolvedUri = URI.parse(new URL(renderedTemplate, window.location.href).toString())
125+
+ const proxyUri = URI.parse(new URL(renderedTemplate, window.location.href).toString())
126+
+ resolvedUri = proxyUri.with({
127+
+ path: proxyUri.path.replace(/\/$/, '') + uri.path,
128+
+ query: uri.query,
129+
+ fragment: uri.fragment,
130+
+ })
120131
+ } else {
121132
+ throw new Error(`Failed to resolve external URI: ${uri.toString()}. Could not determine base url because productConfiguration missing.`)
122133
+ }

test/e2e/extensions.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,56 @@ function runTestExtensionTests() {
1515
const normalizedAddress = address.replace(/\/+$/, "")
1616
await expect(codeServerPage.page.getByText(`Info: proxyUri: ${normalizedAddress}/proxy/{{port}}/`)).toBeVisible()
1717
})
18+
19+
runExternalUriTests()
20+
}
21+
22+
function runExternalUriTests(proxyEndpointTemplate?: string) {
23+
const cases = [
24+
{
25+
name: "path",
26+
input: "http://127.0.0.1:1234/my/path",
27+
suffix: "/my/path",
28+
},
29+
{
30+
name: "query and fragment",
31+
input: "http://localhost:1234/my/path?token=abc&mode=preview#section",
32+
suffix: "/my/path?token%3Dabc%26mode%3Dpreview#section",
33+
},
34+
{
35+
name: "encoded components",
36+
input: "http://0.0.0.0:1234/my%20path/%23file?token=a%20b#my%20section",
37+
suffix: "/my%20path/%23file?token%3Da%20b#my%20section",
38+
},
39+
{
40+
name: "root path",
41+
input: "http://127.0.0.1:1234/",
42+
suffix: "/",
43+
},
44+
]
45+
46+
for (const { name, input, suffix } of cases) {
47+
test(`asExternalUri should preserve ${name}`, async ({ codeServerPage }) => {
48+
const address = await getMaybeProxiedCodeServer(codeServerPage)
49+
const normalizedAddress = address.replace(/\/+$/, "")
50+
const proxyBase = proxyEndpointTemplate
51+
? new URL(proxyEndpointTemplate.replace("{{port}}", "1234"), `${normalizedAddress}/`).toString()
52+
: `${normalizedAddress}/proxy/1234/`
53+
54+
await codeServerPage.waitForTestExtensionLoaded()
55+
await codeServerPage.executeCommandViaMenus("code-server: asExternalUri test")
56+
57+
const inputBox = codeServerPage.page.locator(".quick-input-widget input")
58+
await inputBox.fill(input)
59+
await inputBox.press("Enter")
60+
61+
// The test extension displays URI.toString(), which also encodes query delimiters.
62+
const output = `${proxyBase.replace(/\/$/, "")}${suffix}`
63+
await expect(
64+
codeServerPage.page.getByText(`Info: input: ${input} output: ${output}`, { exact: true }),
65+
).toBeVisible()
66+
})
67+
}
1868
}
1969

2070
const flags = ["--disable-workspace-trust", "--extensions-dir", path.join(__dirname, "./extensions")]
@@ -23,6 +73,17 @@ describe("Extensions", flags, {}, () => {
2373
runTestExtensionTests()
2474
})
2575

76+
for (const proxyEndpointTemplate of ["./proxy/{{port}}", "https://{{port}}-workspace.example.com"]) {
77+
describe(
78+
`Extensions with VSCODE_PROXY_URI=${proxyEndpointTemplate}`,
79+
flags,
80+
{ VSCODE_PROXY_URI: proxyEndpointTemplate },
81+
() => {
82+
runExternalUriTests(proxyEndpointTemplate)
83+
},
84+
)
85+
}
86+
2687
if (process.env.USE_PROXY !== "1") {
2788
describe("Extensions with --cert", [...flags, "--cert"], {}, () => {
2889
runTestExtensionTests()

0 commit comments

Comments
 (0)