@marcel91200 / caddy Mirror · read-only / commits / 80acf1bf
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 18:15 UTC

replacer: Fix escaped closing braces (#5995)

Aziz Rmadi committed Jan 13, 2024 at 14:24 UTC 80acf1bf23890697a12e25f84ae2c6520633da6f
4 files changed +70 -2
caddytest/integration/caddyfile_adapt/uri_replace_brace_escape.txt new
+47
@@ -0,0 +1,47 @@
1 +:9080
2 +uri replace "\}" %7D
3 +uri replace "\{" %7B
4 +
5 +respond "{query}"
6 +----------
7 +{
8 + "apps": {
9 + "http": {
10 + "servers": {
11 + "srv0": {
12 + "listen": [
13 + ":9080"
14 + ],
15 + "routes": [
16 + {
17 + "handle": [
18 + {
19 + "handler": "rewrite",
20 + "uri_substring": [
21 + {
22 + "find": "\\}",
23 + "replace": "%7D"
24 + }
25 + ]
26 + },
27 + {
28 + "handler": "rewrite",
29 + "uri_substring": [
30 + {
31 + "find": "\\{",
32 + "replace": "%7B"
33 + }
34 + ]
35 + },
36 + {
37 + "body": "{http.request.uri.query}",
38 + "handler": "static_response"
39 + }
40 + ]
41 + }
42 + ]
43 + }
44 + }
45 + }
46 + }
47 +}
\ No newline at end of file
caddytest/integration/caddyfile_test.go
+17
@@ -479,3 +479,20 @@ func TestValidPrefix(t *testing.T) {
479 caddytest.AssertAdapt(t, successCase.rawConfig, "caddyfile", successCase.expectedResponse)
480 }
481 }
482 +
483 +func TestUriReplace(t *testing.T) {
484 + tester := caddytest.NewTester(t)
485 +
486 + tester.InitServer(`
487 + {
488 + admin localhost:2999
489 + http_port 9080
490 + }
491 + :9080
492 + uri replace "\}" %7D
493 + uri replace "\{" %7B
494 +
495 + respond "{query}"`, "caddyfile")
496 +
497 + tester.AssertGetResponse("http://localhost:9080/endpoint?test={%20content%20}", 200, "test=%7B%20content%20%7D")
498 +}
replacer.go
+1 -1
@@ -133,7 +133,7 @@ func (r *Replacer) replace(input, empty string,
133 treatUnknownAsEmpty, errOnEmpty, errOnUnknown bool,
134 f ReplacementFunc,
135 ) (string, error) {
136 - if !strings.Contains(input, string(phOpen)) {
136 + if !strings.Contains(input, string(phOpen)) && !strings.Contains(input, string(phClose)) {
137 return input, nil
138 }
139
replacer_test.go
+5 -1
@@ -69,7 +69,7 @@ func TestReplacer(t *testing.T) {
69 },
70 {
71 input: `\}`,
72 - expect: `\}`,
72 + expect: `}`,
73 },
74 {
75 input: "{}",
@@ -164,6 +164,10 @@ func TestReplacer(t *testing.T) {
164 input: string([]byte{0x26, 0x00, 0x83, 0x7B, 0x84, 0x07, 0x5C, 0x7D, 0x84}),
165 expect: string([]byte{0x26, 0x00, 0x83, 0x7B, 0x84, 0x07, 0x7D, 0x84}),
166 },
167 + {
168 + input: `\\}`,
169 + expect: `\}`,
170 + },
171 } {
172 actual := rep.ReplaceAll(tc.input, tc.empty)
173 if actual != tc.expect {