/** * AST Utilities for JavaScript/TypeScript Analysis * Provides AST parsing and traversal utilities using Babel Parser * * Inspired by Semgrep's AST pattern matching */ import { NodePath } from '@babel/traverse'; import * as t from '@babel/types'; /** * AST Parse Options */ export interface ASTParseOptions { /** Source type (script, module, unambiguous) */ sourceType?: 'script' | 'module' | 'unambiguous'; /** Enable TypeScript parsing */ typescript?: boolean; /** Enable JSX parsing */ jsx?: boolean; /** Allow return outside function */ allowReturnOutsideFunction?: boolean; /** Error recovery mode */ errorRecovery?: boolean; } /** * AST Node Location */ export interface ASTLocation { startLine: number; endLine: number; startColumn: number; endColumn: number; } /** * Dangerous call detected in AST */ export interface DangerousCall { /** Name of the dangerous function/method */ name: string; /** Full call expression code */ code: string; /** Location in source */ location: ASTLocation; /** Type of dangerous pattern */ patternType: DangerousPatternType; /** Arguments passed to the call */ arguments: string[]; /** Caller object (for method calls) */ callee?: string; /** Additional context */ context?: string; } /** * Types of dangerous patterns */ export declare enum DangerousPatternType { CODE_EXECUTION = "code_execution", COMMAND_INJECTION = "command_injection", PROTOTYPE_POLLUTION = "prototype_pollution", XSS_SINK = "xss_sink", DYNAMIC_REQUIRE = "dynamic_require", INSECURE_RANDOM = "insecure_random", HARDCODED_SECRET = "hardcoded_secret", DANGEROUS_REGEX = "dangerous_regex", UNSAFE_ASSIGNMENT = "unsafe_assignment", NETWORK_REQUEST = "network_request", FILE_OPERATION = "file_operation", CRYPTO_WEAKNESS = "crypto_weakness" } /** * AST Pattern definition */ export interface ASTPattern { /** Pattern type */ type: DangerousPatternType; /** Description of the pattern */ description: string; /** Matcher function */ matcher: (path: NodePath, context: ASTContext) => boolean; /** Extract relevant information */ extractor?: (path: NodePath) => Partial; } /** * AST Analysis Context */ export interface ASTContext { /** Current file path */ filePath: string; /** Source content */ source: string; /** Detected imports/requires */ imports: Map; /** Is this TypeScript? */ isTypeScript: boolean; /** Is this JSX? */ isJSX: boolean; } /** * AST Utilities Class */ export declare class ASTUtils { private ast; private source; private context; /** * Parse source code to AST */ parse(source: string, options?: ASTParseOptions): t.File | null; /** * Safe AST parsing with automatic feature detection */ safeParse(source: string, filePath: string): t.File | null; /** * Find all dangerous calls in the AST */ findDangerousCalls(filePath: string): DangerousCall[]; /** * Analyze a call expression for dangerous patterns */ private analyzeCallExpression; /** * Analyze a method call (obj.method()) */ private analyzeMemberCall; /** * Analyze new expression: new Function(), etc. */ private analyzeNewExpression; /** * Analyze assignment for dangerous patterns */ private analyzeAssignment; /** * Analyze member expression for dangerous patterns */ private analyzeMemberExpression; /** * Extract imports/requires from the AST */ private extractImports; /** * Get the source code for a node */ private getNodeCode; /** * Get location information for a node */ private getLocation; /** * Extract arguments as strings */ private extractArguments; /** * Get full name of member expression */ private getMemberExpressionName; /** * Find all string literals that look like hardcoded secrets */ findHardcodedSecrets(): DangerousCall[]; /** * Find ReDoS-vulnerable regex patterns */ findDangerousRegex(): DangerousCall[]; /** * Check if code contains anti-debugging techniques */ findAntiDebugging(): DangerousCall[]; } export default ASTUtils; //# sourceMappingURL=astUtils.d.ts.map