diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index d36bc65ea..db47cc655 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -477,7 +477,7 @@ func interpolationFuncElement() ast.Function { list := StringList(args[0].(string)) index, err := strconv.Atoi(args[1].(string)) - if err != nil { + if err != nil || index < 0 { return "", fmt.Errorf( "invalid number for index, got %s", args[1]) } diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 2a2981b76..fdccec2ea 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -778,6 +778,14 @@ func TestInterpolateFuncElement(t *testing.T) { false, }, + // Negative number should fail + { + fmt.Sprintf(`${element("%s", "-1")}`, + NewStringList([]string{"foo"}).String()), + nil, + true, + }, + // Too many args { fmt.Sprintf(`${element("%s", "0", "2")}`,