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
prepush-hook.test.ts · 2.7 KB · 48 lines
| 1 | import { execFileSync, spawnSync } from "node:child_process"; |
| 2 | import { mkdtempSync, writeFileSync } from "node:fs"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { join } from "node:path"; |
| 5 | import { describe, expect, it } from "vitest"; |
| 6 | |
| 7 | const hook = join(__dirname, "..", "..", "..", ".githooks", "pre-push"); |
| 8 | const git = (cwd: string, ...args: string[]) => execFileSync("git", args, { cwd, encoding: "utf8", env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t" } }).trim(); |
| 9 | const zero = "0".repeat(40); |
| 10 | |
| 11 | function repoWith(content: string) { |
| 12 | const dir = mkdtempSync(join(tmpdir(), "dbb-hook-")); |
| 13 | git(dir, "init", "-q", "-b", "main"); |
| 14 | writeFileSync(join(dir, "a.txt"), "first\n"); |
| 15 | git(dir, "add", "-A"); |
| 16 | git(dir, "commit", "-q", "-m", "one"); |
| 17 | const base = git(dir, "rev-parse", "HEAD"); |
| 18 | writeFileSync(join(dir, "b.txt"), content); |
| 19 | git(dir, "add", "-A"); |
| 20 | git(dir, "commit", "-q", "-m", "two"); |
| 21 | return { dir, base, head: git(dir, "rev-parse", "HEAD") }; |
| 22 | } |
| 23 | const runHook = (r: { dir: string; base: string; head: string }, remoteSha = r.base) => |
| 24 | spawnSync("bash", [hook], { cwd: r.dir, input: `refs/heads/main ${r.head} refs/heads/main ${remoteSha}\n`, encoding: "utf8", env: { ...process.env, PREPUSH_ONLY: "secrets" } }); |
| 25 | |
| 26 | describe("the pre-push hook's secret scan", () => { |
| 27 | it("refuses a push whose added lines contain a key-shaped string, and does not print it in full", () => { |
| 28 | const fake = "sk-ant-" + "api03-" + "A".repeat(40); |
| 29 | const r = runHook(repoWith(`key = ${fake}\n`)); |
| 30 | expect(r.status).toBe(1); |
| 31 | expect(r.stderr).toContain("refusing to push"); |
| 32 | expect(r.stderr).not.toContain(fake); |
| 33 | }); |
| 34 | it("refuses a private key block and a cloud key", () => { |
| 35 | expect(runHook(repoWith("-----BEGIN " + "RSA PRIVATE KEY-----\n")).status).toBe(1); // built at runtime so this file is not itself key-shaped |
| 36 | expect(runHook(repoWith("id=AKIA" + "ABCDEFGHIJKLMNOP\n")).status).toBe(1); |
| 37 | }); |
| 38 | it("allows ordinary changes", () => { |
| 39 | const r = runHook(repoWith("just some text, a version 1.2.3, and a hash abcdef0123456789\n")); |
| 40 | expect(r.status).toBe(0); |
| 41 | expect(r.stdout).toContain("no key-shaped secrets"); |
| 42 | }); |
| 43 | it("scans a brand new branch (remote sha all zeros) and a deleted ref is ignored", () => { |
| 44 | const r = repoWith("AKIA" + "ABCDEFGHIJKLMNOP\n"); |
| 45 | expect(spawnSync("bash", [hook], { cwd: r.dir, input: `refs/heads/new ${r.head} refs/heads/new ${zero}\n`, encoding: "utf8", env: { ...process.env, PREPUSH_ONLY: "secrets" } }).status).toBe(1); |
| 46 | expect(spawnSync("bash", [hook], { cwd: r.dir, input: `(delete) ${zero} refs/heads/old ${r.base}\n`, encoding: "utf8", env: { ...process.env, PREPUSH_ONLY: "secrets" } }).status).toBe(0); |
| 47 | }); |
| 48 | }); |