summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/rewrite
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2022-01-13 12:17:15 -0500
committerGitHub <noreply@github.com>2022-01-13 12:17:15 -0500
commit1feb65952acd814f9abcfb8e38f727f4e49e9e68 (patch)
tree8824bcfab94e37f4bd5b73e3615fef898d5efc26 /modules/caddyhttp/rewrite
parent66de438a98989135dc1e4ce5aeb0c4bc98af8049 (diff)
rewrite: Fix a double-encode issue when using the `{uri}` placeholder (#4516)
Diffstat (limited to 'modules/caddyhttp/rewrite')
-rw-r--r--modules/caddyhttp/rewrite/rewrite.go13
-rw-r--r--modules/caddyhttp/rewrite/rewrite_test.go15
2 files changed, 28 insertions, 0 deletions
diff --git a/modules/caddyhttp/rewrite/rewrite.go b/modules/caddyhttp/rewrite/rewrite.go
index fbf655c..cd340e3 100644
--- a/modules/caddyhttp/rewrite/rewrite.go
+++ b/modules/caddyhttp/rewrite/rewrite.go
@@ -168,7 +168,20 @@ func (rewr Rewrite) rewrite(r *http.Request, repl *caddy.Replacer, logger *zap.L
// in a temporary variable so that they all read the
// same version of the URI
var newPath, newQuery, newFrag string
+
if path != "" {
+ // Since the 'uri' placeholder performs a URL-encode,
+ // we need to intercept it so that it doesn't, because
+ // otherwise we risk a double-encode of the path.
+ uriPlaceholder := "{http.request.uri}"
+ if strings.Contains(path, uriPlaceholder) {
+ tmpUri := r.URL.Path
+ if r.URL.RawQuery != "" {
+ tmpUri += "?" + r.URL.RawQuery
+ }
+ path = strings.ReplaceAll(path, uriPlaceholder, tmpUri)
+ }
+
newPath = repl.ReplaceAll(path, "")
}
diff --git a/modules/caddyhttp/rewrite/rewrite_test.go b/modules/caddyhttp/rewrite/rewrite_test.go
index 4d595e2..38d96fe 100644
--- a/modules/caddyhttp/rewrite/rewrite_test.go
+++ b/modules/caddyhttp/rewrite/rewrite_test.go
@@ -189,6 +189,21 @@ func TestRewrite(t *testing.T) {
input: newRequest(t, "GET", "/foo/?a=b"),
expect: newRequest(t, "GET", "/foo/bar?c=d"),
},
+ {
+ rule: Rewrite{URI: "/i{http.request.uri}"},
+ input: newRequest(t, "GET", "/%C2%B7%E2%88%B5.png"),
+ expect: newRequest(t, "GET", "/i/%C2%B7%E2%88%B5.png"),
+ },
+ {
+ rule: Rewrite{URI: "/i{http.request.uri}"},
+ input: newRequest(t, "GET", "/·∵.png?a=b"),
+ expect: newRequest(t, "GET", "/i/%C2%B7%E2%88%B5.png?a=b"),
+ },
+ {
+ rule: Rewrite{URI: "/i{http.request.uri}"},
+ input: newRequest(t, "GET", "/%C2%B7%E2%88%B5.png?a=b"),
+ expect: newRequest(t, "GET", "/i/%C2%B7%E2%88%B5.png?a=b"),
+ },
{
rule: Rewrite{StripPathPrefix: "/prefix"},