Skip to content

Commit 561b634

Browse files
committed
Ensure a trailing slash on subpath proxy
1 parent e68d72c commit 561b634

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

doc/FAQ.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Now you can browse to `<port>.coder.com`. Note that this uses the host header so
8383
ensure your reverse proxy forwards that information if you are using one.
8484

8585
### Sub-paths
86-
Just browse to `/proxy/<port>`.
86+
Just browse to `/proxy/<port>/`.
8787

8888
## x86 releases?
8989

src/node/app/proxy.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,24 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
3030
request: http.IncomingMessage,
3131
response: http.ServerResponse,
3232
): Promise<HttpResponse> {
33+
const isRoot = !route.requestPath || route.requestPath === "/index.html"
3334
if (!this.authenticated(request)) {
3435
// Only redirect from the root. Other requests get an unauthorized error.
35-
if (route.requestPath && route.requestPath !== "/index.html") {
36-
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
36+
if (isRoot) {
37+
return { redirect: "/login", query: { to: route.fullPath } }
3738
}
38-
return { redirect: "/login", query: { to: route.fullPath } }
39+
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
3940
}
4041

41-
const payload = this.doProxy(route.requestPath, request, response, route.base.replace(/^\//, ""))
42+
// Ensure there is a trailing slash so relative paths work correctly.
43+
const base = route.base.replace(/^\//, "")
44+
if (isRoot && !route.originalPath.endsWith("/")) {
45+
return {
46+
redirect: `/proxy/${base}/`,
47+
}
48+
}
49+
50+
const payload = this.doProxy(route.requestPath, request, response, base)
4251
if (payload) {
4352
return payload
4453
}

0 commit comments

Comments
 (0)