Skip to content

Commit d7fe04b

Browse files
committed
checker: forbid accessing custom functions from $env
1 parent 87fda67 commit d7fe04b

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

checker/checker.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ func (v *checker) IdentifierNode(node *ast.IdentifierNode) (reflect.Type, info)
157157
if node.Value == "$env" {
158158
return mapType, info{}
159159
}
160+
if fn, ok := v.config.Builtins[node.Value]; ok {
161+
return functionType, info{fn: fn}
162+
}
163+
if fn, ok := v.config.Functions[node.Value]; ok {
164+
return functionType, info{fn: fn}
165+
}
160166
return v.env(node, node.Value, true)
161167
}
162168

@@ -166,13 +172,9 @@ type NodeWithIndexes interface {
166172
SetMethodIndex(methodIndex int)
167173
}
168174

175+
// env method returns type of environment variable. env only lookups for
176+
// environment variables, no builtins, no custom functions.
169177
func (v *checker) env(node NodeWithIndexes, name string, strict bool) (reflect.Type, info) {
170-
if fn, ok := v.config.Builtins[name]; ok {
171-
return functionType, info{fn: fn}
172-
}
173-
if fn, ok := v.config.Functions[name]; ok {
174-
return functionType, info{fn: fn}
175-
}
176178
if t, ok := v.config.Types[name]; ok {
177179
if t.Ambiguous {
178180
return v.error(node, "ambiguous identifier %v", name)

expr_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,32 @@ func TestEnv_keyword(t *testing.T) {
19711971

19721972
})
19731973
}
1974+
}
1975+
1976+
func TestEnv_keyword_with_custom_functions(t *testing.T) {
1977+
fn := expr.Function("fn", func(params ...any) (any, error) {
1978+
return "ok", nil
1979+
})
1980+
1981+
var tests = []struct {
1982+
code string
1983+
error bool
1984+
}{
1985+
{`fn()`, false},
1986+
{`$env.fn()`, true},
1987+
{`$env["fn"]`, true},
1988+
}
19741989

1990+
for _, tt := range tests {
1991+
t.Run(tt.code, func(t *testing.T) {
1992+
_, err := expr.Compile(tt.code, expr.Env(mock.Env{}), fn)
1993+
if tt.error {
1994+
require.Error(t, err)
1995+
} else {
1996+
require.NoError(t, err)
1997+
}
1998+
})
1999+
}
19752000
}
19762001

19772002
func TestIssue401(t *testing.T) {

0 commit comments

Comments
 (0)