rewrite: Only trim prefix if matched

See #5073
This commit is contained in:
Matthew Holt 2022-09-28 00:13:12 -06:00
parent e747a9bb12
commit 013b510352
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 17 additions and 2 deletions

View file

@ -383,10 +383,15 @@ func trimPathPrefix(escapedPath, prefix string) string {
iPrefix++ iPrefix++
} }
// found matching prefix, trim it // if we iterated through the entire prefix, we found it, so trim it
if iPath >= len(prefix) {
return escapedPath[iPath:] return escapedPath[iPath:]
} }
// otherwise we did not find the prefix
return escapedPath
}
func reverse(s string) string { func reverse(s string) string {
r := []rune(s) r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {

View file

@ -225,6 +225,16 @@ func TestRewrite(t *testing.T) {
input: newRequest(t, "GET", "/prefix/foo/bar"), input: newRequest(t, "GET", "/prefix/foo/bar"),
expect: newRequest(t, "GET", "/foo/bar"), expect: newRequest(t, "GET", "/foo/bar"),
}, },
{
rule: Rewrite{StripPathPrefix: "/prefix"},
input: newRequest(t, "GET", "/prefix"),
expect: newRequest(t, "GET", ""),
},
{
rule: Rewrite{StripPathPrefix: "/prefix"},
input: newRequest(t, "GET", "/"),
expect: newRequest(t, "GET", "/"),
},
{ {
rule: Rewrite{StripPathPrefix: "/prefix"}, rule: Rewrite{StripPathPrefix: "/prefix"},
input: newRequest(t, "GET", "/prefix/foo%2Fbar"), input: newRequest(t, "GET", "/prefix/foo%2Fbar"),