mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-02 14:17:01 +01:00
caddyfile: check that matched key is not a substring of the replacement key (#5685)
This commit is contained in:
parent
b07b198764
commit
9f34383c02
2 changed files with 40 additions and 0 deletions
|
@ -93,6 +93,11 @@ func makeArgsReplacer(args []string) *caddy.Replacer {
|
||||||
// TODO: Remove the deprecated {args.*} placeholder
|
// TODO: Remove the deprecated {args.*} placeholder
|
||||||
// support at some point in the future
|
// support at some point in the future
|
||||||
if matches := argsRegexpIndexDeprecated.FindStringSubmatch(key); len(matches) > 0 {
|
if matches := argsRegexpIndexDeprecated.FindStringSubmatch(key); len(matches) > 0 {
|
||||||
|
// What's matched may be a substring of the key
|
||||||
|
if matches[0] != key {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
value, err := strconv.Atoi(matches[1])
|
value, err := strconv.Atoi(matches[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
caddy.Log().Named("caddyfile").Warn(
|
caddy.Log().Named("caddyfile").Warn(
|
||||||
|
@ -111,6 +116,11 @@ func makeArgsReplacer(args []string) *caddy.Replacer {
|
||||||
|
|
||||||
// Handle args[*] form
|
// Handle args[*] form
|
||||||
if matches := argsRegexpIndex.FindStringSubmatch(key); len(matches) > 0 {
|
if matches := argsRegexpIndex.FindStringSubmatch(key); len(matches) > 0 {
|
||||||
|
// What's matched may be a substring of the key
|
||||||
|
if matches[0] != key {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
if strings.Contains(matches[1], ":") {
|
if strings.Contains(matches[1], ":") {
|
||||||
caddy.Log().Named("caddyfile").Warn(
|
caddy.Log().Named("caddyfile").Warn(
|
||||||
"Variadic placeholder {args[" + matches[1] + "]} must be a token on its own")
|
"Variadic placeholder {args[" + matches[1] + "]} must be a token on its own")
|
||||||
|
|
|
@ -718,6 +718,36 @@ func TestEnvironmentReplacement(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImportReplacementInJSONWithBrace(t *testing.T) {
|
||||||
|
for i, test := range []struct {
|
||||||
|
args []string
|
||||||
|
input string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
args: []string{"123"},
|
||||||
|
input: "{args[0]}",
|
||||||
|
expect: "123",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
args: []string{"123"},
|
||||||
|
input: `{"key":"{args[0]}"}`,
|
||||||
|
expect: `{"key":"123"}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
args: []string{"123", "123"},
|
||||||
|
input: `{"key":[{args[0]},{args[1]}]}`,
|
||||||
|
expect: `{"key":[123,123]}`,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
repl := makeArgsReplacer(test.args)
|
||||||
|
actual := repl.ReplaceKnown(test.input, "")
|
||||||
|
if actual != test.expect {
|
||||||
|
t.Errorf("Test %d: Expected: '%s' but got '%s'", i, test.expect, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSnippets(t *testing.T) {
|
func TestSnippets(t *testing.T) {
|
||||||
p := testParser(`
|
p := testParser(`
|
||||||
(common) {
|
(common) {
|
||||||
|
|
Loading…
Reference in a new issue