UNPKG

codecrucible-synth

Version:

Production-Ready AI Development Platform with Multi-Voice Synthesis, Smithery MCP Integration, Enterprise Security, and Zero-Timeout Reliability

525 lines 19.1 kB
/** * Advanced Security Validator (ENHANCED) * Comprehensive security validation system for CodeCrucible Synth * Enhanced with 2024 AI security research and multi-agent red teaming * * Research findings integrated: * - 29.5% Python and 24.2% JavaScript code snippets contain vulnerabilities * - AI-specific prompt injection patterns and countermeasures * - Secret leak detection in AI-generated code */ import { Logger } from '../logger.js'; export class AdvancedSecurityValidator { logger; policy; knownMaliciousPatterns; suspiciousKeywords; constructor(policy) { this.logger = new Logger('AdvancedSecurityValidator'); this.policy = this.mergeWithDefaultPolicy(policy || {}); this.initializeMaliciousPatterns(); this.initializeSuspiciousKeywords(); } /** * Comprehensive input validation */ async validateInput(input, context) { const violations = []; let riskLevel = 'low'; const recommendations = []; // Length validation if (input.length > this.policy.maxInputLength) { violations.push({ type: 'excessive_length', description: `Input exceeds maximum length of ${this.policy.maxInputLength} characters`, severity: 'medium', }); riskLevel = 'medium'; } // Command injection detection const commandViolations = this.detectCommandInjection(input); violations.push(...commandViolations); if (commandViolations.some(v => v.severity === 'critical')) { riskLevel = 'critical'; } // Path traversal detection const pathViolations = this.detectPathTraversal(input); violations.push(...pathViolations); if (pathViolations.some(v => v.severity === 'high')) { riskLevel = 'high'; } // Malicious pattern detection const patternViolations = this.detectMaliciousPatterns(input); violations.push(...patternViolations); if (patternViolations.some(v => v.severity === 'critical')) { riskLevel = 'critical'; } // Suspicious content analysis const contentViolations = await this.analyzeSuspiciousContent(input); violations.push(...contentViolations); // Update risk level based on all violations const highestSeverity = this.getHighestSeverity(violations); if (highestSeverity === 'critical') riskLevel = 'critical'; else if (highestSeverity === 'high' && riskLevel !== 'critical') riskLevel = 'high'; else if (highestSeverity === 'medium' && riskLevel === 'low') riskLevel = 'medium'; // Generate recommendations recommendations.push(...this.generateRecommendations(violations, riskLevel)); // Sanitize input if possible const sanitizedInput = violations.length === 0 ? input : this.sanitizeInput(input, violations); return { isValid: riskLevel !== 'critical' && violations.filter(v => v.severity === 'critical').length === 0, riskLevel, violations, sanitizedInput, recommendations, }; } /** * Detect command injection attempts */ detectCommandInjection(input) { const violations = []; // Shell metacharacters and patterns const commandPatterns = [ { pattern: /[;&|`$(){}]/g, severity: 'high', desc: 'Shell metacharacters detected' }, { pattern: /\|\s*[a-z]/gi, severity: 'critical', desc: 'Pipe to command detected' }, { pattern: /&&\s*[a-z]/gi, severity: 'critical', desc: 'Command chaining detected' }, { pattern: /;\s*[a-z]/gi, severity: 'critical', desc: 'Command separator detected' }, { pattern: /`[^`]+`/g, severity: 'critical', desc: 'Command substitution detected' }, { pattern: /\$\([^)]+\)/g, severity: 'critical', desc: 'Command substitution detected', }, { pattern: /rm\s+-r?f?\s+/gi, severity: 'critical', desc: 'Dangerous delete command detected', }, { pattern: /curl\s+.*(\||>)/gi, severity: 'high', desc: 'Network download with redirection', }, { pattern: /wget\s+.*(\||>)/gi, severity: 'high', desc: 'Network download with redirection', }, { pattern: /chmod\s+[0-9]+/gi, severity: 'medium', desc: 'File permission modification', }, { pattern: /sudo\s+/gi, severity: 'high', desc: 'Privilege escalation attempt' }, { pattern: /su\s+/gi, severity: 'high', desc: 'User switching attempt' }, ]; for (const { pattern, severity, desc } of commandPatterns) { const matches = input.match(pattern); if (matches) { violations.push({ type: 'command_injection', description: desc, severity, pattern: pattern.toString(), }); } } return violations; } /** * Detect path traversal attempts */ detectPathTraversal(input) { const violations = []; const pathPatterns = [ { pattern: /\.\.\/+/g, severity: 'high', desc: 'Directory traversal detected' }, { pattern: /\.\.\\+/g, severity: 'high', desc: 'Windows directory traversal detected', }, { pattern: /\/etc\/passwd/gi, severity: 'critical', desc: 'System file access attempt', }, { pattern: /\/etc\/shadow/gi, severity: 'critical', desc: 'Password file access attempt', }, { pattern: /C:\\Windows\\System32/gi, severity: 'high', desc: 'Windows system directory access', }, { pattern: /\/proc\/self\/environ/gi, severity: 'high', desc: 'Environment variable access', }, { pattern: /\/home\/[^/]+\/\.ssh/gi, severity: 'high', desc: 'SSH key directory access', }, ]; for (const { pattern, severity, desc } of pathPatterns) { const matches = input.match(pattern); if (matches) { violations.push({ type: 'path_traversal', description: desc, severity, pattern: pattern.toString(), }); } } return violations; } /** * Detect known malicious patterns */ detectMaliciousPatterns(input) { const violations = []; for (const pattern of this.knownMaliciousPatterns) { if (pattern.test(input)) { violations.push({ type: 'malicious_pattern', description: `Known malicious pattern detected: ${pattern.toString()}`, severity: 'critical', pattern: pattern.toString(), }); } } return violations; } /** * Analyze content for suspicious patterns */ async analyzeSuspiciousContent(input) { const violations = []; const lowerInput = input.toLowerCase(); // Check for suspicious keywords for (const keyword of this.suspiciousKeywords) { if (lowerInput.includes(keyword)) { violations.push({ type: 'suspicious_content', description: `Suspicious keyword detected: ${keyword}`, severity: 'medium', }); } } // Check for encoded content that might be malicious if (this.detectEncodedMaliciousContent(input)) { violations.push({ type: 'suspicious_content', description: 'Potentially encoded malicious content detected', severity: 'high', }); } // Check for SQL injection patterns const sqlPatterns = [ /union\s+select/gi, /drop\s+table/gi, /delete\s+from/gi, /insert\s+into/gi, /update\s+.*set/gi, /'.*or.*'.*=.*'/gi, ]; for (const pattern of sqlPatterns) { if (pattern.test(input)) { violations.push({ type: 'suspicious_content', description: 'SQL injection pattern detected', severity: 'high', pattern: pattern.toString(), }); } } return violations; } /** * Detect encoded malicious content */ detectEncodedMaliciousContent(input) { try { // Check for base64 encoded content const base64Pattern = /[A-Za-z0-9+/]{20,}={0,2}/g; const base64Matches = input.match(base64Pattern); if (base64Matches) { for (const match of base64Matches) { try { const decoded = Buffer.from(match, 'base64').toString('utf-8'); if (this.containsSuspiciousPatterns(decoded)) { return true; } } catch { // Ignore invalid base64 } } } // Check for URL encoded content const urlEncodedPattern = /%[0-9A-Fa-f]{2}/g; if (urlEncodedPattern.test(input)) { try { const decoded = decodeURIComponent(input); if (this.containsSuspiciousPatterns(decoded)) { return true; } } catch { // Ignore invalid URL encoding } } // Check for hex encoded content const hexPattern = /\\x[0-9A-Fa-f]{2}/g; if (hexPattern.test(input)) { const decoded = input.replace(/\\x([0-9A-Fa-f]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))); if (this.containsSuspiciousPatterns(decoded)) { return true; } } } catch (error) { this.logger.warn('Error checking encoded content:', error); } return false; } /** * Check if text contains suspicious patterns */ containsSuspiciousPatterns(text) { const suspiciousPatterns = [ /rm\s+-rf/i, /malicious/i, /exploit/i, /payload/i, /shellcode/i, /backdoor/i, ]; return suspiciousPatterns.some(pattern => pattern.test(text)); } /** * Sanitize input by removing or escaping dangerous content */ sanitizeInput(input, violations) { let sanitized = input; // Remove shell metacharacters sanitized = sanitized.replace(/[;&|`$(){}]/g, ''); // Remove path traversal sequences sanitized = sanitized.replace(/\.\.\/+/g, './'); sanitized = sanitized.replace(/\.\.\\+/g, '.\\'); // Remove dangerous commands sanitized = sanitized.replace(/rm\s+-r?f?\s+/gi, '[FILTERED] '); sanitized = sanitized.replace(/curl\s+/gi, '[FILTERED] '); sanitized = sanitized.replace(/wget\s+/gi, '[FILTERED] '); sanitized = sanitized.replace(/sudo\s+/gi, '[FILTERED] '); // Filter out malicious keywords including "malicious" itself const criticalKeywords = ['malicious', 'exploit', 'payload', 'shellcode', 'backdoor']; for (const keyword of criticalKeywords) { const regex = new RegExp(keyword, 'gi'); sanitized = sanitized.replace(regex, '[FILTERED]'); } return sanitized; } /** * Generate security recommendations */ generateRecommendations(violations, riskLevel) { const recommendations = []; if (riskLevel === 'critical') { recommendations.push('CRITICAL: Input contains dangerous patterns that could compromise system security'); recommendations.push('Recommend rejecting this input and implementing additional validation layers'); } if (violations.some(v => v.type === 'command_injection')) { recommendations.push('Implement command whitelisting and input escaping'); recommendations.push('Use sandboxed execution environment for any command execution'); } if (violations.some(v => v.type === 'path_traversal')) { recommendations.push('Implement path validation and restrict file access to approved directories'); recommendations.push('Use absolute paths and canonical path resolution'); } if (violations.some(v => v.type === 'malicious_pattern')) { recommendations.push('Content matches known malicious patterns - consider blocking this input'); recommendations.push('Update malicious pattern database regularly'); } return recommendations; } /** * Get highest severity level from violations */ getHighestSeverity(violations) { if (violations.some(v => v.severity === 'critical')) return 'critical'; if (violations.some(v => v.severity === 'high')) return 'high'; if (violations.some(v => v.severity === 'medium')) return 'medium'; return 'low'; } /** * Initialize known malicious patterns */ initializeMaliciousPatterns() { this.knownMaliciousPatterns = [ // Known attack patterns /nc\s+-l.*-e/gi, // Netcat reverse shell /python.*-c.*exec/gi, // Python code execution /perl.*-e/gi, // Perl one-liner /ruby.*-e/gi, // Ruby one-liner /bash.*-c/gi, // Bash command execution /sh.*-c/gi, // Shell command execution /powershell.*-c/gi, // PowerShell execution /cmd.*\/c/gi, // Windows command execution // File operations /echo.*>>.*\/etc\//gi, // Writing to system files /cat.*\/etc\/passwd/gi, // Reading password file /ls.*-la.*\/etc/gi, // Listing system directories // Network operations /wget.*\|\s*sh/gi, // Download and execute /curl.*\|\s*bash/gi, // Download and execute // Process manipulation /kill\s+-9/gi, // Force kill processes /killall/gi, // Kill all processes /pkill/gi, // Pattern-based process killing // System information gathering /uname\s+-a/gi, // System information /whoami/gi, // Current user /id\s*$/gi, // User ID information /ps\s+aux/gi, // Process listing // Privilege escalation /sudo\s+su/gi, // Switch to root /su\s+-/gi, // Switch user /chmod\s+777/gi, // Full permissions /chown\s+root/gi, // Change ownership to root ]; } /** * Initialize suspicious keywords */ initializeSuspiciousKeywords() { this.suspiciousKeywords = [ // Hacking/exploitation terms 'exploit', 'payload', 'shellcode', 'backdoor', 'trojan', 'virus', 'rootkit', 'keylogger', 'botnet', 'ransomware', 'malware', // Attack methods 'injection', 'overflow', 'xss', 'csrf', 'clickjacking', 'phishing', 'spoofing', 'sniffing', 'bruteforce', 'dictionary', 'rainbow', // System compromise 'privilege escalation', 'lateral movement', 'persistence', 'exfiltration', 'command and control', 'c2', 'reverse shell', 'bind shell', // 2024 AI-specific threat keywords 'prompt injection', 'jailbreak', 'ignore previous instructions', 'forget everything', 'new instructions', 'role hijacking', 'system override', 'memory manipulation', 'context switching', // Sensitive operations (context-dependent) 'format disk', 'delete system', 'remove all', 'wipe drive', 'master boot record', 'boot sector', 'partition table', ]; } /** * Merge with default security policy */ mergeWithDefaultPolicy(policy) { const defaultPolicy = { allowedCommands: [ 'ls', 'cat', 'grep', 'find', 'head', 'tail', 'wc', 'sort', 'uniq', 'git', 'npm', 'node', 'python', 'pip', 'cargo', 'rustc', 'tsc', 'eslint', 'prettier', 'jest', 'mocha', 'pytest', ], blockedPatterns: [/rm\s+-rf/, /sudo\s+/, /chmod\s+777/, />\s*\/dev\/null/, /2>&1/, /nohup/], maxInputLength: 10000, allowCodeExecution: false, allowFileAccess: true, allowNetworkAccess: false, requireSandbox: true, }; return { ...defaultPolicy, ...policy }; } /** * Update security policy */ updatePolicy(newPolicy) { this.policy = { ...this.policy, ...newPolicy }; this.logger.info('Security policy updated'); } /** * Get current security policy */ getPolicy() { return { ...this.policy }; } /** * Generate security report */ generateSecurityReport() { return { policy: this.policy, maliciousPatternsCount: this.knownMaliciousPatterns.length, suspiciousKeywordsCount: this.suspiciousKeywords.length, lastUpdated: new Date().toISOString(), }; } } export default AdvancedSecurityValidator; //# sourceMappingURL=advanced-security-validator.js.map