1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
import { describe, expect, it, beforeAll } from "bun:test";
import { mkdtempSync, writeFileSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
import { installPackInspectionHook } from "../lib/push-policy";
const FIXTURE_SECRET = "AKIAABCDEFGH1234IJKL";
function git(cwd: string, args: string[], allowFail = false) {
const proc = Bun.spawnSync(["git", ...args], {
cwd,
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
});
const exitCode = proc.exitCode;
const stderr = new TextDecoder().decode(proc.stderr);
if (!allowFail && exitCode !== 0) {
throw new Error(`git ${args.join(" ")} failed (${exitCode}):\n${stderr}`);
}
return { exitCode, stderr, stdout: new TextDecoder().decode(proc.stdout) };
}
const GIT_OK = (() => {
try {
return Bun.spawnSync(["git", "--version"]).exitCode === 0;
} catch {
return false;
}
})();
describe.skipIf(!GIT_OK)("pre-receive scan only inspects pushed changes", () => {
let bare: string;
let work: string;
beforeAll(async () => {
const root = mkdtempSync(join(tmpdir(), "gc-newbranch-"));
bare = join(root, "bare.git");
work = join(root, "work");
git(root, ["init", "--bare", "-b", "main", bare]);
git(root, ["init", "-b", "main", work]);
git(work, ["config", "user.email", "t@example.com"]);
git(work, ["config", "user.name", "Test"]);
git(work, ["config", "commit.gpgsign", "false"]);
writeFileSync(join(work, "fixture.ts"), `const K = '${FIXTURE_SECRET}';\n`);
git(work, ["add", "."]);
git(work, ["commit", "-m", "seed fixture (has a secret pattern)"]);
git(work, ["remote", "add", "origin", bare]);
git(work, ["push", "-u", "origin", "main"]);
const hook = await installPackInspectionHook([], { secretScan: true });
if (!hook) throw new Error("hook install returned null");
git(bare, ["config", "core.hooksPath", hook.env.GIT_CONFIG_VALUE_0]);
});
it("accepts a new branch that only adds a clean file (does NOT rescan the pre-existing fixture)", () => {
git(work, ["checkout", "-b", "clean-feature", "main"]);
writeFileSync(join(work, "clean.ts"), "export const ok = 1;\n");
git(work, ["add", "clean.ts"]);
git(work, ["commit", "-m", "add a clean file"]);
const res = git(work, ["push", "origin", "clean-feature"], true);
expect(res.stderr).not.toContain("Secret scan");
expect(res.exitCode).toBe(0);
});
it("still blocks a new branch that itself introduces a real secret", () => {
git(work, ["checkout", "-b", "leaky-feature", "main"]);
writeFileSync(join(work, "leak.ts"), `const K = '${FIXTURE_SECRET}';\n`);
git(work, ["add", "leak.ts"]);
git(work, ["commit", "-m", "oops committed a secret"]);
const res = git(work, ["push", "origin", "leaky-feature"], true);
expect(res.exitCode).not.toBe(0);
expect(res.stderr).toContain("Secret scan");
expect(res.stderr).toContain("leak.ts");
expect(res.stderr).not.toContain("fixture.ts");
});
});
|