Bank-grade JavaScript AST validation with extensible rules, presets, and the AgentScript language for safe LLM code execution.
ast-guard is FrontMCP’s AST validation library for JavaScript. It inspects user-provided or LLM-generated code before execution, blocking dangerous constructs and enforcing API usage policies. ast-guard powers Enclave’s first security layer and can be used standalone for any JavaScript validation needs.
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.
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.
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.
Copy
import { PreScanner, createPreScannerConfig } from 'ast-guard';// Create pre-scanner with AgentScript config (strictest)const scanner = new PreScanner(createPreScannerConfig('agentscript'));const result = scanner.scan(userCode);if (!result.valid) { console.log('Pre-scan failed:', result.issues); // Don't even attempt to parse - could DoS the parser}
The AgentScript preset is purpose-built for validating LLM-generated orchestration code. It’s the default preset used by Enclave and the CodeCall Plugin.
Copy
import { JSAstValidator, createAgentScriptPreset } from 'ast-guard';const validator = new JSAstValidator(createAgentScriptPreset({ // Require at least one callTool() invocation (default: false) requireCallTool: true, // Customize allowed globals allowedGlobals: ['callTool', 'getTool', 'Math', 'JSON', 'Array', 'Object'], // Allow arrow functions for array methods (default: true) allowArrowFunctions: true, // Configure allowed loop types allowedLoops: { allowFor: true, // for (let i = 0; ...) - default: true allowForOf: true, // for (const x of arr) - default: true allowWhile: false, // while (cond) - default: false allowDoWhile: false, // do {} while (cond) - default: false allowForIn: false, // for (key in obj) - default: false },}));
Require at least one callTool() invocation in the code
allowedGlobals
string[]
Standard safe globals
Identifiers that can be referenced without declaration
allowArrowFunctions
boolean
true
Allow arrow functions for array methods
allowedLoops
object
for and for-of
Configure which loop types are allowed
additionalDisallowedIdentifiers
string[]
[]
Additional identifiers to block
Use requireCallTool: true to ensure AgentScript code actually interacts with tools rather than just performing local computations. This is useful for preventing scripts that do nothing useful.
Detects and blocks Unicode-based attacks including Trojan Source, homoglyphs, and invisible characters.
Copy
import { UnicodeSecurityRule } from 'ast-guard';const rule = new UnicodeSecurityRule({ blockBidi: true, // Block bidirectional text attacks (Trojan Source) blockHomoglyphs: true, // Block lookalike characters (Cyrillic 'а' vs Latin 'a') blockZeroWidth: true, // Block zero-width characters blockInvisible: true, // Block invisible formatting characters checkComments: true, // Also check inside comments checkStrings: false, // Skip string literals (default) allowedCharacters: [], // Whitelist specific characters});
Trojan Source attacks (CVE-2021-42574) use Unicode bidirectional control characters to make code appear different than it actually executes. Always enable blockBidi: true for untrusted code.
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.
Copy
import { UnknownGlobalRule } from 'ast-guard';const rule = new UnknownGlobalRule({ // Only these globals are allowed (plus locally declared variables) allowedGlobals: ['callTool', 'getTool', 'Math', 'JSON', 'Array', 'Object'], // Include safe JS globals like undefined, NaN, isNaN, parseInt, etc. allowStandardGlobals: true,});
Identifiers that can be referenced without declaration
allowStandardGlobals
true
Include safe built-ins like undefined, NaN, isNaN, parseInt, etc.
message
Auto-generated
Custom error message for violations
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.
AST Guard prevents unsafe syntax from entering your sandbox, but it does not execute or sandbox code itself. Pair it with your existing isolation layer (isolated-vm, workers, remote runners, etc.) for complete defense-in-depth.