From 1feb65952acd814f9abcfb8e38f727f4e49e9e68 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Thu, 13 Jan 2022 12:17:15 -0500 Subject: rewrite: Fix a double-encode issue when using the `{uri}` placeholder (#4516) --- modules/caddyhttp/rewrite/rewrite.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'modules/caddyhttp/rewrite/rewrite.go') 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, "") } -- cgit v1.2.3