Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replacer: Fix escaped closing braces #5995

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions caddytest/integration/caddyfile_adapt/uri_replace_brace_escape.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
:9080
uri replace "\}" %7D
uri replace "\{" %7B

respond "{query}"
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":9080"
],
"routes": [
{
"handle": [
{
"handler": "rewrite",
"uri_substring": [
{
"find": "\\}",
"replace": "%7D"
}
]
},
{
"handler": "rewrite",
"uri_substring": [
{
"find": "\\{",
"replace": "%7B"
}
]
},
{
"body": "{http.request.uri.query}",
"handler": "static_response"
}
]
}
]
}
}
}
}
}
17 changes: 17 additions & 0 deletions caddytest/integration/caddyfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,20 @@ func TestValidPrefix(t *testing.T) {
caddytest.AssertAdapt(t, successCase.rawConfig, "caddyfile", successCase.expectedResponse)
}
}

func TestUriReplace(t *testing.T) {
tester := caddytest.NewTester(t)

tester.InitServer(`
{
admin localhost:2999
http_port 9080
}
:9080
uri replace "\}" %7D
uri replace "\{" %7B

respond "{query}"`, "caddyfile")

tester.AssertGetResponse("http://localhost:9080/endpoint?test={%20content%20}", 200, "test=%7B%20content%20%7D")
}
2 changes: 1 addition & 1 deletion replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (r *Replacer) replace(input, empty string,
treatUnknownAsEmpty, errOnEmpty, errOnUnknown bool,
f ReplacementFunc,
) (string, error) {
if !strings.Contains(input, string(phOpen)) {
if !strings.Contains(input, string(phOpen)) && !strings.Contains(input, string(phClose)) {
return input, nil
}

Expand Down
6 changes: 5 additions & 1 deletion replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestReplacer(t *testing.T) {
},
{
input: `\}`,
expect: `\}`,
expect: `}`,
},
{
input: "{}",
Expand Down Expand Up @@ -164,6 +164,10 @@ func TestReplacer(t *testing.T) {
input: string([]byte{0x26, 0x00, 0x83, 0x7B, 0x84, 0x07, 0x5C, 0x7D, 0x84}),
expect: string([]byte{0x26, 0x00, 0x83, 0x7B, 0x84, 0x07, 0x7D, 0x84}),
},
{
input: `\\}`,
expect: `\}`,
},
} {
actual := rep.ReplaceAll(tc.input, tc.empty)
if actual != tc.expect {
Expand Down
Loading