askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
secrets.test.ts · 2.3 KB · 56 lines
| 1 | /** |
| 2 | * Guard against committing credentials. Scans every file git would commit (tracked + untracked, |
| 3 | * minus ignored) for API-key and private-key shapes, and checks that no env file is tracked. |
| 4 | * |
| 5 | * Deliberately NOT flagged: the well-known public Anvil/Hardhat test key in main.ts (documented |
| 6 | * as "never fund this one") and on-chain transaction hashes, which look like keys but are public. |
| 7 | */ |
| 8 | import { execFileSync } from "node:child_process"; |
| 9 | import { existsSync, readFileSync, statSync } from "node:fs"; |
| 10 | import { dirname, join } from "node:path"; |
| 11 | import { fileURLToPath } from "node:url"; |
| 12 | import { describe, expect, it } from "vitest"; |
| 13 | |
| 14 | const root = join(dirname(fileURLToPath(import.meta.url)), "..", "..", ".."); |
| 15 | const inGit = existsSync(join(root, ".git")); |
| 16 | |
| 17 | const PATTERNS: Array<[string, RegExp]> = [ |
| 18 | ["Anthropic key", /sk-ant-[A-Za-z0-9_-]{20,}/], |
| 19 | ["OpenAI key", /sk-(?:proj-|svcacct-)?[A-Za-z0-9_-]{32,}/], |
| 20 | ["AWS access key", /AKIA[0-9A-Z]{16}/], |
| 21 | ["GitHub token", /gh[pousr]_[A-Za-z0-9]{30,}/], |
| 22 | ["private key block", /-----BEGIN [A-Z ]*PRIVATE KEY-----/], |
| 23 | ]; |
| 24 | |
| 25 | function candidateFiles(): string[] { |
| 26 | const out = execFileSync("git", ["ls-files", "-co", "--exclude-standard", "-z"], { cwd: root, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }); |
| 27 | return out.split("\0").filter(Boolean); |
| 28 | } |
| 29 | |
| 30 | describe.skipIf(!inGit)("no credentials in the repository", () => { |
| 31 | const files = candidateFiles(); |
| 32 | |
| 33 | it("no env file (other than .env.example) is tracked or about to be committed", () => { |
| 34 | const bad = files.filter((f) => /(^|\/)\.env($|\.)/.test(f) && !f.endsWith(".env.example")); |
| 35 | expect(bad).toEqual([]); |
| 36 | }); |
| 37 | |
| 38 | it("no API-key or private-key shapes appear in any committable file", () => { |
| 39 | const hits: string[] = []; |
| 40 | for (const f of files) { |
| 41 | const p = join(root, f); |
| 42 | try { |
| 43 | if (!statSync(p).isFile() || statSync(p).size > 2_000_000) continue; |
| 44 | const text = readFileSync(p, "utf8"); |
| 45 | if (text.includes("\0")) continue; // binary |
| 46 | for (const [label, re] of PATTERNS) { |
| 47 | if (re.test(text)) hits.push(`${f}: ${label}`); |
| 48 | } |
| 49 | } catch { |
| 50 | /* unreadable: skip */ |
| 51 | } |
| 52 | } |
| 53 | // No exceptions: tests that need fake keys build them at runtime so none exists in any source file. |
| 54 | expect(hits).toEqual([]); |
| 55 | }); |
| 56 | }); |