Skip to content

Commit 5e29536

Browse files
authored
caddyhttp: add replacer placeholders for escaped values (#7181)
1 parent 551f793 commit 5e29536

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

caddyconfig/httpcaddyfile/shorthands.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ func placeholderShorthands() []string {
6464
"{orig_?query}", "{http.request.orig_uri.prefixed_query}",
6565
"{method}", "{http.request.method}",
6666
"{uri}", "{http.request.uri}",
67+
"{%uri}", "{http.request.uri_escaped}",
6768
"{path}", "{http.request.uri.path}",
69+
"{%path}", "{http.request.uri.path_escaped}",
6870
"{dir}", "{http.request.uri.path.dir}",
6971
"{file}", "{http.request.uri.path.file}",
7072
"{query}", "{http.request.uri.query}",
73+
"{%query}", "{http.request.uri.query_escaped}",
7174
"{?query}", "{http.request.uri.prefixed_query}",
7275
"{remote}", "{http.request.remote}",
7376
"{remote_host}", "{http.request.remote.host}",

modules/caddyhttp/replacer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,12 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
172172
// current URI, including any internal rewrites
173173
case "http.request.uri":
174174
return req.URL.RequestURI(), true
175+
case "http.request.uri_escaped":
176+
return url.QueryEscape(req.URL.RequestURI()), true
175177
case "http.request.uri.path":
176178
return req.URL.Path, true
179+
case "http.request.uri.path_escaped":
180+
return url.QueryEscape(req.URL.Path), true
177181
case "http.request.uri.path.file":
178182
_, file := path.Split(req.URL.Path)
179183
return file, true
@@ -186,6 +190,8 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
186190
return path.Ext(req.URL.Path), true
187191
case "http.request.uri.query":
188192
return req.URL.RawQuery, true
193+
case "http.request.uri.query_escaped":
194+
return url.QueryEscape(req.URL.RawQuery), true
189195
case "http.request.uri.prefixed_query":
190196
if req.URL.RawQuery == "" {
191197
return "", true

modules/caddyhttp/replacer_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
)
2929

3030
func TestHTTPVarReplacement(t *testing.T) {
31-
req, _ := http.NewRequest(http.MethodGet, "/foo/bar.tar.gz", nil)
31+
req, _ := http.NewRequest(http.MethodGet, "/foo/bar.tar.gz?a=1&b=2", nil)
3232
repl := caddy.NewReplacer()
3333
localAddr, _ := net.ResolveTCPAddr("tcp", "192.168.159.1:80")
3434
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
@@ -142,6 +142,22 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
142142
get: "http.request.host.labels.2",
143143
expect: "",
144144
},
145+
{
146+
get: "http.request.uri",
147+
expect: "/foo/bar.tar.gz?a=1&b=2",
148+
},
149+
{
150+
get: "http.request.uri_escaped",
151+
expect: "%2Ffoo%2Fbar.tar.gz%3Fa%3D1%26b%3D2",
152+
},
153+
{
154+
get: "http.request.uri.path",
155+
expect: "/foo/bar.tar.gz",
156+
},
157+
{
158+
get: "http.request.uri.path_escaped",
159+
expect: "%2Ffoo%2Fbar.tar.gz",
160+
},
145161
{
146162
get: "http.request.uri.path.file",
147163
expect: "bar.tar.gz",
@@ -155,6 +171,26 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
155171
get: "http.request.uri.path.file.ext",
156172
expect: ".gz",
157173
},
174+
{
175+
get: "http.request.uri.query",
176+
expect: "a=1&b=2",
177+
},
178+
{
179+
get: "http.request.uri.query_escaped",
180+
expect: "a%3D1%26b%3D2",
181+
},
182+
{
183+
get: "http.request.uri.query.a",
184+
expect: "1",
185+
},
186+
{
187+
get: "http.request.uri.query.b",
188+
expect: "2",
189+
},
190+
{
191+
get: "http.request.uri.prefixed_query",
192+
expect: "?a=1&b=2",
193+
},
158194
{
159195
get: "http.request.tls.cipher_suite",
160196
expect: "TLS_AES_256_GCM_SHA384",

0 commit comments

Comments
 (0)