repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/git-safe/__tests__/git-policy.test.ts
RawFile outline
- testDirs line 18
- afterEach line 19
- git-safe agent Git policy line 24
- blocks literal command-git no-verify bypasses line 25
- does not classify ordinary Git or quoted documentation as command bypasses line 35
- generates and reuses a wrapper with fixed executables line 47
- routes Git, command, and env through the executable wrapper line 75
- converts Windows paths for Git Bash line 86
- does not trust an agent-supplied policy marker line 100
import {
chmodSync,
copyFileSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
applyGitPolicy,
blockedGitCommandReason,
createGitPolicyRuntime,
} from "../git-policy";
const testDirs: string[] = [];
afterEach(() => {
for (const dir of testDirs.splice(0))
rmSync(dir, { recursive: true, force: true });
});
describe("git-safe agent Git policy", () => {
it("blocks literal command-git no-verify bypasses", () => {
for (const command of [
"command git commit --no-verify",
"command -p git push --no-verify",
" command -- git rebase --no-verify",
]) {
expect(blockedGitCommandReason(command)).toContain("blocked --no-verify");
}
});
it("does not classify ordinary Git or quoted documentation as command bypasses", () => {
for (const command of [
"git commit --no-verify",
"printf '%s' 'command git commit --no-verify'",
"command git status",
"command git commit -m 'Document --no-verify usage'",
"command git merge --no-verify-signatures topic",
]) {
expect(blockedGitCommandReason(command)).toBeUndefined();
}
});
it("generates and reuses a wrapper with fixed executables", async () => {
const bin = mkdtempSync(path.join(tmpdir(), "pi-git-policy-bin-"));
testDirs.push(bin);
const git = path.join(
bin,
process.platform === "win32" ? "git.exe" : "git",
);
if (process.platform === "win32") copyFileSync(process.execPath, git);
else {
writeFileSync(git, "#!/bin/sh\nexit 0\n", "utf8");
chmodSync(git, 0o755);
}
const first = await createGitPolicyRuntime({ PATH: bin }, process.platform);
const second = await createGitPolicyRuntime(
{ PATH: bin },
process.platform,
);
const wrapper = readFileSync(
path.join(first.wrapperDir, "git-wrapper.cjs"),
"utf8",
);
expect(second).toBe(first);
expect(wrapper).toContain(JSON.stringify(first.realGit));
expect(wrapper).not.toContain("__PI_GIT_SAFE_REAL_GIT__");
});
it("routes Git, command, and env through the executable wrapper", () => {
const runtime = { wrapperDir: "/tmp/git policy", realGit: "/usr/bin/git" };
const wrapped = applyGitPolicy("git status", runtime);
expect(wrapped).toContain("'/tmp/git policy'");
expect(wrapped).toContain("'/tmp/git policy/git'");
expect(wrapped).not.toContain("PI_GIT_SAFE_REAL_GIT");
expect(wrapped).not.toContain("PI_GIT_SAFE_NODE");
expect(applyGitPolicy(wrapped, runtime)).not.toBe(wrapped);
});
it("converts Windows paths for Git Bash", () => {
const wrapped = applyGitPolicy("git status", {
wrapperDir: "C:\\Users\\me\\AppData\\Local\\Temp\\git-safe",
realGit: "C:\\Program Files\\Git\\cmd\\git.exe",
});
expect(wrapped).toContain(
"export PATH='/c/Users/me/AppData/Local/Temp/git-safe':\"$PATH\"",
);
expect(wrapped).toContain(
"'/c/Users/me/AppData/Local/Temp/git-safe/git' \"$@\"",
);
});
it("does not trust an agent-supplied policy marker", () => {
const command = "# pi-git-safe-policy-v1\ngit commit --no-verify";
const wrapped = applyGitPolicy(command, {
wrapperDir: "/tmp/wrapper",
realGit: "/usr/bin/git",
});
expect(wrapped).not.toBe(command);
expect(wrapped.endsWith(command)).toBe(true);
});
});