import { ValidationRule } from '../interfaces'; /** * Base globals allowed at STRICT security level only * Absolute minimum: core types + callTool */ export declare const AGENTSCRIPT_STRICT_GLOBALS: readonly ["callTool", "__safe_callTool", "Math", "JSON", "Array", "Object", "String", "Number", "Date", "undefined", "NaN", "Infinity", "__safe_forOf", "__safe_for", "__safe_while", "__safe_doWhile", "__maxIterations"]; /** * Globals for SECURE security level * Adds safe utility functions (pure functions with no side effects) */ export declare const AGENTSCRIPT_SECURE_GLOBALS: readonly ["callTool", "__safe_callTool", "Math", "JSON", "Array", "Object", "String", "Number", "Date", "undefined", "NaN", "Infinity", "__safe_forOf", "__safe_for", "__safe_while", "__safe_doWhile", "__maxIterations", "parseInt", "parseFloat", "isNaN", "isFinite", "encodeURI", "decodeURI", "encodeURIComponent", "decodeURIComponent"]; /** * Globals for STANDARD security level * Same as SECURE (room for future expansion) */ export declare const AGENTSCRIPT_STANDARD_GLOBALS: readonly ["callTool", "__safe_callTool", "Math", "JSON", "Array", "Object", "String", "Number", "Date", "undefined", "NaN", "Infinity", "__safe_forOf", "__safe_for", "__safe_while", "__safe_doWhile", "__maxIterations", "parseInt", "parseFloat", "isNaN", "isFinite", "encodeURI", "decodeURI", "encodeURIComponent", "decodeURIComponent"]; /** * Globals for PERMISSIVE security level * Adds debugging/logging capabilities */ export declare const AGENTSCRIPT_PERMISSIVE_GLOBALS: readonly ["callTool", "__safe_callTool", "Math", "JSON", "Array", "Object", "String", "Number", "Date", "undefined", "NaN", "Infinity", "__safe_forOf", "__safe_for", "__safe_while", "__safe_doWhile", "__maxIterations", "parseInt", "parseFloat", "isNaN", "isFinite", "encodeURI", "decodeURI", "encodeURIComponent", "decodeURIComponent", "console", "__safe_console"]; export declare const AGENTSCRIPT_BASE_GLOBALS: readonly ["callTool", "__safe_callTool", "Math", "JSON", "Array", "Object", "String", "Number", "Date", "undefined", "NaN", "Infinity", "__safe_forOf", "__safe_for", "__safe_while", "__safe_doWhile", "__maxIterations"]; /** * Security level type for globals selection */ export type SecurityLevel = 'STRICT' | 'SECURE' | 'STANDARD' | 'PERMISSIVE'; /** * Get the allowed globals for a given security level * * Security levels (from most to least restrictive): * - STRICT: Absolute minimum (core types + callTool only) * - SECURE: Adds safe utility functions (parseInt, encodeURI, etc.) * - STANDARD: Same as SECURE (room for future expansion) * - PERMISSIVE: Adds console for debugging * * @param securityLevel The security level * @returns Array of allowed global identifiers */ export declare function getAgentScriptGlobals(securityLevel: SecurityLevel | string): readonly string[]; /** * Configuration options for AgentScript preset */ export interface AgentScriptOptions { /** * Security level that determines default allowed globals. * If allowedGlobals is also provided, it takes precedence. * * - STRICT/SECURE: Base globals only (core types + callTool) * - STANDARD: Adds utility functions (parseInt, encodeURI, etc.) * - PERMISSIVE: Adds console for debugging * * Default: 'STANDARD' */ securityLevel?: SecurityLevel | string; /** * List of allowed global identifiers (APIs available to agent code) * If provided, overrides the securityLevel-based defaults. * Default: Based on securityLevel (see getAgentScriptGlobals) */ allowedGlobals?: string[]; /** * Additional identifiers to block beyond the default dangerous set */ additionalDisallowedIdentifiers?: string[]; /** * Whether to allow arrow functions (for array methods like map, filter) * Default: true */ allowArrowFunctions?: boolean; /** * Allow specific loop types * Default: { allowFor: true, allowForOf: true } (bounded loops only) */ allowedLoops?: { allowFor?: boolean; allowWhile?: boolean; allowDoWhile?: boolean; allowForIn?: boolean; allowForOf?: boolean; }; /** * Validation rules for callTool arguments */ callToolValidation?: { /** Minimum number of arguments */ minArgs?: number; /** Maximum number of arguments */ maxArgs?: number; /** Expected types for each argument position */ expectedTypes?: Array<'string' | 'number' | 'boolean' | 'object' | 'array' | 'function' | 'literal'>; }; /** * Reserved prefixes that user code cannot use * Default: ['__ag_', '__safe_'] */ reservedPrefixes?: string[]; /** * Configuration for static call target validation * Ensures callTool first argument is always a static string literal */ staticCallTarget?: { /** * Whether to enable static call target validation * Default: true */ enabled?: boolean; /** * Whitelist of allowed tool names (exact strings or RegExp patterns) * If provided, only these tools can be called */ allowedToolNames?: (string | RegExp)[]; }; /** * Whether to require at least one callTool invocation * When enabled, scripts that don't call callTool will fail validation * Default: false */ requireCallTool?: boolean; /** * Allow dynamic (computed) array size for .fill() operations * * When true, `Array(dynamicSize).fill()` is allowed because runtime memory * patching will enforce the limit. Only enable this when memoryLimit is * configured at runtime. * * When false (default), only literal sizes are allowed for .fill() to * prevent memory exhaustion in environments without runtime protection. * * Default: false */ allowDynamicArrayFill?: boolean; } /** * Creates an AgentScript preset - a strict JS subset for AI agent orchestration * * **AgentScript Language (v1):** * AgentScript is a restricted subset of JavaScript designed for safe orchestration: * - Simple, linear code flow (no recursion, no complex control flow) * - Tool calls via `await callTool(name, args)` * - Data manipulation with array methods (map, filter, reduce) * - Bounded loops only (for, for-of with iteration limits) * - No access to dangerous globals (process, require, eval, etc.) * - No user-defined functions (v1 - prevents recursion) * * **Use Cases:** * - AI agents orchestrating multiple MCP tool calls * - Data aggregation across multiple API calls * - Simple conditional logic and filtering * - Result transformation and formatting * * **Example AgentScript Code:** * ```javascript * // Get active admin users * const users = await callTool('users:list', { * limit: 100, * filter: { role: 'admin', active: true } * }); * * // Get unpaid invoices for each admin * const results = []; * for (const user of users.items) { * const invoices = await callTool('billing:listInvoices', { * userId: user.id, * status: 'unpaid' * }); * * if (invoices.items.length > 0) { * results.push({ * userId: user.id, * userName: user.name, * unpaidCount: invoices.items.length, * totalAmount: invoices.items.reduce((sum, inv) => sum + inv.amount, 0) * }); * } * } * * return results; * ``` * * **Security Model:** * 1. **Static Validation** (this preset): * - Block dangerous globals (process, require, eval, etc.) * - Block user-defined functions (no recursion) * - Block unknown identifiers (whitelist-only) * - Block reserved prefixes (__ag_, __safe_) * - Allow only safe constructs * * 2. **Transformation** (separate step): * - Wrap code in `async function __ag_main() {}` * - Transform `callTool` → `__safe_callTool` * - Transform loops → `__safe_for`/`__safe_forOf` * * 3. **Runtime** (Enclave): * - Execute in isolated sandbox (vm2/nodevm/wasm) * - Provide only `__safe_*` globals * - Enforce timeouts and resource limits * * @param options Configuration options for the preset * @returns Array of configured validation rules * * @example * ```typescript * import { createAgentScriptPreset } from 'ast-guard'; * * // Default configuration * const rules = createAgentScriptPreset(); * * // Custom configuration * const rules = createAgentScriptPreset({ * allowedGlobals: ['callTool', 'getTool', 'Math', 'JSON'], * allowArrowFunctions: true, * allowedLoops: { allowFor: true, allowForOf: true }, * }); * ``` */ export declare function createAgentScriptPreset(options?: AgentScriptOptions): ValidationRule[];