Skip to main content
ast-guard allows you to write custom validation rules and combine built-in rules to match your specific security requirements.

Combining Built-in Rules

Create a custom validator by combining built-in rules:

Security Presets

ast-guard includes pre-built security presets:

Writing Custom Rules

Rule Interface

A rule has a name, a description, a default severity and a validate method. It reports what it finds through context.report; it does not return a list.
A rule passed to the JSAstValidator constructor runs in every validation that does not switch it off by name (rules: { 'rule-name': false }). The validator collects at most maxIssues issues (100 by default), so a rule that reports once per node cannot make the result grow with the source.

Example: Custom Rule

Example: Block Specific API Calls

This rule sees a tool name only where callTool is called directly with a string literal. The AgentScript preset already requires exactly that form (staticCallTarget), and its allowedToolNames option restricts the names; a rule like this one adds to it and does not stand alone.

Extending Presets

A preset function returns an array of rules. Add rules to it:

Removing Rules from Presets

Filter out rules you don’t need:
The rules of the AgentScript preset depend on each other: several of them are only sound because another one refuses a construct. Removing a rule can open more than that rule’s name suggests. Do it only for a host that does not run untrusted code.

Rule Ordering

Rules are executed in array order. For performance, order rules by:
  1. Fast rejections first - Rules that quickly identify invalid code
  2. Complex analysis last - Rules that traverse the entire AST

Testing Custom Rules