Skip to content

Commit dda09f2

Browse files
authored
Split proxy request paths on / instead of path.sep (#8123)
The base path of /proxy/<port> requests was computed by splitting the request path on path.sep, which is a backslash on Windows, so the whole path became the base. Requests were proxied to / on the target port and redirects were rewritten against the wrong base. URL paths always use /.
1 parent b1ec0ce commit dda09f2

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

‎src/node/routes/pathProxy.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Request, Response } from "express"
2-
import * as path from "path"
32
import { HttpCode, HttpError } from "../../common/http"
43
import { ensureProxyEnabled, authenticated, ensureAuthenticated, ensureOrigin, redirect, self } from "../http"
54
import { proxy as _proxy } from "../proxy"
@@ -46,7 +45,7 @@ export async function proxy(
4645

4746
// The base is used for rewriting (redirects, target).
4847
if (!opts?.passthroughPath) {
49-
;(req as any).base = req.path.split(path.sep).slice(0, 3).join(path.sep)
48+
;(req as any).base = req.path.split("/").slice(0, 3).join("/")
5049
}
5150

5251
_proxy.web(req, res, {
@@ -68,7 +67,7 @@ export async function wsProxy(
6867

6968
// The base is used for rewriting (redirects, target).
7069
if (!opts?.passthroughPath) {
71-
;(req as any).base = req.path.split(path.sep).slice(0, 3).join(path.sep)
70+
;(req as any).base = req.path.split("/").slice(0, 3).join("/")
7271
}
7372

7473
_proxy.ws(req, req.ws, req.head, {

‎test/unit/node/proxy.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ describe("proxy", () => {
6464
expect(json).toBe("asher is the best")
6565
})
6666

67+
it("should rewrite the base path when the path separator is a backslash", async () => {
68+
const nodePath = jest.requireActual("path")
69+
const sep = nodePath.sep
70+
nodePath.sep = "\\"
71+
try {
72+
e.get("/wsup", (req, res) => {
73+
res.json("asher is the best")
74+
})
75+
codeServer = await integration.setup(["--auth=none"], "")
76+
const resp = await codeServer.fetch(proxyPath)
77+
expect(resp.status).toBe(200)
78+
const json = await resp.json()
79+
expect(json).toBe("asher is the best")
80+
} finally {
81+
nodePath.sep = sep
82+
}
83+
})
84+
6785
it("should not rewrite the base path", async () => {
6886
e.get(absProxyPath, (req, res) => {
6987
res.json("joe is the best")

0 commit comments

Comments
 (0)