From b8d830b826faff8bb564ac99b3e1145bfae943dd Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 16 Apr 2024 09:50:36 -0800 Subject: [PATCH] Update path syntax for Express It seems that * matches a literal * now, so we have to use a regular expression. Parentheses around a parameter no longer works (it causes it to match on the parameter name literally) and I am not sure why we had it anyway as it had no effect previously. Matching with a leading / does not appear to work either, but we do not need the leading / anyway since the proxy logic was changed to use the whole path. Consequently it will never be / anymore from what I can tell but I left that check in just in case. I turned it into a named parameter as well, because that seems better. --- src/node/routes/domainProxy.ts | 4 ++-- src/node/routes/index.ts | 8 ++++---- src/node/routes/pathProxy.ts | 2 +- src/node/routes/vscode.ts | 4 ++-- test/unit/node/proxy.test.ts | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/node/routes/domainProxy.ts b/src/node/routes/domainProxy.ts index 8f0537acc..fc0f91dad 100644 --- a/src/node/routes/domainProxy.ts +++ b/src/node/routes/domainProxy.ts @@ -53,7 +53,7 @@ const maybeProxy = (req: Request): string | undefined => { return undefined } -router.all("*", async (req, res, next) => { +router.all(/.*/, async (req, res, next) => { const port = maybeProxy(req) if (!port) { return next() @@ -97,7 +97,7 @@ router.all("*", async (req, res, next) => { export const wsRouter = WsRouter() -wsRouter.ws("*", async (req, _, next) => { +wsRouter.ws(/.*/, async (req, _, next) => { const port = maybeProxy(req) if (!port) { return next() diff --git a/src/node/routes/index.ts b/src/node/routes/index.ts index a235c4252..9c494a546 100644 --- a/src/node/routes/index.ts +++ b/src/node/routes/index.ts @@ -109,21 +109,21 @@ export const register = async (app: App, args: DefaultedArgs): Promise { + app.router.all("/proxy/:port/:path(.*)?", async (req, res) => { await pathProxy.proxy(req, res) }) - app.wsRouter.get("/proxy/(:port)(/*)?", async (req) => { + app.wsRouter.get("/proxy/:port/:path(.*)?", async (req) => { await pathProxy.wsProxy(req as pluginapi.WebsocketRequest) }) // These two routes pass through the path directly. // So the proxied app must be aware it is running // under /absproxy// - app.router.all("/absproxy/(:port)(/*)?", async (req, res) => { + app.router.all("/absproxy/:port/:path(.*)?", async (req, res) => { await pathProxy.proxy(req, res, { passthroughPath: true, }) }) - app.wsRouter.get("/absproxy/(:port)(/*)?", async (req) => { + app.wsRouter.get("/absproxy/:port/:path(.*)?", async (req) => { await pathProxy.wsProxy(req as pluginapi.WebsocketRequest, { passthroughPath: true, }) diff --git a/src/node/routes/pathProxy.ts b/src/node/routes/pathProxy.ts index a2281eab0..7f69bcf9e 100644 --- a/src/node/routes/pathProxy.ts +++ b/src/node/routes/pathProxy.ts @@ -22,7 +22,7 @@ export async function proxy( if (!(await authenticated(req))) { // If visiting the root (/:port only) redirect to the login page. - if (!req.params[0] || req.params[0] === "/") { + if (!req.params.path || req.params.path === "/") { const to = self(req) return redirect(req, res, "login", { to: to !== "/" ? to : undefined, diff --git a/src/node/routes/vscode.ts b/src/node/routes/vscode.ts index b9ee2c13b..c4f2808a5 100644 --- a/src/node/routes/vscode.ts +++ b/src/node/routes/vscode.ts @@ -205,8 +205,8 @@ export class CodeServerRouteWrapper { this.router.get("/", this.ensureCodeServerLoaded, this.$root) this.router.get("/manifest.json", this.manifest) this.router.post("/mint-key", this.mintKey) - this.router.all("*", ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyRequest) - this._wsRouterWrapper.ws("*", ensureOrigin, ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyWebsocket) + this.router.all(/.*/, ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyRequest) + this._wsRouterWrapper.ws(/.*/, ensureOrigin, ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyWebsocket) } dispose() { diff --git a/test/unit/node/proxy.test.ts b/test/unit/node/proxy.test.ts index fc347ff83..9e8017e37 100644 --- a/test/unit/node/proxy.test.ts +++ b/test/unit/node/proxy.test.ts @@ -199,7 +199,7 @@ describe("proxy", () => { }) it("should proxy non-ASCII", async () => { - e.get("*", (req, res) => { + e.get(/.*/, (req, res) => { res.json("ほげ") }) codeServer = await integration.setup(["--auth=none"], "") @@ -211,7 +211,7 @@ describe("proxy", () => { it("should not double-encode query variables", async () => { const spy = jest.fn() - e.get("*", (req, res) => { + e.get(/.*/, (req, res) => { spy([req.originalUrl, req.query]) res.end() })