16 Built-in Rules
Block eval, dangerous globals, prototype manipulation, unbounded loops, ReDoS, and more with battle-tested validation rules.
Pre-Scanner Defense
Layer 0 security that runs BEFORE parsing - catches DoS attacks that could crash the parser itself.
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
Pre-Scanner (Layer 0 Defense)
The pre-scanner runs BEFORE the JavaScript parser (acorn) to catch DoS attacks that could crash or hang the parser itself. It enforces mandatory security limits that cannot be disabled.Mandatory Limits (Cannot Be Exceeded)
These limits protect against parser crashes and cannot be overridden:Pre-Scanner Preset Comparison
Regex Handling Modes
The pre-scanner supports three regex handling modes:block- Block ALL regex literals (AgentScript default, maximum security)analyze- Allow but analyze for ReDoS patterns (Strict/Secure/Standard)allow- Allow all regex without analysis (Permissive only)
ReDoS Detection Patterns
The pre-scanner detects these dangerous regex patterns: | Pattern | Score | Example | Risk | | ----------------------- | ----- | ------------ | ------------------------ | ------------------------ | | Nested quantifier | 90 |(a+)+ | Exponential backtracking |
| Star in repetition | 85 | (a+){2,} | Exponential backtracking |
| Repetition in star | 85 | (a{2,})+ | Exponential backtracking |
| Overlapping alternation | 80 | (a | ab)+ | Exponential backtracking |
| Greedy backtracking | 75 | (.*a)+ | Polynomial backtracking |
| Multiple greedy | 70 | .*foo.*bar | Polynomial backtracking |
AgentScript Preset
The AgentScript preset is purpose-built for validating LLM-generated orchestration code. It’s the default preset used by Enclave and the CodeCall Plugin.AgentScript Preset Options
What AgentScript Blocks
What AgentScript Allows
Code Transformation
ast-guard can transform validated code for safe execution:- Main wrapper:
async function __ag_main()enables top-level await - Safe callTool: Proxied through runtime with call counting
- Safe loops: Iteration limits enforced at runtime
- Reserved prefixes:
__ag_and__safe_cannot be used by user code
Security Presets
ast-guard includes four security presets for different use cases:Validation in Tools
Use ast-guard to validate scripts inside FrontMCP tools:Enforce policy at the platform level
Use hooks to reject bad scripts before the tool executes, even if multiple tools submit code.src/plugins/script-guard.plugin.ts
Built-in Security Rules
ast-guard ships with a comprehensive set of security rules:NoGlobalAccessRule
Blocks access to dangerous global objects via member expressions (e.g.,window.location, process.env).
ReservedPrefixRule
Prevents user code from declaring or assigning identifiers with reserved prefixes (e.g.,__ag_, __safe_).
NoCallTargetAssignmentRule
Protects critical call targets from being reassigned or shadowed.callTool = malicious;- Direct assignmentconst callTool = () => {};- Variable shadowingconst { callTool } = obj;- Destructuring shadowingfunction callTool() {}- Function declaration shadowing
UnicodeSecurityRule
Detects and blocks Unicode-based attacks including Trojan Source, homoglyphs, and invisible characters.StaticCallTargetRule
Enforces static string literals for call targets, preventing dynamic tool name injection.callTool(toolName, args);- Variable referencecallTool("tool" + suffix, args);- ConcatenationcallTool(\tool_$`, args);` - Template with expressionscallTool(cond ? "a" : "b", args);- Ternary expression
NoRegexLiteralRule
Blocks or analyzes regex literals for ReDoS vulnerabilities.NoRegexMethodsRule
Blocks regex method calls to provide defense-in-depth against ReDoS.Mix in fine-grained rules
Combine built-in rules to match your own threat model.src/security/custom-rules.ts
Whitelist-based identifier control with UnknownGlobalRule
UnknownGlobalRule implements a whitelist-based approach where all identifier references must be either declared locally or explicitly allowed. This is the most secure option for sandboxed environments.
UnknownGlobalRule uses a flat symbol table for performance. It collects all declarations across the AST without tracking lexical scope. This is an intentional simplification for AgentScript v1 where user-defined functions are blocked by default (NoUserDefinedFunctionsRule). If you enable user functions, be aware that inner-scope declarations will “whitelist” that identifier name globally.Return actionable errors to requesters
Surface structured issues so users (or copilots) know how to fix their scripts.Monitor and tune validation
stopOnFirstErrorhalts validation as soon as a rule reports an error—great for latency-sensitive flows.maxIssuescaps the number of findings returned for a single run to avoid overwhelming users.parseOptionslets you enforcesourceType, strict mode, or JSX support per tool.validator.getStats(result, durationMs)produces telemetry-friendly counters.