Skip to content

Commit

Permalink
proxy.ts: Do not always rewrite redirects against the base path
Browse files Browse the repository at this point in the history
This breaks --proxy-path-passthrough

However, we still need this when that code is disabled as many apps will
issue absolute redirects and expect the proxy to rewrite as appropriate.

e.g. Go's http.Redirect will rewrite relative redirects as absolute!
See https://golang.org/pkg/net/http/#Redirect
  • Loading branch information
nhooyr committed Jan 20, 2021
1 parent 28e98c0 commit e5d0b49
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/node/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ proxy.on("error", (error, _, res) => {
})

// Intercept the response to rewrite absolute redirects against the base path.
// Is disabled when the request has no base path which means --proxy-path-passthrough has
// been enabled.
proxy.on("proxyRes", (res, req) => {
if (res.headers.location && res.headers.location.startsWith("/") && (req as any).base) {
res.headers.location = (req as any).base + res.headers.location
Expand Down
6 changes: 4 additions & 2 deletions src/node/routes/pathProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ router.all("/(:port)(/*)?", (req, res) => {
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
}

// Absolute redirects need to be based on the subpath when rewriting.
;(req as any).base = `${req.baseUrl}/${req.params.port}`
if (!req.args["proxy-path-passthrough"]) {
// Absolute redirects need to be based on the subpath when rewriting.
;(req as any).base = `${req.baseUrl}/${req.params.port}`
}

proxy.web(req, res, {
ignorePath: true,
Expand Down

0 comments on commit e5d0b49

Please sign in to comment.