cel-go
cel-go copied to clipboard
Add depth validation for ASTs loaded through ParsedExprToAst / Checke...
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)incommon/ast— a bounded traversal that never recurses pastmaxDepth+1levels, so it stays safe on the same adversarially deep inputs it guards against. The defaultast.MaxNestingDepth = 250mirrors the parser's defaultmaxRecursionDepth, keeping loaded ASTs consistent with parsed ones. Env.CheckandEnv.PlanProgramnow 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
CheckandProgramreturn an error (not a crash), while normal expressions remain unaffected.
Closes #1333