repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/verboten/__tests__/rules.test.ts
RawFile outline
- roots line 20
- afterEach line 21
- tempRoot line 26
- linuxRules line 32
- matches line 41
- Verboten built-in rules line 45
- protects dotenv variants but keeps exact template files available line 46
- protects every conservative static basename line 63
- protects resolved Linux application credentials and aliases line 82
- honors environment-selected credential paths line 105
- allows explicit SSH public metadata and protects all other SSH descendants line 132
- builds active Windows and macOS conventions without running on them line 150
- Verboten settings line 184
- merges global and trusted project patterns and skips invalid entries line 185
- does not read or expose untrusted project settings line 223
- Verboten path resolution line 250
- tests raw and normalized absolute candidates line 251
- protects existing and nonexistent targets through a real directory alias line 259
- is separator, casing, quoting, boundary, and composition invariant line 288
import {
mkdirSync,
mkdtempSync,
rmSync,
symlinkSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import fc from "fast-check";
import { afterEach, describe, expect, it } from "vitest";
import {
buildBuiltinRules,
lexicalCandidates,
loadVerbotenSettings,
matchingRules,
matchPath,
} from "../src/rules";
const roots: string[] = [];
afterEach(() => {
for (const root of roots.splice(0))
rmSync(root, { recursive: true, force: true });
});
function tempRoot(name: string): string {
const root = mkdtempSync(path.join(tmpdir(), `${name}-`));
roots.push(root);
return root;
}
function linuxRules(overrides: NodeJS.ProcessEnv = {}) {
return buildBuiltinRules({
platform: "linux",
env: { HOME: "/home/test", ...overrides },
home: "/home/test",
agentDir: "/home/test/.pi/agent",
});
}
function matches(value: string, rules = linuxRules()): boolean {
return matchingRules(value, rules).length > 0;
}
describe("Verboten built-in rules", () => {
it("protects dotenv variants but keeps exact template files available", () => {
for (const value of [
".env",
"./.env.local",
"C:\\repo\\.ENV.PRODUCTION",
"cat '.env.test'",
])
expect(matches(value), value).toBe(true);
for (const value of [
".env.example",
".env.sample",
".env.template",
"environment.ts",
])
expect(matches(value), value).toBe(false);
});
it("protects every conservative static basename", () => {
for (const value of [
"keys.p12",
"KEYS.PFX",
"~/.npmrc",
"/home/me/.pypirc",
"cat .netrc",
])
expect(matches(value), value).toBe(true);
for (const value of [
"keys.pem",
"keys.key",
"config.json",
"settings.xml",
"gradle.properties",
])
expect(matches(value), value).toBe(false);
});
it("protects resolved Linux application credentials and aliases", () => {
for (const value of [
"/home/test/.pi/agent/auth.json",
"cat ~/.claude/.credentials.json",
"$CODEX_HOME/auth.json",
"$HOME/.docker/config.json",
"~/.config/containers/auth.json",
"~/.config/gh/hosts.yml",
"~/.git-credentials",
"~/.config/git/credentials",
"~/.subversion/auth/svn.simple/abc",
"~/.cargo/credentials.toml",
"~/.aws/credentials",
"~/.config/gcloud/credentials.db",
"~/.config/gcloud/legacy_credentials/me/adc.json",
"~/.kube/config",
"~/.local/state/syncthing/config.xml",
"~/.config/aerc/accounts.conf",
"~/.gnupg/private-keys-v1.d/key.key",
])
expect(matches(value), value).toBe(true);
});
it("honors environment-selected credential paths", () => {
const rules = linuxRules({
REGISTRY_AUTH_FILE: "/run/user/auth.json",
XDG_RUNTIME_DIR: "/run/user/1000",
DOCKER_CONFIG: "/opt/docker",
GH_CONFIG_DIR: "/opt/gh",
CARGO_HOME: "/opt/cargo",
AWS_SHARED_CREDENTIALS_FILE: "/opt/aws-creds",
GOOGLE_APPLICATION_CREDENTIALS: "/opt/google.json",
CLOUDSDK_CONFIG: "/opt/gcloud",
KUBECONFIG: "/opt/kube-a:/opt/kube-b",
});
for (const value of [
"/run/user/auth.json",
"$XDG_RUNTIME_DIR/containers/auth.json",
"$DOCKER_CONFIG/config.json",
"$GH_CONFIG_DIR/hosts.yml",
"$CARGO_HOME/credentials",
"$AWS_SHARED_CREDENTIALS_FILE",
"$GOOGLE_APPLICATION_CREDENTIALS",
"$CLOUDSDK_CONFIG/access_tokens.db",
"/opt/kube-a",
"/opt/kube-b",
])
expect(matches(value, rules), value).toBe(true);
});
it("allows explicit SSH public metadata and protects all other SSH descendants", () => {
for (const value of [
"~/.ssh",
"~/.ssh/id_ed25519",
"~/.ssh/private/custom",
"cat $HOME/.ssh/id_rsa",
])
expect(matches(value), value).toBe(true);
for (const value of [
"~/.ssh/id_ed25519.pub",
"~/.ssh/authorized_keys",
"~/.ssh/authorized_keys2",
"~/.ssh/known_hosts.old",
"~/.ssh/config",
])
expect(matches(value), value).toBe(false);
});
it("builds active Windows and macOS conventions without running on them", () => {
const windows = buildBuiltinRules({
platform: "win32",
env: {
HOME: "C:\\Users\\test",
USERPROFILE: "C:\\Users\\test",
APPDATA: "C:\\Users\\test\\AppData\\Roaming",
LOCALAPPDATA: "C:\\Users\\test\\AppData\\Local",
},
home: "C:\\Users\\test",
agentDir: "C:\\Users\\test\\.pi\\agent",
});
for (const value of [
"C:\\Users\\test\\AppData\\Roaming\\GitHub CLI\\hosts.yml",
"%APPDATA%\\Subversion\\auth\\x",
"C:\\Users\\test\\AppData\\Local\\Syncthing\\config.xml",
])
expect(matches(value, windows), value).toBe(true);
const mac = buildBuiltinRules({
platform: "darwin",
env: { HOME: "/Users/test" },
home: "/Users/test",
agentDir: "/Users/test/.pi/agent",
});
expect(
matches(
"/Users/test/Library/Application Support/Syncthing/config.xml",
mac,
),
).toBe(true);
});
});
describe("Verboten settings", () => {
it("merges global and trusted project patterns and skips invalid entries", () => {
const root = tempRoot("verboten-settings");
const agentDir = path.join(root, "agent");
const projectDir = path.join(root, "project", ".pi");
mkdirSync(agentDir, { recursive: true });
mkdirSync(projectDir, { recursive: true });
writeFileSync(
path.join(agentDir, "settings.json"),
JSON.stringify({
verboten: {
patterns: ["global-secret", "[broken"],
judgeModel: "provider/global",
},
}),
);
writeFileSync(
path.join(projectDir, "settings.json"),
JSON.stringify({
verboten: {
patterns: ["project-secret", 7],
judgeModel: "provider/project",
},
}),
);
const loaded = loadVerbotenSettings(path.dirname(projectDir), true, {
platform: "linux",
env: { HOME: root },
home: root,
agentDir,
});
expect(matches("global-secret", loaded.rules)).toBe(true);
expect(matches("project-secret", loaded.rules)).toBe(true);
expect(loaded.judgeModel).toBe("provider/project");
expect(loaded.warnings).toHaveLength(2);
expect(loaded.rules.some((rule) => rule.pattern === "[broken")).toBe(false);
});
it("does not read or expose untrusted project settings", () => {
const root = tempRoot("verboten-untrusted");
const agentDir = path.join(root, "agent");
const project = path.join(root, "project");
mkdirSync(agentDir, { recursive: true });
mkdirSync(path.join(project, ".pi"), { recursive: true });
writeFileSync(
path.join(project, ".pi", "settings.json"),
JSON.stringify({
verboten: {
patterns: ["project-secret"],
judgeModel: "provider/project",
},
}),
);
const loaded = loadVerbotenSettings(project, false, {
platform: "linux",
env: { HOME: root },
home: root,
agentDir,
});
expect(matches("project-secret", loaded.rules)).toBe(false);
expect(loaded.judgeModel).toBeNull();
expect(loaded.projectIgnored).toBe(true);
});
});
describe("Verboten path resolution", () => {
it("tests raw and normalized absolute candidates", () => {
const candidates = lexicalCandidates("@secrets/.env", "/workspace");
expect(candidates).toEqual([
"secrets/.env",
path.resolve("/workspace", "secrets/.env").replaceAll("\\", "/"),
]);
});
it("protects existing and nonexistent targets through a real directory alias", async () => {
const root = tempRoot("verboten-alias");
const protectedDir = path.join(root, "protected");
const alias = path.join(root, "alias");
mkdirSync(protectedDir);
writeFileSync(path.join(protectedDir, "secret.txt"), "fixture");
symlinkSync(
protectedDir,
alias,
process.platform === "win32" ? "junction" : "dir",
);
const escaped = protectedDir
.replaceAll("\\", "/")
.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
.replaceAll("/", String.raw`[\\/]`);
const rule = {
group: "global setting" as const,
label: "fixture",
pattern: escaped,
regex: new RegExp(escaped, "iu"),
};
expect(
(await matchPath(path.join(alias, "secret.txt"), root, [rule])).matches,
).toHaveLength(1);
expect(
(await matchPath(path.join(alias, "future.txt"), root, [rule])).matches,
).toHaveLength(1);
});
it("is separator, casing, quoting, boundary, and composition invariant", () => {
const rules = buildBuiltinRules({
platform: "win32",
env: { HOME: "C:\\Users\\Alice", USERPROFILE: "C:\\Users\\Alice" },
home: "C:\\Users\\Alice",
agentDir: "C:\\Users\\Alice\\.pi\\agent",
});
fc.assert(
fc.property(
fc.constantFrom("/", "\\"),
fc.constantFrom("", "'", '"'),
fc.constantFrom("cat ", "Get-Content ", "node read "),
fc.boolean(),
(separator, quote, prefix, upper) => {
const segments = [
"C:",
"Users",
"Alice",
".pi",
"agent",
"auth.json",
];
let target = segments.join(separator);
if (upper) target = target.toUpperCase();
expect(
matchingRules(`${prefix}${quote}${target}${quote}`, rules),
).not.toHaveLength(0);
expect(matchingRules(`${target}.backup`, rules)).toHaveLength(0);
},
),
{ numRuns: 100 },
);
});
});