Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 10:30 UTC
rewrite: Only trim prefix if matched
See #5073
Matthew Holt committed
Sep 28, 2022 at 00:13 UTC
013b5103527cb1646dc717cadf48ad34aceaa77e
2 files changed
+17
-2
modules/caddyhttp/rewrite/rewrite.go
+7
-2
@@ -383,8 +383,13 @@ func trimPathPrefix(escapedPath, prefix string) string {
383
iPrefix++
384
}
385
386
- // found matching prefix, trim it
387
- return escapedPath[iPath:]
386
+ // if we iterated through the entire prefix, we found it, so trim it
387
+ if iPath >= len(prefix) {
388
+ return escapedPath[iPath:]
389
+ }
390
+
391
+ // otherwise we did not find the prefix
392
+ return escapedPath
393
}
394
395
func reverse(s string) string {
modules/caddyhttp/rewrite/rewrite_test.go
+10
@@ -225,6 +225,16 @@ func TestRewrite(t *testing.T) {
225
input: newRequest(t, "GET", "/prefix/foo/bar"),
226
expect: newRequest(t, "GET", "/foo/bar"),
227
},
228
+ {
229
+ rule: Rewrite{StripPathPrefix: "/prefix"},
230
+ input: newRequest(t, "GET", "/prefix"),
231
+ expect: newRequest(t, "GET", ""),
232
+ },
233
+ {
234
+ rule: Rewrite{StripPathPrefix: "/prefix"},
235
+ input: newRequest(t, "GET", "/"),
236
+ expect: newRequest(t, "GET", "/"),
237
+ },
238
{
239
rule: Rewrite{StripPathPrefix: "/prefix"},
240
input: newRequest(t, "GET", "/prefix/foo%2Fbar"),