feat(eda): reject an args value whose checked CEL type cannot fill its Args field

This commit is contained in:
sam committed 2026-09-23 22:05:50 +10:00
1 parent 2bc4b8db32
commit 534153d50a
5 files changed
+121 -10

No files matched your search

+40 -1
View File
@@ -277,7 +277,7 @@ func (e *Env) Compile(src string) (*Program, error) {
return nil, err
}
ids, resolvable := actionReferences(ast)
return &Program{prg: prg, actionIDs: ids, resolvable: resolvable}, nil
return &Program{prg: prg, actionIDs: ids, resolvable: resolvable, outType: ast.OutputType()}, nil
}
// actionReferences walks the checked AST once and classifies every read of
@@ -328,6 +328,45 @@ type Program struct {
prg cel.Program
actionIDs []string
resolvable bool
outType *cel.Type
}
// Fits reports whether the program's checked output type can fill a Go value
// of type t. A dyn output fits anything: its type is known only per
// evaluation. A Go type with no CEL scalar counterpart is not checked here.
func (p *Program) Fits(t reflect.Type) bool {
if p.outType == nil || p.outType.Kind() == types.DynKind {
return true
}
want, ok := celScalar(t)
return !ok || want.IsExactType(p.outType)
}
// OutputType is the program's checked output type, for error messages.
func (p *Program) OutputType() string {
if p.outType == nil {
return "dyn"
}
return p.outType.String()
}
// celScalar maps a Go scalar kind to the CEL type an expression must produce
// to fill it.
func celScalar(t reflect.Type) (*cel.Type, bool) {
switch t.Kind() {
case reflect.String:
return cel.StringType, true
case reflect.Bool:
return cel.BoolType, true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return cel.IntType, true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return cel.UintType, true
case reflect.Float32, reflect.Float64:
return cel.DoubleType, true
default:
return nil, false
}
}
// ActionReferences reports the action ids the expression reads from the
+26
View File
@@ -448,6 +448,27 @@ func argsLabel(label, key string) string {
return fmt.Sprintf("%s args[%q]", label, key)
}
// checkArgType refuses an args value whose checked CEL type cannot fill the
// Args field it names. A dyn value passes: the module decode checks it per
// event.
func checkArgType(path, label, kind string, argsSchema reflect.Type, key string, prg *expr.Program) error {
t := argsSchema
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
for field := range t.Fields() {
if strings.ToLower(field.Name) != key || prg.Fits(field.Type) {
continue
}
loc := kind + " kind"
if label != "" {
loc = label + " " + loc
}
return fmt.Errorf("playbook %s: %s args[%q] is %s, want %s", path, loc, key, prg.OutputType(), field.Type)
}
return nil
}
// compileArgs compiles every args value as a CEL expression at load, keyed by
// arg name. J2 has no literal/expression split: the YAML scalar text IS the
// CEL source, so a string literal is quoted inside YAML and a number or
@@ -476,6 +497,11 @@ func compileArgs(path, label, kind string, raw map[string]string, env *expr.Env,
if err != nil {
return nil, err
}
if argsSchema != nil {
if err := checkArgType(path, label, kind, argsSchema, key, prg); err != nil {
return nil, err
}
}
args[key] = prg
}
return args, nil
@@ -1443,3 +1443,36 @@ func TestLoadActionPlaybookRejectsActionReferences(t *testing.T) {
})
}
}
// An args value whose static CEL type cannot fill the kind's Args field fails
// the load, naming the key. A dyn value (an event read) is accepted: its type
// is known only per event, and the module decode refuses a bad one at run.
func TestLoadArgsValueTypeAgainstArgsSchema(t *testing.T) {
tests := []struct {
name string
message string
wantErr bool
}{
{name: "string literal", message: `'"hello"'`},
{name: "string concatenation", message: `'"n=" + string(1)'`},
{name: "dyn event read", message: `event.title`},
{name: "int literal", message: `'123'`, wantErr: true},
{name: "bool expression", message: `'1 == 1'`, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
writeFile(t, dir, "pb.yaml", "trigger:\n kind: bug\nactions:\n - position: module\n kind: log\n args:\n message: "+tt.message+"\n")
_, err := Load(dir, testSchemas(t))
if !tt.wantErr {
if err != nil {
t.Fatalf("Load: %v", err)
}
return
}
if err == nil || !strings.Contains(err.Error(), `args["message"]`) || !strings.Contains(err.Error(), "string") {
t.Fatalf("Load error = %v, want args[\"message\"] refused for not being a string", err)
}
})
}
}