|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Security scanning script for detecting secrets and API keys |
| 5 | + * Can be run manually or as part of CI/CD pipeline |
| 6 | + */ |
| 7 | + |
| 8 | +import { readFileSync, readdirSync, statSync } from 'fs'; |
| 9 | +import { join, relative } from 'path'; |
| 10 | + |
| 11 | +const RED = '\x1b[31m'; |
| 12 | +const GREEN = '\x1b[32m'; |
| 13 | +const YELLOW = '\x1b[33m'; |
| 14 | +const RESET = '\x1b[0m'; |
| 15 | + |
| 16 | +const SECRET_PATTERNS = [ |
| 17 | + { name: 'API Key', regex: /api[_-]?key\s*[=:]\s*['"][a-zA-Z0-9]{20,}['"]/gi }, |
| 18 | + { name: 'Secret Key', regex: /secret[_-]?key\s*[=:]\s*['"][a-zA-Z0-9]{20,}['"]/gi }, |
| 19 | + { name: 'Access Token', regex: /access[_-]?token\s*[=:]\s*['"][a-zA-Z0-9]{20,}['"]/gi }, |
| 20 | + { name: 'Private Key', regex: /-----BEGIN (RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----/gi }, |
| 21 | + { name: 'AWS Secret', regex: /AWS[_-]?SECRET[_-]?ACCESS[_-]?KEY\s*[=:]\s*['"][A-Za-z0-9/+=]{40}['"]/gi }, |
| 22 | + { name: 'Groq API Key', regex: /GROQ_API_KEY\s*[=:]\s*['"]sk-[a-zA-Z0-9]{20,}['"]/gi }, |
| 23 | + { name: 'OpenAI API Key', regex: /OPENAI_API_KEY\s*[=:]\s*['"]sk-[a-zA-Z0-9]{20,}['"]/gi }, |
| 24 | + { name: 'Anthropic API Key', regex: /ANTHROPIC_API_KEY\s*[=:]\s*['"]sk-ant-[a-zA-Z0-9]{20,}['"]/gi }, |
| 25 | + { name: 'OpenRouter API Key', regex: /OPENROUTER_API_KEY\s*[=:]\s*['"]sk-or-v1-[a-zA-Z0-9]{20,}['"]/gi }, |
| 26 | + { name: 'Context7 API Key', regex: /ctx7sk-[a-zA-Z0-9_-]{20,}/gi }, |
| 27 | + { name: 'Reference API Key', regex: /ref-[a-zA-Z0-9_-]{20,}/gi }, |
| 28 | + { name: 'GitHub Token', regex: /ghp_[a-zA-Z0-9]{36}/gi }, |
| 29 | + { name: 'GitLab Token', regex: /glpat-[a-zA-Z0-9]{20,}/gi }, |
| 30 | +]; |
| 31 | + |
| 32 | +const EXCLUDED_DIRS = [ |
| 33 | + 'node_modules', |
| 34 | + 'dist', |
| 35 | + 'build', |
| 36 | + '.git', |
| 37 | + 'coverage', |
| 38 | + 'target', |
| 39 | + '__pycache__', |
| 40 | +]; |
| 41 | + |
| 42 | +const EXCLUDED_FILES = [ |
| 43 | + '.env.example', |
| 44 | + 'security-scan.js', |
| 45 | + 'package-lock.json', |
| 46 | + 'yarn.lock', |
| 47 | + 'pnpm-lock.yaml', |
| 48 | +]; |
| 49 | + |
| 50 | +function shouldExclude(dirPath) { |
| 51 | + return EXCLUDED_DIRS.some(excluded => dirPath.includes(excluded)); |
| 52 | +} |
| 53 | + |
| 54 | +function scanFile(filePath, rootDir) { |
| 55 | + const relativePath = relative(rootDir, filePath); |
| 56 | + |
| 57 | + if (EXCLUDED_FILES.includes(relativePath)) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + const content = readFileSync(filePath, 'utf-8'); |
| 63 | + const findings = []; |
| 64 | + |
| 65 | + for (const pattern of SECRET_PATTERNS) { |
| 66 | + const matches = content.matchAll(pattern.regex); |
| 67 | + for (const match of matches) { |
| 68 | + findings.push({ |
| 69 | + file: relativePath, |
| 70 | + type: pattern.name, |
| 71 | + line: content.substring(0, match.index).split('\n').length, |
| 72 | + match: match[0].substring(0, 50) + '...', |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return findings; |
| 78 | + } catch (error) { |
| 79 | + return []; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function scanDirectory(dirPath, rootDir) { |
| 84 | + let findings = []; |
| 85 | + |
| 86 | + try { |
| 87 | + const entries = readdirSync(dirPath, { withFileTypes: true }); |
| 88 | + |
| 89 | + for (const entry of entries) { |
| 90 | + const fullPath = join(dirPath, entry.name); |
| 91 | + |
| 92 | + if (entry.isDirectory()) { |
| 93 | + if (!shouldExclude(fullPath)) { |
| 94 | + findings = findings.concat(scanDirectory(fullPath, rootDir)); |
| 95 | + } |
| 96 | + } else if (entry.isFile()) { |
| 97 | + findings = findings.concat(scanFile(fullPath, rootDir)); |
| 98 | + } |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + console.error(`${YELLOW}Warning: Cannot scan ${dirPath}${RESET}`); |
| 102 | + } |
| 103 | + |
| 104 | + return findings; |
| 105 | +} |
| 106 | + |
| 107 | +function main() { |
| 108 | + console.log('🔒 CodeSight Security Scanner'); |
| 109 | + console.log('==============================\n'); |
| 110 | + |
| 111 | + const rootDir = process.cwd(); |
| 112 | + const findings = scanDirectory(rootDir, rootDir); |
| 113 | + |
| 114 | + if (findings.length === 0) { |
| 115 | + console.log(`${GREEN}✅ No secrets detected!${RESET}\n`); |
| 116 | + process.exit(0); |
| 117 | + } |
| 118 | + |
| 119 | + console.log(`${RED}❌ POTENTIAL SECRETS DETECTED:${RESET}\n`); |
| 120 | + |
| 121 | + for (const finding of findings) { |
| 122 | + console.log(`${YELLOW}File:${RESET} ${finding.file}`); |
| 123 | + console.log(`${YELLOW}Type:${RESET} ${finding.type}`); |
| 124 | + console.log(`${YELLOW}Line:${RESET} ${finding.line}`); |
| 125 | + console.log(`${YELLOW}Match:${RESET} ${finding.match}`); |
| 126 | + console.log('---'); |
| 127 | + } |
| 128 | + |
| 129 | + console.log(`\n${RED}Total findings: ${findings.length}${RESET}`); |
| 130 | + console.log(`\n${YELLOW}Action Required:${RESET} Remove these secrets before committing!`); |
| 131 | + console.log(`${YELLOW}Tip:${RESET} Use .env files and add them to .gitignore\n`); |
| 132 | + |
| 133 | + process.exit(1); |
| 134 | +} |
| 135 | + |
| 136 | +main(); |
0 commit comments