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.
This commit is contained in:
Asher 2024-04-16 09:50:36 -08:00
parent 417c1f36cb
commit b8d830b826
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
5 changed files with 11 additions and 11 deletions

View File

@ -53,7 +53,7 @@ const maybeProxy = (req: Request): string | undefined => {
return undefined return undefined
} }
router.all("*", async (req, res, next) => { router.all(/.*/, async (req, res, next) => {
const port = maybeProxy(req) const port = maybeProxy(req)
if (!port) { if (!port) {
return next() return next()
@ -97,7 +97,7 @@ router.all("*", async (req, res, next) => {
export const wsRouter = WsRouter() export const wsRouter = WsRouter()
wsRouter.ws("*", async (req, _, next) => { wsRouter.ws(/.*/, async (req, _, next) => {
const port = maybeProxy(req) const port = maybeProxy(req)
if (!port) { if (!port) {
return next() return next()

View File

@ -109,21 +109,21 @@ export const register = async (app: App, args: DefaultedArgs): Promise<Disposabl
app.router.use("/", domainProxy.router) app.router.use("/", domainProxy.router)
app.wsRouter.use("/", domainProxy.wsRouter.router) app.wsRouter.use("/", domainProxy.wsRouter.router)
app.router.all("/proxy/(:port)(/*)?", async (req, res) => { app.router.all("/proxy/:port/:path(.*)?", async (req, res) => {
await pathProxy.proxy(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) await pathProxy.wsProxy(req as pluginapi.WebsocketRequest)
}) })
// These two routes pass through the path directly. // These two routes pass through the path directly.
// So the proxied app must be aware it is running // So the proxied app must be aware it is running
// under /absproxy/<someport>/ // under /absproxy/<someport>/
app.router.all("/absproxy/(:port)(/*)?", async (req, res) => { app.router.all("/absproxy/:port/:path(.*)?", async (req, res) => {
await pathProxy.proxy(req, res, { await pathProxy.proxy(req, res, {
passthroughPath: true, passthroughPath: true,
}) })
}) })
app.wsRouter.get("/absproxy/(:port)(/*)?", async (req) => { app.wsRouter.get("/absproxy/:port/:path(.*)?", async (req) => {
await pathProxy.wsProxy(req as pluginapi.WebsocketRequest, { await pathProxy.wsProxy(req as pluginapi.WebsocketRequest, {
passthroughPath: true, passthroughPath: true,
}) })

View File

@ -22,7 +22,7 @@ export async function proxy(
if (!(await authenticated(req))) { if (!(await authenticated(req))) {
// If visiting the root (/:port only) redirect to the login page. // 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) const to = self(req)
return redirect(req, res, "login", { return redirect(req, res, "login", {
to: to !== "/" ? to : undefined, to: to !== "/" ? to : undefined,

View File

@ -205,8 +205,8 @@ export class CodeServerRouteWrapper {
this.router.get("/", this.ensureCodeServerLoaded, this.$root) this.router.get("/", this.ensureCodeServerLoaded, this.$root)
this.router.get("/manifest.json", this.manifest) this.router.get("/manifest.json", this.manifest)
this.router.post("/mint-key", this.mintKey) this.router.post("/mint-key", this.mintKey)
this.router.all("*", ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyRequest) this.router.all(/.*/, ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyRequest)
this._wsRouterWrapper.ws("*", ensureOrigin, ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyWebsocket) this._wsRouterWrapper.ws(/.*/, ensureOrigin, ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyWebsocket)
} }
dispose() { dispose() {

View File

@ -199,7 +199,7 @@ describe("proxy", () => {
}) })
it("should proxy non-ASCII", async () => { it("should proxy non-ASCII", async () => {
e.get("*", (req, res) => { e.get(/.*/, (req, res) => {
res.json("ほげ") res.json("ほげ")
}) })
codeServer = await integration.setup(["--auth=none"], "") codeServer = await integration.setup(["--auth=none"], "")
@ -211,7 +211,7 @@ describe("proxy", () => {
it("should not double-encode query variables", async () => { it("should not double-encode query variables", async () => {
const spy = jest.fn() const spy = jest.fn()
e.get("*", (req, res) => { e.get(/.*/, (req, res) => {
spy([req.originalUrl, req.query]) spy([req.originalUrl, req.query])
res.end() res.end()
}) })