diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2022-09-28 00:13:12 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2022-09-28 00:13:12 -0600 |
commit | 013b5103527cb1646dc717cadf48ad34aceaa77e (patch) | |
tree | e3e7ef08406562215f4e5a043430f94a08df19a1 | |
parent | e747a9bb12bc644aebce369061a2dc0de567d8d9 (diff) |
rewrite: Only trim prefix if matched
See #5073
-rw-r--r-- | modules/caddyhttp/rewrite/rewrite.go | 9 | ||||
-rw-r--r-- | modules/caddyhttp/rewrite/rewrite_test.go | 10 |
2 files changed, 17 insertions, 2 deletions
diff --git a/modules/caddyhttp/rewrite/rewrite.go b/modules/caddyhttp/rewrite/rewrite.go index 95bc504..31c9778 100644 --- a/modules/caddyhttp/rewrite/rewrite.go +++ b/modules/caddyhttp/rewrite/rewrite.go @@ -383,8 +383,13 @@ func trimPathPrefix(escapedPath, prefix string) string { iPrefix++ } - // found matching prefix, trim it - return escapedPath[iPath:] + // 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 { diff --git a/modules/caddyhttp/rewrite/rewrite_test.go b/modules/caddyhttp/rewrite/rewrite_test.go index bc20c85..5875983 100644 --- a/modules/caddyhttp/rewrite/rewrite_test.go +++ b/modules/caddyhttp/rewrite/rewrite_test.go @@ -227,6 +227,16 @@ func TestRewrite(t *testing.T) { }, { 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"), expect: newRequest(t, "GET", "/foo%2Fbar"), }, |