Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | /** * Local Scanner Service * Performs security vulnerability detection on local repositories */ const fs = require('fs-extra') const path = require('path') const { glob } = require('glob') const ignore = require('ignore') const crypto = require('crypto') class LocalScanner { constructor(options = {}) { this.path = options.path || '.' this.severity = options.severity || 'medium' this.exclude = options.exclude || [] this.include = options.include || [] this.respectGitignore = options.respectGitignore !== false this.aiPatterns = options.aiPatterns || false this.framework = options.framework this.verbose = options.verbose || false this.vulnerabilities = [] this.stats = { filesScanned: 0, linesScanned: 0, scanDuration: 0, aiPatternsFound: 0 } } async scan() { const startTime = Date.now() try { // Get files to scan const files = await this.getFilesToScan() if (this.verbose) { console.log(`Found ${files.length} files to scan`) } // Scan each file for (const file of files) { await this.scanFile(file) } this.stats.scanDuration = Date.now() - startTime return { vulnerabilities: this.vulnerabilities, stats: this.stats, scanId: crypto.randomUUID(), timestamp: new Date().toISOString() } } catch (error) { throw new Error(`Scan failed: ${error.message}`) } } async getFilesToScan() { const files = [] const ig = ignore() // Load .gitignore if respecting it if (this.respectGitignore) { try { const gitignorePath = path.join(this.path, '.gitignore') if (await fs.exists(gitignorePath)) { const gitignoreContent = await fs.readFile(gitignorePath, 'utf8') ig.add(gitignoreContent) } } catch (error) { // Continue if .gitignore can't be read } } // Add exclude patterns if (this.exclude.length > 0) { ig.add(this.exclude) } // Default exclusions ig.add([ 'node_modules/**', '.git/**', '.vscode/**', '.idea/**', '*.log', '*.tmp', 'dist/**', 'build/**', '__pycache__/**', '*.pyc', '.pytest_cache/**' ]) // Get all files const globPattern = this.include.length > 0 ? `{${this.include.join(',')}}` : '**/*' const allFiles = await glob(globPattern, { cwd: this.path, nodir: true, ignore: ['node_modules/**', '.git/**'] }) // Filter files const filteredFiles = allFiles.filter(file => { // Apply ignore rules if (ig.ignores(file)) return false // Only scan code files const ext = path.extname(file).toLowerCase() const codeExtensions = [ '.js', '.jsx', '.ts', '.tsx', '.py', '.rb', '.php', '.java', '.go', '.rs', '.cpp', '.c', '.cs', '.swift', '.kt', '.scala', '.vue', '.svelte', '.html', '.css', '.yaml', '.yml', '.json', '.xml', '.sql', '.sh', '.bash', '.ps1' ] return codeExtensions.includes(ext) }) return filteredFiles.map(file => path.join(this.path, file)) } async scanFile(filePath) { try { const content = await fs.readFile(filePath, 'utf8') const lines = content.split('\n') this.stats.filesScanned++ this.stats.linesScanned += lines.length // Detect vulnerabilities const fileVulnerabilities = this.detectVulnerabilities(filePath, content, lines) this.vulnerabilities.push(...fileVulnerabilities) } catch (error) { if (this.verbose) { console.warn(`Could not scan file ${filePath}: ${error.message}`) } } } detectVulnerabilities(filePath, content, lines) { const vulnerabilities = [] const fileName = path.basename(filePath) const ext = path.extname(filePath).toLowerCase() // Security patterns to detect const patterns = [ // SQL Injection { pattern: /\$\{[^}]*\}.*(?:SELECT|INSERT|UPDATE|DELETE)/i, type: 'sql_injection', severity: 'high', message: 'Potential SQL injection vulnerability' }, // XSS { pattern: /innerHTML\s*=.*\$\{|dangerouslySetInnerHTML/i, type: 'xss', severity: 'medium', message: 'Potential XSS vulnerability' }, // Hardcoded secrets { pattern: /(password|secret|key|token)\s*[:=]\s*['"]\w{8,}/i, type: 'exposed_secret', severity: 'critical', message: 'Hardcoded secret detected' }, // Command injection { pattern: /exec\(.*\$\{|system\(.*\$\{|eval\(.*\$\{/i, type: 'command_injection', severity: 'critical', message: 'Potential command injection' }, // Path traversal { pattern: /\.\.\//, type: 'path_traversal', severity: 'medium', message: 'Potential path traversal vulnerability' } ] // AI-specific patterns if (this.aiPatterns) { patterns.push( { pattern: /\/\*\s*AI\s*generated|AI-generated|Generated by AI/i, type: 'ai_generated_code', severity: 'info', message: 'AI-generated code detected' }, { pattern: /prompt.*injection|inject.*prompt/i, type: 'prompt_injection', severity: 'high', message: 'Potential prompt injection vulnerability' } ) } // Check each line lines.forEach((line, index) => { patterns.forEach(pattern => { if (pattern.pattern.test(line)) { // Skip if severity is below threshold const severityLevel = { 'info': 0, 'low': 1, 'medium': 2, 'high': 3, 'critical': 4 } if (severityLevel[pattern.severity] < severityLevel[this.severity]) { return } vulnerabilities.push({ type: pattern.type, severity: pattern.severity, message: pattern.message, file: path.relative(this.path, filePath), line: index + 1, code: line.trim(), description: this.getVulnerabilityDescription(pattern.type) }) if (pattern.type === 'ai_generated_code') { this.stats.aiPatternsFound++ } } }) }) return vulnerabilities } getVulnerabilityDescription(type) { const descriptions = { sql_injection: 'Dynamic SQL queries with user input can lead to data theft or corruption', xss: 'Unescaped user input in HTML can execute malicious scripts', exposed_secret: 'Hardcoded credentials in source code pose security risks', command_injection: 'Dynamic command execution can lead to system compromise', path_traversal: 'File path manipulation can access unauthorized files', ai_generated_code: 'AI-generated code may contain subtle security vulnerabilities', prompt_injection: 'AI prompt manipulation can bypass security controls' } return descriptions[type] || 'Security vulnerability detected' } } module.exports = LocalScanner |