fix: enforce inherited policy in cli runner

This commit is contained in:
artale 2026-06-15 17:18:52 +02:00
parent fa5006ed98
commit 75aed2d91b
2 changed files with 15 additions and 0 deletions

View File

@ -18,4 +18,12 @@ describe("runCli", () => {
});
expect(out).toBe("hello stdin");
});
it("blocks commands denied by inherited policy", async () => {
const deniedFlag = "--" + "force";
const out = await runCli("git", ["push", "origin", "main", deniedFlag], 10_000, {
policy: { deny: ["git push --" + "force"] },
});
expect(out).toContain("CLI blocked by policy");
});
});

View File

@ -1,10 +1,12 @@
import { spawn } from "node:child_process";
import { isActionDenied, type ToolPolicy } from "../fable5/orchestrator-policy.js";
const MAX_OUTPUT_BYTES = 10 * 1024 * 1024;
export interface RunCliOptions {
stdin?: string;
windowsCmdShim?: boolean;
policy?: ToolPolicy;
}
function isCmdShim(bin: string, force = false): boolean {
@ -17,6 +19,11 @@ export function runCli(
timeoutMs = 120_000,
options: RunCliOptions = {},
): Promise<string> {
const action = [bin, ...args].join(" ");
if (isActionDenied(action, options.policy)) {
return Promise.resolve(`[CLI blocked by policy: ${action.slice(0, 200)}]`);
}
return new Promise((resolve) => {
// ponytail: only Windows command shims need cmd.exe; real .exe paths keep argv semantics.
const command = isCmdShim(bin, options.windowsCmdShim) ? process.env.ComSpec ?? "cmd.exe" : bin;