|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Pre-commit hook: Scan staged files for secrets. |
| 5 | + * Catches API keys, tokens, passwords, and connection strings |
| 6 | + * that .gitignore might miss (e.g., hardcoded in source files). |
| 7 | + */ |
| 8 | + |
| 9 | +const { execSync } = require('child_process'); |
| 10 | +const fs = require('fs'); |
| 11 | +const path = require('path'); |
| 12 | + |
| 13 | +const CONFIG = { |
| 14 | + patterns: [ |
| 15 | + // Generic secrets |
| 16 | + { regex: /(?:api[_-]?key|apikey)\s*[:=]\s*['"][A-Za-z0-9_\-]{20,}['"]/gi, label: 'API key' }, |
| 17 | + { regex: /(?:secret|token)\s*[:=]\s*['"][A-Za-z0-9_\-]{20,}['"]/gi, label: 'Secret/Token' }, |
| 18 | + { regex: /(?:password|passwd|pwd)\s*[:=]\s*['"][^'"]{8,}['"]/gi, label: 'Password' }, |
| 19 | + |
| 20 | + // Supabase |
| 21 | + { regex: /eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\.[A-Za-z0-9_-]+/g, label: 'Supabase JWT' }, |
| 22 | + { regex: /sbp_[a-f0-9]{40}/g, label: 'Supabase service key' }, |
| 23 | + |
| 24 | + // PostHog |
| 25 | + { regex: /phc_[A-Za-z0-9]{30,}/g, label: 'PostHog API key' }, |
| 26 | + |
| 27 | + // Google / Gemini |
| 28 | + { regex: /AIza[A-Za-z0-9_\\-]{35}/g, label: 'Google API key' }, |
| 29 | + |
| 30 | + // Neon DB |
| 31 | + { regex: /postgresql:\/\/[^:]+:[^@]+@[^/]+/g, label: 'Database connection string' }, |
| 32 | + |
| 33 | + // Private keys |
| 34 | + { regex: /-----BEGIN (?:RSA |EC |DSA )?PRIVATE KEY-----/g, label: 'Private key' }, |
| 35 | + |
| 36 | + // Generic high-entropy strings (base64, 40+ chars assigned to suspect vars) |
| 37 | + { regex: /(?:SUPABASE|POSTHOG|NEON|GEMINI|GOOGLE)[_A-Z]*\s*=\s*['"]?[A-Za-z0-9+/=_\-]{30,}['"]?/g, label: 'Environment variable with secret' }, |
| 38 | + ], |
| 39 | + allowlistPaths: [ |
| 40 | + '.env.example', |
| 41 | + '.env.local.example', |
| 42 | + 'scripts/lib/check-secrets.js', // This file contains regex patterns, not real secrets |
| 43 | + ], |
| 44 | +}; |
| 45 | + |
| 46 | +function getStagedFiles() { |
| 47 | + try { |
| 48 | + const output = execSync('git diff --cached --name-only --diff-filter=ACMR', { |
| 49 | + encoding: 'utf8', |
| 50 | + }); |
| 51 | + return output.trim().split('\n').filter(Boolean); |
| 52 | + } catch { |
| 53 | + return []; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function checkFile(filePath) { |
| 58 | + const violations = []; |
| 59 | + |
| 60 | + if (CONFIG.allowlistPaths.some((allowed) => filePath.endsWith(allowed))) { |
| 61 | + return violations; |
| 62 | + } |
| 63 | + |
| 64 | + // Skip binary files |
| 65 | + const ext = path.extname(filePath).toLowerCase(); |
| 66 | + if (['.png', '.jpg', '.jpeg', '.gif', '.ico', '.woff', '.woff2', '.ttf', '.eot'].includes(ext)) { |
| 67 | + return violations; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 72 | + const lines = content.split('\n'); |
| 73 | + |
| 74 | + for (let i = 0; i < lines.length; i++) { |
| 75 | + const line = lines[i]; |
| 76 | + for (const pattern of CONFIG.patterns) { |
| 77 | + if (pattern.regex.test(line)) { |
| 78 | + violations.push({ |
| 79 | + file: filePath, |
| 80 | + line: i + 1, |
| 81 | + label: pattern.label, |
| 82 | + snippet: line.trim().substring(0, 80), |
| 83 | + }); |
| 84 | + // Reset regex lastIndex for global patterns |
| 85 | + pattern.regex.lastIndex = 0; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } catch { |
| 90 | + // Skip unreadable files |
| 91 | + } |
| 92 | + |
| 93 | + return violations; |
| 94 | +} |
| 95 | + |
| 96 | +// Main |
| 97 | +const files = getStagedFiles(); |
| 98 | +const allViolations = []; |
| 99 | + |
| 100 | +for (const file of files) { |
| 101 | + allViolations.push(...checkFile(file)); |
| 102 | +} |
| 103 | + |
| 104 | +if (allViolations.length > 0) { |
| 105 | + console.error('\n🔐 SECRET SCANNING FAILED\n'); |
| 106 | + console.error('The following staged files appear to contain secrets:\n'); |
| 107 | + for (const v of allViolations) { |
| 108 | + console.error(` ${v.file}:${v.line} — ${v.label}`); |
| 109 | + console.error(` ${v.snippet}\n`); |
| 110 | + } |
| 111 | + console.error('Remove the secrets and use environment variables instead.'); |
| 112 | + console.error('If this is a false positive, add the file to CONFIG.allowlistPaths in scripts/lib/check-secrets.js\n'); |
| 113 | + process.exit(1); |
| 114 | +} |
| 115 | + |
| 116 | +console.log('✓ Secret scan passed'); |
0 commit comments