Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 00:15 UTC
rewrite: Fix a double-encode issue when using the `{uri}` placeholder (#4516)
Francis Lavoie committed
Jan 13, 2022 at 12:17 UTC
1feb65952acd814f9abcfb8e38f727f4e49e9e68
2 files changed
+28
modules/caddyhttp/rewrite/rewrite.go
+13
@@ -168,7 +168,20 @@ func (rewr Rewrite) rewrite(r *http.Request, repl *caddy.Replacer, logger *zap.L
168
// in a temporary variable so that they all read the
169
// same version of the URI
170
var newPath, newQuery, newFrag string
171
+
172
if path != "" {
173
+ // Since the 'uri' placeholder performs a URL-encode,
174
+ // we need to intercept it so that it doesn't, because
175
+ // otherwise we risk a double-encode of the path.
176
+ uriPlaceholder := "{http.request.uri}"
177
+ if strings.Contains(path, uriPlaceholder) {
178
+ tmpUri := r.URL.Path
179
+ if r.URL.RawQuery != "" {
180
+ tmpUri += "?" + r.URL.RawQuery
181
+ }
182
+ path = strings.ReplaceAll(path, uriPlaceholder, tmpUri)
183
+ }
184
+
185
newPath = repl.ReplaceAll(path, "")
186
}
187
modules/caddyhttp/rewrite/rewrite_test.go
+15
@@ -189,6 +189,21 @@ func TestRewrite(t *testing.T) {
189
input: newRequest(t, "GET", "/foo/?a=b"),
190
expect: newRequest(t, "GET", "/foo/bar?c=d"),
191
},
192
+ {
193
+ rule: Rewrite{URI: "/i{http.request.uri}"},
194
+ input: newRequest(t, "GET", "/%C2%B7%E2%88%B5.png"),
195
+ expect: newRequest(t, "GET", "/i/%C2%B7%E2%88%B5.png"),
196
+ },
197
+ {
198
+ rule: Rewrite{URI: "/i{http.request.uri}"},
199
+ input: newRequest(t, "GET", "/·∵.png?a=b"),
200
+ expect: newRequest(t, "GET", "/i/%C2%B7%E2%88%B5.png?a=b"),
201
+ },
202
+ {
203
+ rule: Rewrite{URI: "/i{http.request.uri}"},
204
+ input: newRequest(t, "GET", "/%C2%B7%E2%88%B5.png?a=b"),
205
+ expect: newRequest(t, "GET", "/i/%C2%B7%E2%88%B5.png?a=b"),
206
+ },
207
208
{
209
rule: Rewrite{StripPathPrefix: "/prefix"},