SPB Git

Repository Public

secrets-scan.ts 1.2 KB 22 lines typescript

1import { execSync } from "node:child_process";2import { readFileSync } from "node:fs";34/** Fails if a tracked (or about-to-be-tracked) file looks like it contains a credential. */5const files = execSync("git ls-files --cached --others --exclude-standard", { encoding: "utf8" }).split("\n").filter(Boolean);6const patterns: [string, RegExp][] = [7  ["private key", /-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/],8  ["postgres URL with password", /postgres(?:ql)?:\/\/[^:\s/]+:(?!CHANGE_ME|password\b)[^@\s]{6,}@/],9  ["APP_SECRET value", /^APP_SECRET=[A-Za-z0-9+/=_-]{32,}\s*$/m],10  ["generic API key", /\b(?:sk|pk)-[A-Za-z0-9_-]{24,}\b/],11];12let bad = 0;13for (const f of files) {14  if (/\.(png|ico|jpg|webp|lock)$|pnpm-lock\.yaml$/.test(f) || f === ".env") continue;15  let text: string;16  try { text = readFileSync(f, "utf8"); } catch { continue; }17  for (const [name, re] of patterns) if (re.test(text)) { console.error(`SECRET? ${name} in ${f}`); bad++; }18}19if (files.includes(".env") && execSync("git ls-files .env", { encoding: "utf8" }).trim()) { console.error(".env is tracked by git"); bad++; }20if (bad) process.exit(1);21console.log(`secrets scan ok (${files.length} files)`);22
Files main
Menu

Theme