|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execFileSync } from 'node:child_process'; |
| 3 | +import { existsSync, readFileSync, statSync } from 'node:fs'; |
| 4 | +import { relative, resolve, sep } from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | + |
| 7 | +const root = resolve(fileURLToPath(new URL('..', import.meta.url))); |
| 8 | +const args = process.argv.slice(2); |
| 9 | + |
| 10 | +const RULES = [ |
| 11 | + { |
| 12 | + id: 'openai-api-key', |
| 13 | + regex: /sk-[A-Za-z0-9_-]{20,}/g, |
| 14 | + }, |
| 15 | + { |
| 16 | + id: 'literal-credential-assignment', |
| 17 | + regex: /\b(?:secret|token|password)\b\s*[:=]\s*["'][A-Za-z0-9_./+=-]{16,}["']/gi, |
| 18 | + }, |
| 19 | + { |
| 20 | + id: 'private-key-block', |
| 21 | + regex: /-----BEGIN [A-Z ]*PRIVATE KEY-----/g, |
| 22 | + }, |
| 23 | + { |
| 24 | + id: 'credentialed-email-example', |
| 25 | + regex: /[A-Za-z0-9._%+-]+@(?!example\.(?:com|org|net)\b)[A-Za-z0-9.-]+\.[A-Za-z]{2,}["']?\s*,\s*["']?password["']?\s*:/gi, |
| 26 | + }, |
| 27 | +]; |
| 28 | + |
| 29 | +const IGNORED_PATHS = new Set([ |
| 30 | + 'scripts/secret-scan.mjs', |
| 31 | + 'test/secret-scan.test.js', |
| 32 | +]); |
| 33 | + |
| 34 | +const IGNORED_PREFIXES = [ |
| 35 | + 'test/', |
| 36 | + 'test/_research/', |
| 37 | +]; |
| 38 | + |
| 39 | +const IGNORED_EXTENSIONS = new Set([ |
| 40 | + '.png', '.jpg', '.jpeg', '.gif', '.webp', '.ico', '.zip', '.db', |
| 41 | +]); |
| 42 | + |
| 43 | +function toRepoPath(file) { |
| 44 | + return relative(root, resolve(root, file)).split(sep).join('/'); |
| 45 | +} |
| 46 | + |
| 47 | +function isIgnored(file) { |
| 48 | + const repoPath = toRepoPath(file); |
| 49 | + if (!repoPath || repoPath.startsWith('..') || repoPath.includes('\0')) return true; |
| 50 | + if (IGNORED_PATHS.has(repoPath)) return true; |
| 51 | + if (IGNORED_PREFIXES.some(prefix => repoPath.startsWith(prefix))) return true; |
| 52 | + const lower = repoPath.toLowerCase(); |
| 53 | + return [...IGNORED_EXTENSIONS].some(ext => lower.endsWith(ext)); |
| 54 | +} |
| 55 | + |
| 56 | +function trackedFiles() { |
| 57 | + const output = execFileSync('git', ['ls-files', '-z'], { |
| 58 | + cwd: root, |
| 59 | + encoding: 'utf8', |
| 60 | + maxBuffer: 16 * 1024 * 1024, |
| 61 | + }); |
| 62 | + return output.split('\0').filter(Boolean); |
| 63 | +} |
| 64 | + |
| 65 | +function inputFiles() { |
| 66 | + if (args.length) return args; |
| 67 | + return trackedFiles(); |
| 68 | +} |
| 69 | + |
| 70 | +function lineForOffset(text, offset) { |
| 71 | + let line = 1; |
| 72 | + for (let i = 0; i < offset; i += 1) { |
| 73 | + if (text.charCodeAt(i) === 10) line += 1; |
| 74 | + } |
| 75 | + return line; |
| 76 | +} |
| 77 | + |
| 78 | +function scanFile(file) { |
| 79 | + if (isIgnored(file)) return []; |
| 80 | + const abs = resolve(root, file); |
| 81 | + if (!existsSync(abs) || !statSync(abs).isFile()) return []; |
| 82 | + const text = readFileSync(abs, 'utf8'); |
| 83 | + const findings = []; |
| 84 | + for (const rule of RULES) { |
| 85 | + rule.regex.lastIndex = 0; |
| 86 | + for (const match of text.matchAll(rule.regex)) { |
| 87 | + findings.push({ |
| 88 | + path: toRepoPath(file), |
| 89 | + line: lineForOffset(text, match.index || 0), |
| 90 | + rule: rule.id, |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + return findings; |
| 95 | +} |
| 96 | + |
| 97 | +const findings = inputFiles().flatMap(scanFile) |
| 98 | + .sort((a, b) => a.path.localeCompare(b.path) || a.line - b.line || a.rule.localeCompare(b.rule)); |
| 99 | + |
| 100 | +for (const finding of findings) { |
| 101 | + console.log(`${finding.path}:${finding.line} ${finding.rule}`); |
| 102 | +} |
| 103 | + |
| 104 | +if (findings.length) process.exitCode = 1; |
0 commit comments