From 8dc4fa4c84f5bc6cc87173479dda354d010861f4 Mon Sep 17 00:00:00 2001 From: Tom Fenech Date: Thu, 29 Feb 2024 18:33:03 +0100 Subject: [PATCH] fix: strip path from incoming request, not the upstream request --- proxy/proxy.go | 2 ++ proxy/proxy_test.go | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/proxy/proxy.go b/proxy/proxy.go index e21f253adb..83879077ff 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -197,7 +197,9 @@ func ConfigureBackendURL(r *http.Request, rl *rule.Rule) error { if rl.Upstream.StripPath != "" { forwardURL.Path = strings.Replace(forwardURL.Path, "/"+strings.Trim(rl.Upstream.StripPath, "/"), "", 1) + proxyPath = strings.Replace(proxyPath, "/"+strings.Trim(rl.Upstream.StripPath, "/"), "", 1) } + forwardURL.Path = "/" + strings.TrimLeft("/"+strings.Trim(backendPath, "/")+"/"+strings.TrimLeft(proxyPath, "/"), "/") r.Host = backendHost if rl.Upstream.PreserveHost { diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index 579919529a..54f31e6124 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -501,6 +501,12 @@ func TestConfigureBackendURL(t *testing.T) { eURL: "http://localhost:4000/foo/users/1234", eHost: "localhost:3000", }, + { + r: &http.Request{Host: "localhost:3000", URL: &url.URL{Path: "/foo/baz", Scheme: "http"}}, + rl: &rule.Rule{Upstream: rule.Upstream{URL: "http://localhost:4000/foo/bar", PreserveHost: true, StripPath: "foo"}}, + eURL: "http://localhost:4000/foo/bar/baz", + eHost: "localhost:3000", + }, } { t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { require.NoError(t, proxy.ConfigureBackendURL(tc.r, tc.rl))