fix(eda): reject map-index actions references to unknown prior ids
This commit is contained in:
1 parent
ed92494348
commit
f1b53d666f
3 files changed
+78
-35
No files matched your search
@@ -86,28 +86,38 @@ func (e *Env) Compile(src string) (*Program, error) {
|
||||
}
|
||||
|
||||
// referencedActionIDs walks the compiled AST and returns the sorted, de-
|
||||
// duplicated set of action ids an expression reads through `actions.<id>`.
|
||||
// `actions` is declared map(string,dyn), so CEL type-checking cannot reject
|
||||
// an unknown id; the playbook loader compares this set against the ids of
|
||||
// prior actions to reject unknown references at load (J1 in
|
||||
// docs/prds/playbook-expression-syntax.md). The map-index form
|
||||
// (`actions["notify"]`) is a runtime lookup, not a field selection, and is
|
||||
// intentionally not collected here.
|
||||
// duplicated set of action ids an expression reads from `actions`. Both
|
||||
// field-selection (`actions.notify`) and literal map-index
|
||||
// (`actions["notify"]`) forms are collected; `actions` is declared
|
||||
// map(string,dyn), so CEL type-checking cannot reject an unknown id, and the
|
||||
// playbook loader compares this set against the ids of prior actions to
|
||||
// reject unknown references at load (J1 in
|
||||
// docs/prds/playbook-expression-syntax.md). A non-literal index key
|
||||
// (`actions[key]`) is a runtime lookup whose id cannot be known at load.
|
||||
func referencedActionIDs(ast *cel.Ast) []string {
|
||||
if ast == nil || ast.NativeRep() == nil {
|
||||
return nil
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
visitor := celast.NewExprVisitor(func(e celast.Expr) {
|
||||
if e.Kind() != celast.SelectKind {
|
||||
return
|
||||
switch e.Kind() {
|
||||
case celast.SelectKind:
|
||||
sel := e.AsSelect()
|
||||
if isActionsIdent(sel.Operand()) {
|
||||
seen[sel.FieldName()] = struct{}{}
|
||||
}
|
||||
case celast.CallKind:
|
||||
call := e.AsCall()
|
||||
if call.FunctionName() != "_[_]" || len(call.Args()) != 2 {
|
||||
return
|
||||
}
|
||||
if !isActionsIdent(call.Args()[0]) || call.Args()[1].Kind() != celast.LiteralKind {
|
||||
return
|
||||
}
|
||||
if id, ok := call.Args()[1].AsLiteral().Value().(string); ok {
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
}
|
||||
sel := e.AsSelect()
|
||||
operand := sel.Operand()
|
||||
if operand.Kind() != celast.IdentKind || operand.AsIdent() != "actions" {
|
||||
return
|
||||
}
|
||||
seen[sel.FieldName()] = struct{}{}
|
||||
})
|
||||
celast.PreOrderVisit(ast.NativeRep().Expr(), visitor)
|
||||
ids := make([]string, 0, len(seen))
|
||||
@@ -118,6 +128,11 @@ func referencedActionIDs(ast *cel.Ast) []string {
|
||||
return ids
|
||||
}
|
||||
|
||||
// isActionsIdent reports whether e is the `actions` context-root identifier.
|
||||
func isActionsIdent(e celast.Expr) bool {
|
||||
return e.Kind() == celast.IdentKind && e.AsIdent() == "actions"
|
||||
}
|
||||
|
||||
// Program is a compiled, cost-limited playbook expression.
|
||||
type Program struct {
|
||||
prg cel.Program
|
||||
@@ -125,8 +140,9 @@ type Program struct {
|
||||
}
|
||||
|
||||
// ReferencedActionIDs returns the sorted, de-duplicated action ids the
|
||||
// expression reads through `actions.<id>`. It is empty when the expression
|
||||
// reads no prior-action result.
|
||||
// expression reads from `actions` (either `actions.<id>` or
|
||||
// `actions["<id>"]`). It is empty when the expression reads no prior-action
|
||||
// result.
|
||||
func (p *Program) ReferencedActionIDs() []string {
|
||||
if p == nil {
|
||||
return nil
|
||||
|
||||
@@ -110,6 +110,11 @@ func TestReferencedActionIDs(t *testing.T) {
|
||||
src: `has(actions.notify.result.delivered)`,
|
||||
want: []string{"notify"},
|
||||
},
|
||||
{
|
||||
name: "map index literal reference",
|
||||
src: `actions["notify"].result.delivered == true`,
|
||||
want: []string{"notify"},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/expr"
|
||||
)
|
||||
|
||||
func writeFile(t *testing.T, dir, name, content string) string {
|
||||
@@ -480,27 +482,47 @@ func TestValidateActionIDs(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestLoadWhenReferencesUnknownActionIDFails: a when reading actions.<id>
|
||||
// with no prior action declaring that id is a load failure naming the
|
||||
// playbook path and the id -- never a runtime miss.
|
||||
// (or actions["<id>"]) with no prior action declaring that id is a load
|
||||
// failure naming the playbook path and the id -- never a runtime miss.
|
||||
func TestLoadWhenReferencesUnknownActionIDFails(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, dir, "pb.yaml", `
|
||||
trigger:
|
||||
kind: bug
|
||||
actions:
|
||||
- position: workflow
|
||||
workflow: tdd
|
||||
when: actions.notify.result.delivered == true
|
||||
`)
|
||||
_, err := Load(dir)
|
||||
if err == nil {
|
||||
t.Fatal("Load(when referencing undeclared action id) = nil, want load failure")
|
||||
for _, when := range []string{
|
||||
`actions.notify.result.delivered == true`,
|
||||
`actions["notify"].result.delivered == true`,
|
||||
} {
|
||||
t.Run(when, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, dir, "pb.yaml", "\ntrigger:\n kind: bug\nactions:\n - position: workflow\n workflow: tdd\n when: "+when+"\n")
|
||||
_, err := Load(dir)
|
||||
if err == nil {
|
||||
t.Fatal("Load(when referencing undeclared action id) = nil, want load failure")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "pb.yaml") {
|
||||
t.Errorf("Load error = %q, want the playbook path named", err.Error())
|
||||
}
|
||||
if !strings.Contains(err.Error(), "notify") {
|
||||
t.Errorf("Load error = %q, want the unknown id named", err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
if !strings.Contains(err.Error(), "pb.yaml") {
|
||||
t.Errorf("Load error = %q, want the playbook path named", err.Error())
|
||||
}
|
||||
|
||||
// TestUnknownActionReferenceGeneralRule: the reference check compares against
|
||||
// the declared ids of earlier actions, so it already behaves correctly when
|
||||
// the one-action boundary later relaxes (the Load path can only exercise the
|
||||
// empty-prior set today).
|
||||
func TestUnknownActionReferenceGeneralRule(t *testing.T) {
|
||||
prg, err := expr.NewEnv().Compile(`actions.notify.result.delivered == true`)
|
||||
if err != nil {
|
||||
t.Fatalf("Compile: %v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "notify") {
|
||||
t.Errorf("Load error = %q, want the unknown id named", err.Error())
|
||||
|
||||
// notify declared on the prior action: known.
|
||||
if id, unknown := unknownActionReference([]rawAction{{ID: "notify"}}, 1, prg); unknown {
|
||||
t.Fatalf("unknownActionReference(declared prior) = (%q, true), want known", id)
|
||||
}
|
||||
// A different prior id leaves notify unknown.
|
||||
if id, unknown := unknownActionReference([]rawAction{{ID: "build"}}, 1, prg); !unknown || id != "notify" {
|
||||
t.Fatalf("unknownActionReference(undeclared) = (%q, %v), want (notify, true)", id, unknown)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user