mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-10-26 10:45:22 +00:00
feat(permission): integrate auto-review and hard guards
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { chmod, mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises";
|
||||
import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -84,12 +84,32 @@ test("all package extensions load together without global registration conflicts
|
||||
packageJson.pi.extensions.indexOf("./pi-tool-search/extensions/index.ts"),
|
||||
"pi-chrome must register before Tool Search catalogs its authorized tools",
|
||||
);
|
||||
const permissionConfig = JSON.parse(
|
||||
await readFile(join(repositoryRoot, "config", "pi-permission-system.json"), "utf8"),
|
||||
) as { authorizerChain: string[]; permission: Record<string, unknown> };
|
||||
const permissionPolicySource = await readFile(
|
||||
join(repositoryRoot, "config", "pi-permission-policy.jsonc"),
|
||||
"utf8",
|
||||
);
|
||||
const hardGuardSource = await readFile(
|
||||
join(repositoryRoot, "config", "pi-permission-hard-guards.jsonc"),
|
||||
"utf8",
|
||||
);
|
||||
const stripLineComments = (source: string): string => source.replace(/^\s*\/\/.*$/gmu, "");
|
||||
const permissionConfig = JSON.parse(stripLineComments(permissionPolicySource)) as {
|
||||
authorizerChain: string[];
|
||||
permission: Record<string, unknown>;
|
||||
};
|
||||
const hardGuardConfig = JSON.parse(stripLineComments(hardGuardSource)) as {
|
||||
permission: { path?: Record<string, string>; bash: Record<string, string> };
|
||||
};
|
||||
assert.equal(permissionConfig.permission.ssh_connect, "ask", "SSH connection must enter the permission gate");
|
||||
assert.equal(permissionConfig.permission.ssh_cd, "ask", "remote workspace changes must enter the permission gate");
|
||||
assert.deepEqual(permissionConfig.authorizerChain, ["auto-review"], "SSH connection asks must reach AutoReview");
|
||||
assert.equal(hardGuardConfig.permission.path, undefined, "sensitive paths must not be hard-denied by default");
|
||||
assert.equal(hardGuardConfig.permission.bash["rm -rf /"], "deny");
|
||||
assert.equal(
|
||||
packageJson.pi.extensions.some((entry) => entry.includes("pi-permission-auto-review")),
|
||||
false,
|
||||
"AutoReview must be integrated instead of loaded as a second extension",
|
||||
);
|
||||
assert.ok(
|
||||
packageJson.pi.extensions.indexOf("./extensions/permission-system.ts") <
|
||||
packageJson.pi.extensions.indexOf("./pi-ssh/index.ts"),
|
||||
@@ -142,27 +162,55 @@ test("all package extensions load together without global registration conflicts
|
||||
for (const entry of packageJson.pi.extensions) args.push("-e", resolve(repositoryRoot, entry));
|
||||
args.push("--mode", "rpc");
|
||||
|
||||
const result = spawnSync("pi", args, {
|
||||
const loadEnvironment = {
|
||||
...process.env,
|
||||
HOME: home,
|
||||
XDG_CONFIG_HOME: join(home, ".config"),
|
||||
PI_CODING_AGENT_DIR: join(home, ".pi-agent"),
|
||||
MY_PI_SEARCH_CONFIG: join(home, "missing-search.env"),
|
||||
PATH: `${mockBin}:${process.env.PATH ?? ""}`,
|
||||
PI_OFFLINE: "1",
|
||||
TAVILY_API_KEY: "",
|
||||
EXA_API_KEY: "",
|
||||
KEENABLE_API_KEY: "",
|
||||
};
|
||||
const runBundle = () => spawnSync("pi", args, {
|
||||
encoding: "utf8",
|
||||
timeout: 60_000,
|
||||
env: {
|
||||
...process.env,
|
||||
HOME: home,
|
||||
XDG_CONFIG_HOME: join(home, ".config"),
|
||||
PI_CODING_AGENT_DIR: join(home, ".pi-agent"),
|
||||
MY_PI_SEARCH_CONFIG: join(home, "missing-search.env"),
|
||||
PATH: `${mockBin}:${process.env.PATH ?? ""}`,
|
||||
PI_OFFLINE: "1",
|
||||
TAVILY_API_KEY: "",
|
||||
EXA_API_KEY: "",
|
||||
KEENABLE_API_KEY: "",
|
||||
},
|
||||
env: loadEnvironment,
|
||||
});
|
||||
const result = runBundle();
|
||||
|
||||
assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`);
|
||||
assert.doesNotMatch(`${result.stdout}\n${result.stderr}`, /Failed to load extension|conflicts with/u);
|
||||
const deployedPermissionConfig = JSON.parse(
|
||||
await readFile(join(home, ".pi-agent", "extensions", "pi-permission-system", "config.json"), "utf8"),
|
||||
const deployedPermissionPolicy = await readFile(
|
||||
join(home, ".pi-agent", "extensions", "pi-permission-system", "policy.jsonc"),
|
||||
"utf8",
|
||||
);
|
||||
assert.deepEqual(deployedPermissionConfig, permissionConfig, "the deployed permission config must match the bundle source");
|
||||
const deployedHardGuards = await readFile(
|
||||
join(home, ".pi-agent", "extensions", "pi-permission-system", "hard-guards.jsonc"),
|
||||
"utf8",
|
||||
);
|
||||
assert.equal(deployedPermissionPolicy, permissionPolicySource, "the seeded policy must match the bundle source");
|
||||
assert.equal(deployedHardGuards, hardGuardSource, "the seeded hard guards must match the bundle source");
|
||||
|
||||
const deployedConfigDir = join(home, ".pi-agent", "extensions", "pi-permission-system");
|
||||
const deployedPolicyPath = join(deployedConfigDir, "policy.jsonc");
|
||||
const deployedHardGuardPath = join(deployedConfigDir, "hard-guards.jsonc");
|
||||
const customPolicy = `{ "permission": { "*": "deny" } }\n`;
|
||||
const customHardGuards = `{ "version": 1, "permission": { "*": "allow" } }\n`;
|
||||
await writeFile(deployedPolicyPath, customPolicy, "utf8");
|
||||
await writeFile(deployedHardGuardPath, customHardGuards, "utf8");
|
||||
const preserveResult = runBundle();
|
||||
assert.equal(preserveResult.status, 0, `${preserveResult.stdout}\n${preserveResult.stderr}`);
|
||||
assert.equal(await readFile(deployedPolicyPath, "utf8"), customPolicy, "reload must preserve user policy edits");
|
||||
assert.equal(await readFile(deployedHardGuardPath, "utf8"), customHardGuards, "reload must preserve user hard guards");
|
||||
|
||||
await rm(deployedPolicyPath);
|
||||
const legacyPolicy = `{ "authorizerChain": ["auto-review"], "permission": { "*": "ask" } }\n`;
|
||||
await writeFile(join(deployedConfigDir, "config.json"), legacyPolicy, "utf8");
|
||||
const migrationResult = runBundle();
|
||||
assert.equal(migrationResult.status, 0, `${migrationResult.stdout}\n${migrationResult.stderr}`);
|
||||
assert.equal(await readFile(deployedPolicyPath, "utf8"), legacyPolicy, "legacy config.json must seed policy.jsonc once");
|
||||
assert.equal(await readFile(deployedHardGuardPath, "utf8"), customHardGuards, "legacy migration must not replace hard guards");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user