summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/rewrite
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2023-11-01 17:57:48 +0100
committerTom Barrett <tom@tombarrett.xyz>2023-11-01 18:11:33 +0100
commit240c3d1338415e5d82ef7ca0e52c4284be6441bd (patch)
tree4b0ee5d208c2cdffa78d65f1b0abe0ec85f15652 /modules/caddyhttp/rewrite
parent73e78ab226f21e6c6c68961af88c4ab9c746f4f4 (diff)
parent0e204b730aa2b1fa0835336b1117eff8c420f713 (diff)
vbump to v2.7.5
Diffstat (limited to 'modules/caddyhttp/rewrite')
-rw-r--r--modules/caddyhttp/rewrite/rewrite.go23
-rw-r--r--modules/caddyhttp/rewrite/rewrite_test.go12
2 files changed, 22 insertions, 13 deletions
diff --git a/modules/caddyhttp/rewrite/rewrite.go b/modules/caddyhttp/rewrite/rewrite.go
index 31c9778..77ef668 100644
--- a/modules/caddyhttp/rewrite/rewrite.go
+++ b/modules/caddyhttp/rewrite/rewrite.go
@@ -22,9 +22,10 @@ import (
"strconv"
"strings"
+ "go.uber.org/zap"
+
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
- "go.uber.org/zap"
)
func init() {
@@ -195,16 +196,10 @@ func (rewr Rewrite) Rewrite(r *http.Request, repl *caddy.Replacer) bool {
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)
+ // replace the `path` placeholder to escaped path
+ pathPlaceholder := "{http.request.uri.path}"
+ if strings.Contains(path, pathPlaceholder) {
+ path = strings.ReplaceAll(path, pathPlaceholder, r.URL.EscapedPath())
}
newPath = repl.ReplaceAll(path, "")
@@ -232,7 +227,11 @@ func (rewr Rewrite) Rewrite(r *http.Request, repl *caddy.Replacer) bool {
// update the URI with the new components
// only after building them
if pathStart >= 0 {
- r.URL.Path = newPath
+ if path, err := url.PathUnescape(newPath); err != nil {
+ r.URL.Path = newPath
+ } else {
+ r.URL.Path = path
+ }
}
if qsStart >= 0 {
r.URL.RawQuery = newQuery
diff --git a/modules/caddyhttp/rewrite/rewrite_test.go b/modules/caddyhttp/rewrite/rewrite_test.go
index 5875983..bb937ec 100644
--- a/modules/caddyhttp/rewrite/rewrite_test.go
+++ b/modules/caddyhttp/rewrite/rewrite_test.go
@@ -60,6 +60,16 @@ func TestRewrite(t *testing.T) {
expect: newRequest(t, "GET", "foo"),
},
{
+ rule: Rewrite{URI: "{http.request.uri}"},
+ input: newRequest(t, "GET", "/bar%3Fbaz?c=d"),
+ expect: newRequest(t, "GET", "/bar%3Fbaz?c=d"),
+ },
+ {
+ rule: Rewrite{URI: "{http.request.uri.path}"},
+ input: newRequest(t, "GET", "/bar%3Fbaz"),
+ expect: newRequest(t, "GET", "/bar%3Fbaz"),
+ },
+ {
rule: Rewrite{URI: "/foo{http.request.uri.path}"},
input: newRequest(t, "GET", "/bar"),
expect: newRequest(t, "GET", "/foo/bar"),
@@ -323,7 +333,7 @@ func TestRewrite(t *testing.T) {
input: newRequest(t, "GET", "/foo/findme%2Fbar"),
expect: newRequest(t, "GET", "/foo/replaced%2Fbar"),
},
-
+
{
rule: Rewrite{PathRegexp: []*regexReplacer{{Find: "/{2,}", Replace: "/"}}},
input: newRequest(t, "GET", "/foo//bar///baz?a=b//c"),