A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

dbbasic-browser/apps/browser/test/prepush-hook.test.ts
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
prepush-hook.test.ts · 2.7 KB · 48 lines
1import { execFileSync, spawnSync } from "node:child_process";
2import { mkdtempSync, writeFileSync } from "node:fs";
3import { tmpdir } from "node:os";
4import { join } from "node:path";
5import { describe, expect, it } from "vitest";
6
7const hook = join(__dirname, "..", "..", "..", ".githooks", "pre-push");
8const 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();
9const zero = "0".repeat(40);
10
11function 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}
23const 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
26describe("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});