Skip to main content
ast-guard ships with 21 built-in rules; 19 of them are exported as classes (InfiniteLoopRule and ResourceExhaustionRule are part of the AgentScript preset only). Each rule can be configured and combined to match your security requirements. This page covers the ones a host is most likely to configure itself; the AgentScript preset combines them for LLM-generated code.

NoGlobalAccessRule

Blocks access to the global objects, to constructor, to the Reflect API and to the reflection statics of Object.
Blocks:
  • window.location
  • globalThis['Function']
  • this as a value in any position, when 'this' is in blockedGlobals: const g = this, f(this), const { x } = this. The STRICT and SECURE presets refuse it this way
  • Object.create, Object.defineProperty, Object.getPrototypeOf and the other prototype and descriptor statics, wherever the member is read
With requireDirectObjectUse: true the global Object may only be used directly: as Object.name with a name written in the source, called, constructed, or as the operand of typeof / instanceof. An alias (const O = Object), passing or storing it, destructuring from it, a member name computed at run time and Object.valueOf() are refused with OBJECT_INDIRECT_USE. The AgentScript preset switches this on.

ReservedPrefixRule

Prevents user code from declaring identifiers with reserved prefixes.
Blocks:
  • const __ag_hack = 1;
  • let __safe_bypass = true;
  • __maxIterations, the runtime’s name for the iteration limit, whatever prefixes are configured
On the rule, reservedPrefixes is the complete list. The reservedPrefixes option of the AgentScript preset adds to __ag_ and __safe_ instead.

NoCallTargetAssignmentRule

Protects critical call targets from being reassigned or shadowed.
Blocks:
  • callTool = malicious; - Direct assignment
  • const callTool = () => {}; - Variable shadowing
  • const { callTool } = obj; - Destructuring shadowing
  • function callTool() {} - Function declaration shadowing
The rule sees every write and every declaration of a protected name: destructuring patterns, loop heads, ++ / --, catch-clause patterns, parameters and their default values, and class names.

UnicodeSecurityRule

Reports bidirectional control characters, look-alike characters, zero-width characters and invisible characters in a script. The tag characters (U+E0000 to U+E007F) count as invisible characters. No preset includes this rule; add it to a validator yourself.
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.

StaticCallTargetRule

Enforces static string literals for call targets.
Blocks:
  • callTool(dynamicName, {}) - Variable as tool name
  • callTool(getToolName(), {}) - Function call as tool name
Allows:
  • callTool('users:list', {}) - Static string literal
With requireDirectCall: true (as in the AgentScript preset) nothing else may be done with the target function: no alias, no .call / .apply / .bind, no tagged template, no passing it as a value.

NoRegexLiteralRule

Blocks or analyzes regex literals for ReDoS vulnerabilities.

DisallowedIdentifierRule

Blocks specific identifier names.

ForbiddenLoopRule

Restricts which loop constructs are allowed.

RequiredFunctionCallRule

Ensures code contains required function calls.

UnknownGlobalRule

Rejects references to undeclared identifiers (whitelist mode).
Names are resolved by lexical scope: a declaration in a block, an arrow function or a catch clause makes a name local only there. The first UNKNOWN_GLOBAL issue of a validation states the list of allowed globals (in its message and in data.allowedGlobals); the following ones name the identifier only.

NoEvalRule

Blocks eval and dynamic code execution.
Blocks:
  • eval('code')
  • new Function('return 1')
  • setTimeout('code', 100) - String form

NoAsyncRule

Restricts async/await usage.

NoUserDefinedFunctionsRule

Blocks user-defined functions (prevents recursion).
A name in allowedFunctionNames allows one declaration: the first one at the top level that has the shape of the wrapper (async function name() { ... }: async, not a generator, no parameters). Every other function declaration is refused whatever its name. Blocks:
  • function foo() {}
  • const fn = function() {}
Allows:
  • array.map(x => x * 2) - Arrow functions (if enabled)

UnreachableCodeRule

Detects code after return/throw statements.

CallArgumentValidationRule

Validates function call arguments.

AllowedSyntaxRule

An allowlist of syntax: a script may only contain the listed ESTree node types.
Four constructs share their node type with an ordinary one and are constructs of their own. They are refused (DISALLOWED_SYNTAX) unless listed under their own name: UsingDeclaration, AwaitUsingDeclaration, ForAwaitStatement and GeneratorFunction. Listing ForOfStatement allows for…of, not for await…of.

NoRegexMethodsRule

Blocks the string and regex methods that run a regular expression.
match, matchAll and search compile a string argument into a regular expression, so with allowStringArguments: false they are refused with any argument. The method name is resolved in every static spelling (x.match, x['match'], a template literal without substitutions), and the call is seen through call / apply / bind, as a template tag and without an argument. With requireDirectCall: true, a property named match, matchAll or search can only be called where it stands: reading it, passing or storing it and taking it by destructuring are refused. That also refuses reading a field of ordinary data with such a name (params.search), which is why it is off by default.

NoJsonCallbacksRule

Blocks JSON.stringify with a replacer function and JSON.parse with a reviver function.
With requireDirectUse: true an alias of JSON, .call / .apply / .bind, a computed method name and every method other than parse and stringify are refused (JSON_INDIRECT_USE). Only the global undefined counts as “no replacer or reviver”, not a variable of the script with that name.

Rule Severity

A rule has a default severity. Override it per validation, by rule name: