/** * Main PreScanner class - Layer 0 Defense for AST Guard. * * The PreScanner runs BEFORE the JavaScript parser (acorn) to catch attacks * that could DoS the parser itself. This provides defense-in-depth security. * * @module pre-scanner/pre-scanner */ import type { PreScannerConfig, PreScannerPresetLevel } from './config'; import type { PreScanResult } from './scan-state'; /** * Options for creating a PreScanner instance */ export interface PreScannerOptions { /** * Preset level to use as base configuration. * Default: 'standard' */ preset?: PreScannerPresetLevel; /** * Custom configuration overrides. * These are merged with the preset configuration. */ config?: Partial; } /** * PreScanner - Layer 0 security scanning before AST parsing. * * This scanner runs BEFORE acorn.parse() to catch attacks that could: * - Exhaust memory (huge inputs) * - Overflow the parser stack (deep nesting) * - Cause ReDoS via regex literals * - Exploit Unicode vulnerabilities (Trojan Source) * * @example * ```typescript * // Default configuration (standard preset) * const scanner = new PreScanner(); * const result = scanner.scan(sourceCode); * * if (!result.success) { * throw new Error(result.fatalIssue?.message); * } * * // Now safe to parse with acorn * const ast = acorn.parse(sourceCode, options); * ``` * * @example * ```typescript * // AgentScript preset (maximum security) * const scanner = new PreScanner({ preset: 'agentscript' }); * * // Or with custom overrides * const scanner = new PreScanner({ * preset: 'secure', * config: { * maxInputSize: 200 * 1024, // 200KB instead of 1MB * regexMode: 'block', // Block all regex * }, * }); * ``` */ export declare class PreScanner { /** * The resolved configuration for this scanner */ readonly config: PreScannerConfig; /** * The preset level used (if any) */ readonly presetLevel: PreScannerPresetLevel; /** * Create a new PreScanner instance. * * @param options - Configuration options */ constructor(options?: PreScannerOptions); /** * Scan source code for security issues. * * This method performs multiple passes over the source code, * checking for various security issues in order of severity. * It stops early if a fatal issue is found. * * @param source - The source code to scan * @returns Scan result with success status, issues, and statistics * * @example * ```typescript * const result = scanner.scan(code); * * if (result.success) { * // Safe to proceed with AST parsing * console.log(`Scan passed in ${result.stats.scanDurationMs}ms`); * } else { * // Handle security issue * console.error(`Blocked: ${result.fatalIssue?.message}`); * console.error(`Error code: ${result.fatalIssue?.code}`); * } * ``` */ scan(source: string): PreScanResult; /** * Quick validation that only checks critical security issues. * Faster than full scan but may miss some warnings. * * @param source - The source code to validate * @returns true if the source passes critical checks */ quickValidate(source: string): boolean; /** * Get the current configuration. * Returns a copy to prevent mutation. */ getConfig(): Readonly; /** * Create a new scanner with modified configuration. * The original scanner is not modified. * * @param overrides - Configuration overrides * @returns New PreScanner instance with merged configuration */ withConfig(overrides: Partial): PreScanner; /** * Create scanners for common use cases */ static forAgentScript(overrides?: Partial): PreScanner; static forStrict(overrides?: Partial): PreScanner; static forSecure(overrides?: Partial): PreScanner; static forStandard(overrides?: Partial): PreScanner; static forPermissive(overrides?: Partial): PreScanner; } /** * Convenience function to scan source code with default settings. * * @param source - The source code to scan * @param preset - Optional preset level (default: 'standard') * @returns Scan result * * @example * ```typescript * const result = preScan(code); * if (!result.success) { * throw new Error(result.fatalIssue?.message); * } * ``` */ export declare function preScan(source: string, preset?: PreScannerPresetLevel): PreScanResult; /** * Convenience function for quick validation. * * @param source - The source code to validate * @param preset - Optional preset level (default: 'standard') * @returns true if source passes validation */ export declare function isPreScanValid(source: string, preset?: PreScannerPresetLevel): boolean;