Documentation
¶
Overview ¶
Package celmatcher compiles CEL matcher expressions evaluated against a JWT claims map, restricting the parsed expression's shape before it is type checked.
The restriction exists because the time and memory CEL type checking takes is not linear in the expression's length. The checker stores a full type for every node, and two things let a short expression give many nodes a large type:
- A macro's iteration variable takes the element type of the macro's range. Over a literal or another macro's result, that type can be large, and using the variable twice in a literal doubles it, so twenty links of [1].map(a, {a: a}).map(a, {a: a})... (under 300 characters) need gigabytes.
- An empty list or map literal has a free element type, which the checker unifies with whatever it is compared or listed with. A list of a large literal followed by hundreds of [] gives every [] the large type, so memory grows with the product of the two.
Matchers are set on namespaced custom resources and compiled by the cluster-wide operator during reconcile, so an unbounded checker would let one namespace's resource stop the operator for all of them.
Compile therefore parses first and rejects an expression unless:
- every macro (all, exists, exists_one, map, filter) ranges over claims, or over a chain of field selections and index operations rooted at claims or at an enclosing macro's iteration variable, such as claims.groups or claims["realm_access"].roles. claims is a map(string, dyn), so every iteration variable is a string key of claims or a dyn;
- it contains no empty list or map literal, [] or {};
- it is nested no deeper than MaxDepth.
The accumulator variable macros introduce is given a name source cannot spell, so claims and iteration variables are the only variables an expression can reference. Together the rules remove both sources above: no variable can carry a large type, and no literal has a free element type. What remains grows with the depth of the inferred types, which MaxDepth bounds: the checker renders a type as a string at every level it substitutes, so a deeply nested type costs time even when it is small. Measured with cel-go v0.33.4, the most expensive accepted expression of up to 4096 characters found by an adversarial search makes Compile return in about 75ms with a peak live heap of about 5MiB; at the engine's 10000-character limit, about 250ms and 12MiB.
Ordinary matchers such as
claims.azp in ["client-a", "client-b"]
claims.groups.exists(g, g.startsWith("platform-")) && claims.iss == "https://idp.example.com"
are unaffected. A test of a literal list against a claim, such as ["a", "b"].exists(x, x in claims.groups), must range over the claim instead: claims.groups.exists(g, g in ["a", "b"]). Macros cannot be chained, so claims.groups.filter(g, p(g)).exists(g, q(g)) must be written claims.groups.exists(g, p(g) && q(g)), and an emptiness test such as claims.groups == [] must be written type(claims.groups) == list && size(claims.groups) == 0.
Index ¶
Constants ¶
const MaxDepth = 24
MaxDepth is the maximum nesting depth of a matcher's expression tree after macro expansion, counting the root as 1. Chains of && and || are balanced by the parser, so long conjunctions stay shallow; list and map literals, field selections, function calls and macros each add depth (a macro adds three or four levels around its body), while parentheses do not.
Variables ¶
var ( // ErrTooComplex is returned, wrapped, when an expression is nested deeper // than MaxDepth. ErrTooComplex = errors.New("expression is too complex") // ErrUnsupportedShape is returned, wrapped, when a macro ranges over // anything other than claims or a field or index chain rooted at claims // or at an enclosing macro's iteration variable, when the expression // contains an empty list or map literal, or when a comprehension's // accumulator is not the hidden name standard macros use. ErrUnsupportedShape = errors.New("unsupported matcher shape") )
Functions ¶
This section is empty.
Types ¶
type CompiledExpression ¶
type CompiledExpression = cel.CompiledExpression
CompiledExpression is a compiled matcher, so callers need not import the CEL engine package themselves.
func Compile ¶
func Compile(expr string) (*CompiledExpression, error)
Compile parses expr, checks its shape as described in the package documentation, and then type checks and compiles it with the "claims" variable bound to a map[string]any.
A syntax error, a type error or a rejected shape is returned as an error wrapping cel.ErrExpressionCheck; a rejected shape also wraps ErrTooComplex or ErrUnsupportedShape. Compile does not check the result type: an expression that type checks but does not return bool compiles, and is rejected when CompiledExpression.EvaluateBool runs.