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,8 +383,13 @@ func trimPathPrefix(escapedPath, prefix string) string {
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:]
}
// otherwise we did not find the prefix
return escapedPath
}
func reverse(s string) string {

View File

@ -225,6 +225,16 @@ func TestRewrite(t *testing.T) {
input: newRequest(t, "GET", "/prefix/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"},
input: newRequest(t, "GET", "/prefix/foo%2Fbar"),