v1.0.2

AST Security & Sandboxing

Kafal utilizes an Abstract Syntax Tree (AST) validation layer to securely execute user-provided scripts. This architecture isolates the host environment from remote code execution and resource exhaustion.

Complexity Limits

To prevent CPU exhaustion, the execution environment imposes non-configurable limits on script complexity prior to compilation:

  • AST Node Cap: Expressions are limited to a maximum of 2,000 AST nodes. Complex mathematical formulas must be divided into discrete variable assignments.
  • Expression Length: Individual string expressions cannot exceed 10,000 characters.
  • Recursion Depth: User-defined functions are limited to a maximum call stack depth of 32 to prevent stack overflow.

Restricted Features

The syntax validator explicitly blocks Python language features that could compromise host security. Attempting to use restricted features throws an UnsafeExpressionError.

Blocked Syntax
  • Inline Lambdas
  • List and Dict Comprehensions
  • f-strings and string interpolation
  • Walrus operators (:=)
  • Keyword arguments in function calls
Blocked Operations
  • System imports (import, from)
  • Dunder methods (e.g., __class__)
  • File I/O (read_csv, to_sql)
  • Execution APIs (eval, exec)

AST Transformations

Kafal performs structural AST rewriting on historical and conditional expressions before evaluation. This permits an expressive DSL syntax while maintaining strict bounds checking within the underlying engine.

// User Script Syntax
prev_val = close[1]
color_val = close > open ? "green" : "red"

// AST Internal Transformation
// The engine compiles the above down to function calls before execution
// to guarantee array bounds and type safety.
prev_val = history(close, 1)
color_val = where(close > open, "green", "red")