proxy.ts: Do not always rewrite redirects against the base path

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
This commit is contained in:
Anmol Sethi 2021-01-20 14:26:15 -05:00
parent 5446e0ad43
commit f5cf3fd331
No known key found for this signature in database
GPG Key ID: 8CEF1878FF10ADEB
2 changed files with 6 additions and 2 deletions

View File

@ -9,6 +9,8 @@ proxy.on("error", (error, _, res) => {
}) })
// Intercept the response to rewrite absolute redirects against the base path. // 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) => { proxy.on("proxyRes", (res, req) => {
if (res.headers.location && res.headers.location.startsWith("/") && (req as any).base) { if (res.headers.location && res.headers.location.startsWith("/") && (req as any).base) {
res.headers.location = (req as any).base + res.headers.location res.headers.location = (req as any).base + res.headers.location

View File

@ -28,8 +28,10 @@ router.all("/(:port)(/*)?", (req, res) => {
throw new HttpError("Unauthorized", HttpCode.Unauthorized) throw new HttpError("Unauthorized", HttpCode.Unauthorized)
} }
if (!req.args["proxy-path-passthrough"]) {
// Absolute redirects need to be based on the subpath when rewriting. // Absolute redirects need to be based on the subpath when rewriting.
;(req as any).base = `${req.baseUrl}/${req.params.port}` ;(req as any).base = `${req.baseUrl}/${req.params.port}`
}
proxy.web(req, res, { proxy.web(req, res, {
ignorePath: true, ignorePath: true,