Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

threatrules: bound CEL evaluation cost and make ordered correlation linear

Claude4afc3b4314e3
diff --git a/pkg/asymptoteobserve/threatrules/cel.go b/pkg/asymptoteobserve/threatrules/cel.go
index 94ee1aa6..997ab5fc 100644
--- a/pkg/asymptoteobserve/threatrules/cel.go
+++ b/pkg/asymptoteobserve/threatrules/cel.go
@@ -73,11 +73,32 @@ func celFieldName(f reflect.StructField) string {
return f.Name
}
+// MaxEvalCost is the CEL runtime cost one evaluation of one match expression against one
+// event may spend before it is stopped with an error. Cost is cel-go's deterministic
+// measure of the work an expression does (comprehension iterations, string bytes
+// traversed, regex size times input size), so the limit holds on every machine and is
+// not a wall-clock timeout.
+//
+// Without it a rule could hang a scan: seven nested .all() macros over ten-element list
+// literals run ten million iterations per event, about six seconds each, and nested
+// .map() can exhaust memory. A rule arrives as data (`beacon rules pull`), so the engine
+// cannot trust it to be cheap.
+//
+// The value is generous. Across the open corpus and the embedded baseline the most
+// expensive expression spends 3,506 on its own fixtures, and 537,433 on a pessimistic
+// event whose every string field is 64 KiB at once (the writer caps the whole event line
+// at 64 KiB, so no real event gets close): the limit leaves over 1,400 times headroom on
+// the first and about 9 times on the second. TestCorpusStaysUnderEvalCostLimit pins that
+// headroom. At this limit the seven-deep .all() above stops with an error after roughly
+// two seconds instead of running to completion.
+const MaxEvalCost uint64 = 5_000_000
+
// CompileMatch compiles a single match expression and verifies it types to bool. The
-// returned program is safe for concurrent reuse across events. A compile or type-check
-// error (including a reference to an unknown event field) is returned to the caller; this
-// is what makes "every shipped rule provably compiles against the schema" a load-time
-// guarantee.
+// returned program is safe for concurrent reuse across events, and each evaluation is
+// bounded by MaxEvalCost: one that exceeds it fails with an error rather than matching. A
+// compile or type-check error (including a reference to an unknown event field) is
+// returned to the caller; this is what makes "every shipped rule provably compiles
+// against the schema" a load-time guarantee.
func CompileMatch(expr string) (cel.Program, error) {
if strings.TrimSpace(expr) == "" {
return nil, fmt.Errorf("empty match expression")
@@ -93,7 +114,7 @@ func CompileMatch(expr string) (cel.Program, error) {
if ast.OutputType() != cel.BoolType {
return nil, fmt.Errorf("match expression must evaluate to bool, got %s", ast.OutputType())
}
- prog, err := env.Program(ast)
+ prog, err := env.Program(ast, cel.CostLimit(MaxEvalCost))
if err != nil {
return nil, fmt.Errorf("build CEL program: %w", err)
}
diff --git a/pkg/asymptoteobserve/threatrules/correlation.go b/pkg/asymptoteobserve/threatrules/correlation.go
index 4a4bda08..7454fed3 100644
--- a/pkg/asymptoteobserve/threatrules/correlation.go
+++ b/pkg/asymptoteobserve/threatrules/correlation.go
@@ -47,6 +47,12 @@ func (c *CompiledRule) matchSessionInSequence(events []asymptoteobserve.Event) (
// Try every step-0 match as a candidate anchor. A single greedy pass is not enough:
// an early anchor whose final step falls outside the window must not mask a later
// anchor that completes in-window with the same downstream event.
+ //
+ // The cursors are shared by every anchor, so each later step's expression runs at most
+ // once per event in the session. Without them a session with many step-0 matches
+ // re-evaluated every later event once per anchor, quadratic in the session (8,000
+ // credential reads took over half a minute for one rule).
+ cursors := make([]stepCursor, len(c.steps))
for start := range events {
matched, err := c.eval(c.steps[0], events[start])
if err != nil {
@@ -55,7 +61,7 @@ func (c *CompiledRule) matchSessionInSequence(events []asymptoteobserve.Event) (
if !matched {
continue
}
- seq, err := c.completeFrom(events, start)
+ seq, err := c.completeFrom(events, start, cursors)
if err != nil {
return nil, err
}
@@ -75,31 +81,67 @@ func (c *CompiledRule) matchSessionInSequence(events []asymptoteobserve.Event) (
// time of the final step, so if the greedy-earliest final is outside the window no other
// alignment from this anchor could do better — the anchor is abandoned and the caller
// tries the next one.
-func (c *CompiledRule) completeFrom(events []asymptoteobserve.Event, start int) ([]asymptoteobserve.Event, error) {
- startTime, startKnown := eventTime(events[start])
- final := len(c.steps) - 1
- stepIdx := 1
+func (c *CompiledRule) completeFrom(events []asymptoteobserve.Event, start int, cursors []stepCursor) ([]asymptoteobserve.Event, error) {
seq := make([]asymptoteobserve.Event, 1, len(c.steps))
seq[0] = events[start]
- for j := start + 1; j < len(events); j++ {
- matched, err := c.eval(c.steps[stepIdx], events[j])
+ prev := start
+ for s := 1; s < len(c.steps); s++ {
+ j, err := c.nextHit(&cursors[s], s, events, prev)
if err != nil {
return nil, err
}
- if !matched {
- continue
+ if j < 0 {
+ return nil, nil
}
seq = append(seq, events[j])
- if stepIdx == final {
- t, known := eventTime(events[j])
- if startKnown && known && t.Sub(startTime) > c.window {
- return nil, nil
- }
- return seq, nil
+ prev = j
+ }
+ startTime, startKnown := eventTime(events[start])
+ t, known := eventTime(events[prev])
+ if startKnown && known && t.Sub(startTime) > c.window {
+ return nil, nil
+ }
+ return seq, nil
+}
+
+// stepCursor records how far one sequence step has been evaluated across the anchors of
+// one session.
+//
+// Anchors are tried in order, and the greedy chain from a later anchor sits, step by
+// step, at or after the chain from an earlier one: the earliest hit after a later position
+// is never earlier. So the positions each step is asked about only move forward, and one
+// pass over the session per step answers every anchor. The zero value is a cursor that
+// has evaluated nothing.
+type stepCursor struct {
+ scanned int // events before this index have been evaluated against the step
+ hit int // 1 + the index of the last hit found, or 0 for none
+}
+
+// nextHit returns the index of the earliest event after `after` that satisfies step s, or
+// -1 when none does. Successive calls on one cursor must pass a non-decreasing `after`.
+func (c *CompiledRule) nextHit(cur *stepCursor, s int, events []asymptoteobserve.Event, after int) (int, error) {
+ if hit := cur.hit - 1; hit > after {
+ // The query that found it started at or before after and saw no earlier hit, so
+ // there is none between after and it either.
+ return hit, nil
+ }
+ j := after + 1
+ if cur.scanned > j {
+ // Only after an exhausted scan, which found nothing from an earlier position on.
+ j = cur.scanned
+ }
+ for ; j < len(events); j++ {
+ matched, err := c.eval(c.steps[s], events[j])
+ if err != nil {
+ return -1, err
+ }
+ if matched {
+ cur.scanned, cur.hit = j+1, j+1
+ return j, nil
}
- stepIdx++
}
- return nil, nil
+ cur.scanned, cur.hit = len(events), 0
+ return -1, nil
}
// matchSessionAnyOrder returns the matched events, in the order they occurred, for the
diff --git a/pkg/asymptoteobserve/threatrules/evaluator.go b/pkg/asymptoteobserve/threatrules/evaluator.go
index 5f668ff9..2e6b6bbb 100644
--- a/pkg/asymptoteobserve/threatrules/evaluator.go
+++ b/pkg/asymptoteobserve/threatrules/evaluator.go
@@ -32,9 +32,16 @@ type CompiledRule struct {
derived bool
}
+// evalObserver, when set, is called once per expression evaluation. It is nil outside tests,
+// which set it to count how much work a rule does without timing it.
+var evalObserver func()
+
// eval evaluates one compiled expression against one event, deriving fields first only for
// rules that reference them.
func (c *CompiledRule) eval(prog cel.Program, event asymptoteobserve.Event) (bool, error) {
+ if evalObserver != nil {
+ evalObserver()
+ }
if c.derived {
event = withDerivedFields(event)
}
diff --git a/pkg/asymptoteobserve/threatrules/findings.go b/pkg/asymptoteobserve/threatrules/findings.go
index e47a1c94..8b914670 100644
--- a/pkg/asymptoteobserve/threatrules/findings.go
+++ b/pkg/asymptoteobserve/threatrules/findings.go
@@ -1,6 +1,7 @@
package threatrules
import (
+ "fmt"
"sort"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
@@ -128,7 +129,10 @@ func ScanEvents(rules []*CompiledRule, events []asymptoteobserve.Event) ([]Findi
for _, rule := range rules {
found, err := rule.Findings(events)
if err != nil {
- return nil, err
+ // Name the rule: an evaluation error (such as one that exceeded MaxEvalCost)
+ // belongs to the rule, not to the log, and the operator needs to know which to
+ // remove.
+ return nil, fmt.Errorf("rule %q: %w", rule.Rule().ID, err)
}
all = append(all, found...)
}
diff --git a/pkg/asymptoteobserve/threatrules/limits_test.go b/pkg/asymptoteobserve/threatrules/limits_test.go
new file mode 100644
index 00000000..d2a5215e
--- /dev/null
+++ b/pkg/asymptoteobserve/threatrules/limits_test.go
@@ -0,0 +1,372 @@
+package threatrules
+
+import (
+ "fmt"
+ "math/rand"
+ "path/filepath"
+ "reflect"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/google/cel-go/cel"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+)
+
+// nestedAll is a match expression that runs 10^depth comprehension iterations per event:
+// at depth 7 it took about six seconds on one event before evaluation was cost-limited.
+func nestedAll(depth int) string {
+ const list = "[0,1,2,3,4,5,6,7,8,9]"
+ expr := `e.event.action != "x"`
+ for d := 0; d < depth; d++ {
+ expr = fmt.Sprintf("%s.all(v%d, %s)", list, d, expr)
+ }
+ return expr
+}
+
+func countEvals(t *testing.T) *int {
+ t.Helper()
+ n := 0
+ evalObserver = func() { n++ }
+ t.Cleanup(func() { evalObserver = nil })
+ return &n
+}
+
+func TestEvalCostLimitStopsRunawayExpression(t *testing.T) {
+ prog, err := CompileMatch(nestedAll(7))
+ if err != nil {
+ t.Fatalf("compile: %v", err)
+ }
+ start := time.Now()
+ matched, err := EvalMatch(prog, asymptoteobserve.Event{})
+ elapsed := time.Since(start)
+ if err == nil || !strings.Contains(err.Error(), "cost limit exceeded") {
+ t.Fatalf("EvalMatch = %v, %v; want a cost limit error", matched, err)
+ }
+ if matched {
+ t.Fatal("an expression stopped by the cost limit must not match")
+ }
+ // The limit is deterministic (cel-go cost units, not time); the bound only guards
+ // against the limit being dropped, so it is far above what the limit allows.
+ if elapsed > 30*time.Second {
+ t.Fatalf("cost-limited evaluation took %s", elapsed)
+ }
+
+ // A cheap nested expression stays well under the limit and evaluates normally.
+ small, err := CompileMatch(nestedAll(3))
+ if err != nil {
+ t.Fatalf("compile: %v", err)
+ }
+ if ok, err := EvalMatch(small, asymptoteobserve.Event{}); err != nil || !ok {
+ t.Fatalf("depth-3 EvalMatch = %v, %v; want true, nil", ok, err)
+ }
+}
+
+func TestScanNamesTheRuleThatExceededTheCostLimit(t *testing.T) {
+ rule := &Rule{
+ ID: "runaway", Version: 1, Title: "runaway",
+ Severity: asymptoteobserve.SeverityHigh, Status: StatusExperimental, Posture: PostureDetect,
+ Emit: Emit{Reason: "x"},
+ Match: nestedAll(7),
+ Tests: []Fixture{{Name: "x", Verdict: VerdictMatch, Events: []FixtureEvent{{Event: corrEvent("", "file.read", "s", nil)}}}},
+ }
+ c, err := Compile(rule)
+ if err != nil {
+ t.Fatalf("compile: %v", err)
+ }
+ // CheckRule (run at install by `beacon rules add|pull`) reports it as a fixture error.
+ results, err := CheckRule(rule)
+ if err != nil || len(results) != 1 || results[0].Err == nil || results[0].OK() {
+ t.Fatalf("CheckRule = %+v, %v; want one failing fixture with an error", results, err)
+ }
+ _, err = ScanEvents([]*CompiledRule{c}, []asymptoteobserve.Event{corrEvent("", "file.read", "s", nil)})
+ if err == nil || !strings.Contains(err.Error(), `rule "runaway"`) || !strings.Contains(err.Error(), "cost limit exceeded") {
+ t.Fatalf("ScanEvents error = %v; want it to name the rule and the cost limit", err)
+ }
+}
+
+// corpusRules loads the open corpus and the embedded baseline.
+func corpusRules(t *testing.T) []*Rule {
+ t.Helper()
+ var all []*Rule
+ for _, dir := range []string{rulesDir(t), filepath.Join(repoRoot(t), "pkg", "asymptoteobserve", "rulestore", "baseline")} {
+ rules, err := LoadDir(dir)
+ if err != nil {
+ t.Fatalf("load %s: %v", dir, err)
+ }
+ all = append(all, rules...)
+ }
+ return all
+}
+
+func ruleExpressions(r *Rule) []string {
+ if r.Correlation == nil {
+ return []string{r.Match}
+ }
+ var exprs []string
+ for _, s := range r.Correlation.Steps {
+ exprs = append(exprs, s.Match)
+ }
+ return exprs
+}
+
+// fillStrings sets every empty string field reachable from v (allocating nil sub-objects)
+// to s.
+func fillStrings(v reflect.Value, s string, depth int) {
+ switch v.Kind() {
+ case reflect.Ptr:
+ if depth > 6 {
+ return
+ }
+ if v.IsNil() && v.CanSet() {
+ v.Set(reflect.New(v.Type().Elem()))
+ }
+ if !v.IsNil() {
+ fillStrings(v.Elem(), s, depth+1)
+ }
+ case reflect.Struct:
+ for i := 0; i < v.NumField(); i++ {
+ if v.Type().Field(i).IsExported() {
+ fillStrings(v.Field(i), s, depth+1)
+ }
+ }
+ case reflect.String:
+ if v.CanSet() && v.Len() == 0 {
+ v.SetString(s)
+ }
+ }
+}
+
+// TestCorpusStaysUnderEvalCostLimit measures the cost every shipped expression spends on
+// its own fixtures and on a pessimistic copy of each fixture event whose every string field
+// is 64 KiB (the writer caps a whole event line at 64 KiB, so no real event gets close), and
+// requires headroom under MaxEvalCost. If a new rule trips this, the rule is doing far more
+// work per event than anything in the corpus; look at it before raising the limit.
+func TestCorpusStaysUnderEvalCostLimit(t *testing.T) {
+ env, err := Env()
+ if err != nil {
+ t.Fatal(err)
+ }
+ big := strings.Repeat("a", asymptoteobserve.DefaultMaxEventBytes)
+ var maxFixture, maxPessimistic uint64
+ var maxFixtureID, maxPessimisticID string
+ for _, r := range corpusRules(t) {
+ for _, expr := range ruleExpressions(r) {
+ ast, iss := env.Compile(expr)
+ if iss != nil && iss.Err() != nil {
+ t.Fatalf("%s: %v", r.ID, iss.Err())
+ }
+ prog, err := env.Program(ast, cel.EvalOptions(cel.OptTrackCost))
+ if err != nil {
+ t.Fatalf("%s: %v", r.ID, err)
+ }
+ cost := func(e asymptoteobserve.Event) uint64 {
+ _, det, err := prog.Eval(map[string]any{eventVar: e})
+ if err != nil {
+ return 0 // a type error on a synthetic value is not a cost measurement
+ }
+ return *det.ActualCost()
+ }
+ for _, f := range r.Tests {
+ for _, ev := range f.EventList() {
+ ev = withDerivedFields(ev)
+ if c := cost(ev); c > maxFixture {
+ maxFixture, maxFixtureID = c, r.ID
+ }
+ fillStrings(reflect.ValueOf(&ev).Elem(), big, 0)
+ if c := cost(ev); c > maxPessimistic {
+ maxPessimistic, maxPessimisticID = c, r.ID
+ }
+ }
+ }
+ }
+ }
+ t.Logf("max cost: fixtures %d (%s), every string 64 KiB %d (%s), limit %d",
+ maxFixture, maxFixtureID, maxPessimistic, maxPessimisticID, MaxEvalCost)
+ if maxFixture == 0 || maxPessimistic == 0 {
+ t.Fatal("measured no cost; the measurement is broken")
+ }
+ if maxFixture*100 > MaxEvalCost {
+ t.Errorf("rule %s spends %d on its fixtures, within 100x of MaxEvalCost %d", maxFixtureID, maxFixture, MaxEvalCost)
+ }
+ if maxPessimistic*5 > MaxEvalCost {
+ t.Errorf("rule %s spends %d on a 64 KiB-string event, within 5x of MaxEvalCost %d", maxPessimisticID, maxPessimistic, MaxEvalCost)
+ }
+}
+
+// manyAnchors is a session where every event satisfies step 0 of
+// secret-read-then-external-mcp and none satisfies step 1: the shape that made ordered
+// correlation quadratic.
+func manyAnchors(n int) []asymptoteobserve.Event {
+ base := time.Date(2026, 6, 13, 10, 0, 0, 0, time.UTC)
+ events := make([]asymptoteobserve.Event, n)
+ for i := range events {
+ events[i] = corrEvent(base.Add(time.Duration(i)*time.Second).Format(time.RFC3339), "file.read", "s1", withEnv)
+ }
+ return events
+}
+
+func loadCorpusRule(t *testing.T, rel string) *CompiledRule {
+ t.Helper()
+ rule, err := LoadRule(filepath.Join(rulesDir(t), rel))
+ if err != nil {
+ t.Fatalf("load %s: %v", rel, err)
+ }
+ c, err := Compile(rule)
+ if err != nil {
+ t.Fatalf("compile %s: %v", rel, err)
+ }
+ return c
+}
+
+// TestOrderedCorrelationIsLinearInTheSession counts evaluations rather than timing them: each
+// step's expression must run at most once per event. Before, 8,000 anchors re-evaluated every
+// later event (about 32 million evaluations, over half a minute).
+func TestOrderedCorrelationIsLinearInTheSession(t *testing.T) {
+ c := loadCorpusRule(t, "context-exfiltration/secret-read-then-external-mcp.rule.yaml")
+ const n = 8000
+ events := manyAnchors(n)
+
+ evals := countEvals(t)
+ v, err := c.Evaluate(events)
+ if err != nil || v != VerdictNoMatch {
+ t.Fatalf("Evaluate = %v, %v; want no_match", v, err)
+ }
+ if limit := n * len(c.steps); *evals > limit {
+ t.Fatalf("ordered correlation ran %d evaluations over %d events and %d steps; want at most %d", *evals, n, len(c.steps), limit)
+ }
+
+ // The same session with the external MCP call at the end, out of window from every
+ // anchor but the last few: still linear, and still finds the in-window alignment.
+ end, _ := eventTime(events[n-1])
+ events = append(events, corrEvent(end.Add(10*time.Second).Format(time.RFC3339), "mcp.tool_invoked", "s1", func(e *asymptoteobserve.Event) {
+ e.MCP = &asymptoteobserve.MCPInfo{Server: "https://api.example.com", Tool: "x"}
+ }))
+ *evals = 0
+ findings, err := c.Findings(events)
+ if err != nil || len(findings) != 1 {
+ t.Fatalf("Findings = %v, %v; want one finding", findings, err)
+ }
+ if got := findings[0].Events[0].Timestamp; got == events[0].Timestamp {
+ t.Fatalf("finding anchored at the first read (%s), which is outside the window", got)
+ }
+ if limit := len(events) * len(c.steps); *evals > limit {
+ t.Fatalf("ran %d evaluations; want at most %d", *evals, limit)
+ }
+}
+
+// matchSessionInSequenceReference is the exhaustive matcher the cursors replaced: for every
+// anchor it re-scans every later event. It is the oracle for the equivalence test.
+func (c *CompiledRule) matchSessionInSequenceReference(events []asymptoteobserve.Event) ([]asymptoteobserve.Event, error) {
+ for start := range events {
+ matched, err := c.eval(c.steps[0], events[start])
+ if err != nil {
+ return nil, err
+ }
+ if !matched {
+ continue
+ }
+ startTime, startKnown := eventTime(events[start])
+ final := len(c.steps) - 1
+ stepIdx := 1
+ seq := []asymptoteobserve.Event{events[start]}
+ for j := start + 1; j < len(events); j++ {
+ matched, err := c.eval(c.steps[stepIdx], events[j])
+ if err != nil {
+ return nil, err
+ }
+ if !matched {
+ continue
+ }
+ seq = append(seq, events[j])
+ if stepIdx == final {
+ t, known := eventTime(events[j])
+ if startKnown && known && t.Sub(startTime) > c.window {
+ seq = nil
+ }
+ break
+ }
+ stepIdx++
+ }
+ if len(seq) == len(c.steps) {
+ return seq, nil
+ }
+ }
+ return nil, nil
+}
+
+// TestOrderedCorrelationMatchesExhaustiveSearch compares the cursor matcher with the
+// exhaustive one on random sessions: unsorted input, missing timestamps, events that
+// satisfy several steps, and windows that cut alignments short.
+func TestOrderedCorrelationMatchesExhaustiveSearch(t *testing.T) {
+ actions := []string{"a", "b", "c", "x"}
+ rules := []*CompiledRule{
+ compileCorrelation(t, "", "10s",
+ CorrelationStep{ID: "a", Match: `e.event.action == "a"`},
+ CorrelationStep{ID: "b", Match: `e.event.action == "b"`},
+ ),
+ compileCorrelation(t, "", "20s",
+ CorrelationStep{ID: "a", Match: `e.event.action == "a"`},
+ CorrelationStep{ID: "ab", Match: `e.event.action in ["a", "b"]`},
+ CorrelationStep{ID: "c", Match: `e.event.action == "c"`},
+ ),
+ compileCorrelation(t, "", "5s",
+ CorrelationStep{ID: "a", Match: `e.event.action == "a"`},
+ CorrelationStep{ID: "a2", Match: `e.event.action == "a"`},
+ CorrelationStep{ID: "b", Match: `e.event.action != "x"`},
+ ),
+ }
+ rng := rand.New(rand.NewSource(1))
+ base := time.Date(2026, 6, 13, 10, 0, 0, 0, time.UTC)
+ for iter := 0; iter < 3000; iter++ {
+ n := rng.Intn(14)
+ events := make([]asymptoteobserve.Event, n)
+ at := base
+ for i := range events {
+ at = at.Add(time.Duration(rng.Intn(8)) * time.Second)
+ if rng.Intn(6) == 0 {
+ at = at.Add(-time.Duration(rng.Intn(15)) * time.Second) // out of order
+ }
+ ts := at.Format(time.RFC3339)
+ if rng.Intn(5) == 0 {
+ ts = "" // no timestamp: the window is not enforced against it
+ }
+ events[i] = corrEvent(ts, actions[rng.Intn(len(actions))], "s", nil)
+ }
+ for ri, c := range rules {
+ got, err := c.matchSessionInSequence(events)
+ if err != nil {
+ t.Fatal(err)
+ }
+ want, err := c.matchSessionInSequenceReference(events)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("iter %d rule %d: cursor matcher %v, exhaustive %v, events %v", iter, ri, got, want, events)
+ }
+ }
+ }
+}
+
+func TestCostLimitErrorIsNotAMatch(t *testing.T) {
+ // Guard the contract callers rely on: the error is returned, never folded into a verdict.
+ c, err := Compile(&Rule{
+ ID: "runaway-step", Spec: SupportedSpec, Version: 1, Title: "x",
+ Severity: asymptoteobserve.SeverityHigh, Status: StatusExperimental, Posture: PostureDetect,
+ Emit: Emit{Reason: "x"},
+ Correlation: &Correlation{Scope: ScopeSession, Window: "60s", Steps: []CorrelationStep{
+ {ID: "a", Match: `e.event.action == "a"`},
+ {ID: "b", Match: nestedAll(7)},
+ }},
+ Tests: []Fixture{{Name: "x", Verdict: VerdictMatch, Events: []FixtureEvent{{Event: corrEvent("", "a", "s", nil)}}}},
+ })
+ if err != nil {
+ t.Fatalf("compile: %v", err)
+ }
+ v, err := c.Evaluate([]asymptoteobserve.Event{corrEvent("", "a", "s", nil), corrEvent("", "b", "s", nil)})
+ if err == nil || v == VerdictMatch || !strings.Contains(err.Error(), "cost limit exceeded") {
+ t.Fatalf("Evaluate = %v, %v; want an error and no match", v, err)
+ }
+}
diff --git a/spec/threat-rules/SPEC.md b/spec/threat-rules/SPEC.md
index 3f32051d..61e4c00d 100644
--- a/spec/threat-rules/SPEC.md
+++ b/spec/threat-rules/SPEC.md
@@ -138,6 +138,13 @@ the steps in both orders still fires once. A single event never satisfies two st
- Regular expressions use RE2 via CEL `.matches(re)` (the same engine as Go `regexp`).
RE2 runs in time linear in its input and has no backtracking, so a pattern cannot be
made to run super-linearly (no ReDoS); cost is bounded by the size of the text matched.
+- Evaluation is bounded. Macros (`.all`, `.exists`, `.map`, `.filter`) can nest, so an
+ expression can do work exponential in its own length. An engine SHOULD cap the work one
+ evaluation of one expression against one event may do, and MUST report an evaluation it
+ stops as an error for that rule, never as a `match` or `no_match`. The reference engine
+ uses cel-go's runtime cost limit at 5,000,000 (`threatrules.MaxEvalCost`); every rule in
+ the open corpus spends under 4,000 on its fixtures. A rule should stay orders of
+ magnitude below the limit on any event the writer can produce (at most 64 KiB).
- Derived fields. `e.gen_ai.tool.call.result_text` is not on the wire: an engine MUST
compute it from the event before evaluation, and MUST ignore any value an input
claims for it. It is `gen_ai.tool.call.result` as text (a string as is; for an object