21 Built-in Rules
Block eval, dangerous globals, prototype manipulation, unbounded loops, ReDoS, and more with battle-tested validation rules.
Pre-Scanner Defense
Layer 0 checks that run before parsing: size, nesting and line limits, and by preset regex and Unicode checks.
AgentScript Preset
Purpose-built preset for LLM-generated orchestration code with whitelist-only globals and strict control flow.
When to Use ast-guard
- LLM-generated code - Validate AI-written JavaScript before execution
- User scripts - Accept arbitrary JavaScript with deterministic guardrails
- Workflow builders - Enforce API usage and block dangerous constructs
- Compliance requirements - Audit trails showing exactly which rule blocked a script
ast-guard is a pure TypeScript package with zero native dependencies. It works in Node.js 22+ and can be used standalone or as part of the Enclave execution environment.
Installation
Quick Start
valid means that every rule ran and none reported an error. It does not mean the code is safe to run on its own: a validator is a filter on source text, and what code does when it runs is decided by where it runs (see the warning below).
Validation Result
Validation Options
maxIssues a validation collects at most DEFAULT_MAX_ISSUES (100) issues; 0 collects every issue. At the cap the validation stops as soon as an error is on the list. While only warnings were found the remaining rules still run, because one of them may refuse the source, and further warnings are not collected. A result whose list is not everything the rules had to report has incomplete: true. A validation that stopped early, at the cap or by stopOnFirstError, is never valid: valid is true only when every rule ran and none reported an error.
Related
- Pre-Scanner - Layer 0 defense before parsing
- AgentScript Preset - Preset for LLM code
- Security Rules - Built-in rules reference
- enclave-vm - Runtime sandbox