diff --git a/src/exec-broker/broker.test.ts b/src/exec-broker/broker.test.ts index 47466687..230b978c 100644 --- a/src/exec-broker/broker.test.ts +++ b/src/exec-broker/broker.test.ts @@ -251,6 +251,38 @@ describe("brokerExec symlink hardening (impure realpath check)", () => { }); }); +describe("brokerExec multi-root fs (policy.fs.roots)", () => { + it("allows a write under ANY listed root and denies one under none", async () => { + const a = mkdtempSync(join(tmpdir(), "kit-broker-a-")); + const b = mkdtempSync(join(tmpdir(), "kit-broker-b-")); + const outside = mkdtempSync(join(tmpdir(), "kit-broker-out-")); + try { + const policy: BrokerPolicy = { ...POLICY, fs: { root: a, roots: [b] } }; + // under the primary root + assert.equal( + (await brokerExec(CTX({ fsWrites: [join(a, "x.txt")] }), policy, async () => 1)).ok, + true, + ); + // under the additional root + assert.equal( + (await brokerExec(CTX({ fsWrites: [join(b, "y.txt")] }), policy, async () => 1)).ok, + true, + ); + // under neither → denied + const out = await brokerExec( + CTX({ fsWrites: [join(outside, "z.txt")] }), + policy, + async () => 1, + ); + assert.equal(out.ok, false); + } finally { + rmSync(a, { recursive: true, force: true }); + rmSync(b, { recursive: true, force: true }); + rmSync(outside, { recursive: true, force: true }); + } + }); +}); + describe("runBrokered opt-in (policy file present/absent)", () => { const prev = process.env.KIT_EXEC_BROKER_POLICY; after(() => { diff --git a/src/exec-broker/broker.ts b/src/exec-broker/broker.ts index 5551d700..63bd8f6a 100644 --- a/src/exec-broker/broker.ts +++ b/src/exec-broker/broker.ts @@ -28,7 +28,7 @@ import { dirname, resolve, sep } from "node:path"; import type { OperationContext } from "../governance-middleware.js"; import { appendAuditEventDirect } from "../audit.js"; import { checkEgress, checkFsWrite, scopeEnv } from "./decisions.js"; -import { brokerPolicyPath, loadBrokerPolicy, type BrokerPolicy } from "./policy.js"; +import { brokerPolicyPath, loadBrokerPolicy, policyFsRoots, type BrokerPolicy } from "./policy.js"; /** Environment label stamped on broker audit entries. */ const BROKER_ENV = "exec-broker"; @@ -157,14 +157,21 @@ function collectDenials(context: BrokerContext, policy: BrokerPolicy): string[] if (!d.ok) denials.push(d.reason ?? `egress denied: ${target}`); } + const roots = policyFsRoots(policy); for (const path of context.fsWrites ?? []) { - const d = checkFsWrite(path, policy.fs.root); - if (!d.ok) { - denials.push(d.reason ?? `fs-write denied: ${path}`); - continue; + // Allowed iff SOME root passes BOTH the pure traversal gate AND the symlink-aware gate. + // Denied only when every root rejects it; report the primary root's reason for clarity. + const allowed = roots.some( + (root) => checkFsWrite(path, root).ok && checkFsWriteRealpath(path, root).ok, + ); + if (!allowed) { + const d = checkFsWrite(path, policy.fs.root); + const sl = d.ok ? checkFsWriteRealpath(path, policy.fs.root) : d; + denials.push( + (!d.ok ? d.reason : sl.reason) ?? + `fs-write denied: ${path} outside ${roots.length} allowed root(s)`, + ); } - const sl = checkFsWriteRealpath(path, policy.fs.root); - if (!sl.ok) denials.push(sl.reason ?? `fs-write symlink-escape denied: ${path}`); } for (const key of context.envRequested ?? []) { diff --git a/src/exec-broker/policy.ts b/src/exec-broker/policy.ts index 9282df88..686e16fd 100644 --- a/src/exec-broker/policy.ts +++ b/src/exec-broker/policy.ts @@ -18,10 +18,21 @@ import { resolve } from "node:path"; export interface BrokerPolicy { egress: { allow: string[] }; - fs: { root: string }; + /** + * Filesystem write-scope. `root` is the primary allowed root (required, back-compatible with + * the original single-root policy + `.kit-exec-broker.json`). `roots` optionally adds more + * allowed roots — the effective allowed set is `[root, ...roots]`. This lets a richer source + * (the signed profile's `[scope].fs` path LIST) map faithfully without breaking the JSON shape. + */ + fs: { root: string; roots?: string[] }; env: { declared: string[] }; } +/** The effective list of allowed write-roots for a policy: `[root, ...roots]`, de-duplicated. */ +export function policyFsRoots(policy: BrokerPolicy): string[] { + return [...new Set([policy.fs.root, ...(policy.fs.roots ?? [])])]; +} + /** The env override knob for this module (mirrors the amazonq/kiro parsers). */ export const BROKER_POLICY_ENV = "KIT_EXEC_BROKER_POLICY"; @@ -56,13 +67,15 @@ function validateBrokerPolicy(parsed: unknown): BrokerPolicy | null { const fs = p.fs as Record | undefined; if (typeof fs !== "object" || fs === null) return null; if (typeof fs.root !== "string" || fs.root.length === 0) return null; + // Optional `roots`: when present it must be a string[] (fail-closed on any other shape). + if (fs.roots !== undefined && !isStringArray(fs.roots)) return null; const env = p.env as Record | undefined; if (typeof env !== "object" || env === null || !isStringArray(env.declared)) return null; return { egress: { allow: [...egress.allow] }, - fs: { root: fs.root }, + fs: fs.roots ? { root: fs.root, roots: [...fs.roots] } : { root: fs.root }, env: { declared: [...env.declared] }, }; }