cel-go icon indicating copy to clipboard operation
cel-go copied to clipboard

Add depth validation for ASTs loaded through ParsedExprToAst / Checke...

Open wilyan09007 opened this issue 5 months ago • 2 comments

Summary

cel-go enforces a recursion/expression-depth limit while parsing CEL source, but ASTs ingested directly — through ParsedExprToAst / CheckedExprToAst — bypass that guard. A deeply nested loaded AST then flows into the recursive checker (Env.Check) and planner (Env.Program / PlanProgram), which can exhaust the Go stack and abort the process with fatal error: stack overflow instead of returning a recoverable error. Embedders that load stored or serialized CEL ASTs as policy objects are the most exposed.

This validates expression nesting depth before those recursive phases:

  • Adds ast.ExceedsMaxDepth(expr, maxDepth) in common/ast — a bounded traversal that never recurses past maxDepth+1 levels, so it stays safe on the same adversarially deep inputs it guards against. The default ast.MaxNestingDepth = 250 mirrors the parser's default maxRecursionDepth, keeping loaded ASTs consistent with parsed ones.
  • Env.Check and Env.PlanProgram now run that check up front and return an ordinary error/issue — input exceeds maximum expression nesting depth: 250 — rather than recursing into a stack overflow.
  • Adds a regression test that loads a synthetic over-deep AST and asserts Check and Program return an error (not a crash), while normal expressions remain unaffected.

Closes #1333

wilyan09007 avatar Jun 04 '26 19:06 wilyan09007