Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Fix command-form approval rules (#324)

Joshua Franced719f54075af
diff --git a/cli/src/sandbox-layer.ts b/cli/src/sandbox-layer.ts
index 45390ee95..49d184cce 100644
--- a/cli/src/sandbox-layer.ts
+++ b/cli/src/sandbox-layer.ts
@@ -623,7 +623,7 @@ export function compileApproval(binary: string, a: ToolApproval): { pattern: str
const decision: ApprovalDecision = a.decision ?? "require_approval";
if (a.pattern !== undefined) return { pattern: a.pattern, decision };
const words = (a.command ?? "").trim().split(/\s+/).filter(Boolean).map(escapeRegex);
- const pattern = `\\b${[escapeRegex(binary), ...words].join("\\s+")}(?:\\b|(?=\\s|$))`;
+ const pattern = `\\b${[escapeRegex(binary), ...words].join("\\s+")}(?:\\b|\\s|$)`;
return { pattern, decision };
}
diff --git a/cli/test/tool-descriptor.test.ts b/cli/test/tool-descriptor.test.ts
index 90506e529..c5098c25c 100644
--- a/cli/test/tool-descriptor.test.ts
+++ b/cli/test/tool-descriptor.test.ts
@@ -235,15 +235,15 @@ test("approvals: command|pattern (exactly one), decision enum, reason optional",
test("compileApproval anchors a command to the binary and builds the regex; pattern is verbatim", () => {
assert.deepEqual(compileApproval("my-tool", { command: "deploy" }), {
- pattern: "\\bmy-tool\\s+deploy(?:\\b|(?=\\s|$))",
+ pattern: "\\bmy-tool\\s+deploy(?:\\b|\\s|$)",
decision: "require_approval",
});
assert.deepEqual(compileApproval("my-tool", { command: "secrets set" }), {
- pattern: "\\bmy-tool\\s+secrets\\s+set(?:\\b|(?=\\s|$))",
+ pattern: "\\bmy-tool\\s+secrets\\s+set(?:\\b|\\s|$)",
decision: "require_approval",
});
assert.deepEqual(compileApproval("my-tool", { command: "delete", decision: "deny" }), {
- pattern: "\\bmy-tool\\s+delete(?:\\b|(?=\\s|$))",
+ pattern: "\\bmy-tool\\s+delete(?:\\b|\\s|$)",
decision: "deny",
});
assert.deepEqual(compileApproval("my-tool", { pattern: "\\bmy-tool\\b\\s+--force\\b" }), {
diff --git a/src/deployment/deployment-layer.ts b/src/deployment/deployment-layer.ts
index 9ee0c27e4..16d3e24fd 100644
--- a/src/deployment/deployment-layer.ts
+++ b/src/deployment/deployment-layer.ts
@@ -612,7 +612,7 @@ export function compileApproval(binary: string, a: ToolApproval): { pattern: str
const decision: ApprovalDecision = a.decision ?? "require_approval";
if (a.pattern !== undefined) return { pattern: a.pattern, decision };
const words = (a.command ?? "").trim().split(/\s+/).filter(Boolean).map(escapeRegex);
- return { pattern: `\\b${[escapeRegex(binary), ...words].join("\\s+")}(?:\\b|(?=\\s|$))`, decision };
+ return { pattern: `\\b${[escapeRegex(binary), ...words].join("\\s+")}(?:\\b|\\s|$)`, decision };
}
export function interpolateSplitEnv(
diff --git a/test/deployment-layer-load.test.ts b/test/deployment-layer-load.test.ts
index f73606574..cc91de170 100644
--- a/test/deployment-layer-load.test.ts
+++ b/test/deployment-layer-load.test.ts
@@ -8,6 +8,7 @@ import { buildApp } from "../src/wiring.ts";
import { testConfig } from "./support/test-config.ts";
import { parseToolDescriptor } from "../src/deployment/deployment-layer.ts";
import { BASE_EPHEMERAL_CRED_LINKS, BASE_RESIDENT_AUTH_PATHS } from "../src/credentials/resident-paths.ts";
+import { evaluateCommandWithLayer } from "../src/policy/command-policy.ts";
const credentialFile = (path: string) => ({ path, kind: "file" as const });
const credentialDirectory = (path: string) => ({ path, kind: "directory" as const });
@@ -121,13 +122,24 @@ test("loadDeploymentLayer: an authless tool contributes no connector or paths",
assert.deepEqual(layer.advertisedTools, ["helper tool"]);
});
-test("approval rules target install.binary rather than the descriptor id", () => {
+test("command-form approval rules target install.binary and are enforced by policy evaluation", () => {
const layer = loadDeploymentLayer(
layerDir({
- acme: { id: "acme", install: { binary: "acmectl" }, approvals: [{ command: "delete", decision: "deny" }] },
+ acme: {
+ id: "acme",
+ install: { binary: "acmectl" },
+ approvals: [{ command: "delete", decision: "require_approval" }],
+ },
}),
);
- assert.equal(layer.commandRules[0]?.pattern, "\\bacmectl\\s+delete(?:\\b|(?=\\s|$))");
+ assert.equal(layer.commandRules[0]?.pattern, "\\bacmectl\\s+delete(?:\\b|\\s|$)");
+ const policy = { mode: "denylist" as const, rules: [] };
+ assert.equal(
+ evaluateCommandWithLayer("acmectl delete project", policy, layer.commandRules).decision,
+ "require_approval",
+ );
+ assert.equal(evaluateCommandWithLayer("acmectl delete", policy, layer.commandRules).decision, "require_approval");
+ assert.equal(evaluateCommandWithLayer("acmectl deleteall", policy, layer.commandRules).decision, "allow");
});
test("loadDeploymentLayer: a dir with no tools/ is a valid, empty layer", () => {