fix: per-agent sandbox overrides
This commit is contained in:
@@ -55,7 +55,12 @@ describe("resolveAgentConfig", () => {
|
||||
mode: "all",
|
||||
scope: "agent",
|
||||
perSession: false,
|
||||
workspaceAccess: "ro",
|
||||
workspaceRoot: "~/sandboxes",
|
||||
tools: {
|
||||
allow: ["read"],
|
||||
deny: ["bash"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -66,7 +71,12 @@ describe("resolveAgentConfig", () => {
|
||||
mode: "all",
|
||||
scope: "agent",
|
||||
perSession: false,
|
||||
workspaceAccess: "ro",
|
||||
workspaceRoot: "~/sandboxes",
|
||||
tools: {
|
||||
allow: ["read"],
|
||||
deny: ["bash"],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -29,9 +29,14 @@ export function resolveAgentConfig(
|
||||
model?: string;
|
||||
sandbox?: {
|
||||
mode?: "off" | "non-main" | "all";
|
||||
workspaceAccess?: "none" | "ro" | "rw";
|
||||
scope?: "session" | "agent" | "shared";
|
||||
perSession?: boolean;
|
||||
workspaceRoot?: string;
|
||||
tools?: {
|
||||
allow?: string[];
|
||||
deny?: string[];
|
||||
};
|
||||
};
|
||||
tools?: {
|
||||
allow?: string[];
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import { createClawdbotCodingTools } from "./pi-tools.js";
|
||||
import type { SandboxDockerConfig } from "./sandbox.js";
|
||||
|
||||
describe("Agent-specific tool filtering", () => {
|
||||
it("should apply global tool policy when no agent-specific policy exists", () => {
|
||||
@@ -188,7 +189,15 @@ describe("Agent-specific tool filtering", () => {
|
||||
workspaceAccess: "none",
|
||||
containerName: "test-container",
|
||||
containerWorkdir: "/workspace",
|
||||
docker: {} as any,
|
||||
docker: {
|
||||
image: "test-image",
|
||||
containerPrefix: "test-",
|
||||
workdir: "/workspace",
|
||||
readOnlyRoot: true,
|
||||
tmpfs: [],
|
||||
network: "none",
|
||||
capDrop: [],
|
||||
} satisfies SandboxDockerConfig,
|
||||
tools: {
|
||||
allow: ["read", "write", "bash"],
|
||||
deny: [],
|
||||
|
||||
@@ -596,7 +596,7 @@ export function createClawdbotCodingTools(options?: {
|
||||
options.config.agent.tools.deny?.length)
|
||||
? filterToolsByPolicy(filtered, options.config.agent.tools)
|
||||
: filtered;
|
||||
|
||||
|
||||
// Agent-specific tool policy
|
||||
let agentFiltered = globallyFiltered;
|
||||
if (options?.sessionKey && options?.config) {
|
||||
@@ -606,7 +606,7 @@ export function createClawdbotCodingTools(options?: {
|
||||
agentFiltered = filterToolsByPolicy(globallyFiltered, agentConfig.tools);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const sandboxed = sandbox
|
||||
? filterToolsByPolicy(agentFiltered, sandbox.tools)
|
||||
: agentFiltered;
|
||||
|
||||
@@ -1,13 +1,33 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { EventEmitter } from "node:events";
|
||||
import { Readable } from "node:stream";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
|
||||
// We need to test the internal defaultSandboxConfig function, but it's not exported.
|
||||
// Instead, we test the behavior through resolveSandboxContext which uses it.
|
||||
|
||||
vi.mock("node:child_process", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("node:child_process")>();
|
||||
return {
|
||||
...actual,
|
||||
spawn: () => {
|
||||
const child = new EventEmitter() as {
|
||||
stdout?: Readable;
|
||||
stderr?: Readable;
|
||||
on: (event: string, cb: (...args: unknown[]) => void) => void;
|
||||
};
|
||||
child.stdout = new Readable({ read() {} });
|
||||
child.stderr = new Readable({ read() {} });
|
||||
queueMicrotask(() => child.emit("close", 0));
|
||||
return child;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
describe("Agent-specific sandbox config", () => {
|
||||
it("should use global sandbox config when no agent-specific config exists", async () => {
|
||||
const { resolveSandboxContext } = await import("./sandbox.js");
|
||||
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
agent: {
|
||||
sandbox: {
|
||||
@@ -36,7 +56,7 @@ describe("Agent-specific sandbox config", () => {
|
||||
|
||||
it("should override with agent-specific sandbox mode 'off'", async () => {
|
||||
const { resolveSandboxContext } = await import("./sandbox.js");
|
||||
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
agent: {
|
||||
sandbox: {
|
||||
@@ -68,7 +88,7 @@ describe("Agent-specific sandbox config", () => {
|
||||
|
||||
it("should use agent-specific sandbox mode 'all'", async () => {
|
||||
const { resolveSandboxContext } = await import("./sandbox.js");
|
||||
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
agent: {
|
||||
sandbox: {
|
||||
@@ -100,7 +120,7 @@ describe("Agent-specific sandbox config", () => {
|
||||
|
||||
it("should use agent-specific scope", async () => {
|
||||
const { resolveSandboxContext } = await import("./sandbox.js");
|
||||
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
agent: {
|
||||
sandbox: {
|
||||
@@ -134,7 +154,7 @@ describe("Agent-specific sandbox config", () => {
|
||||
|
||||
it("should use agent-specific workspaceRoot", async () => {
|
||||
const { resolveSandboxContext } = await import("./sandbox.js");
|
||||
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
agent: {
|
||||
sandbox: {
|
||||
@@ -169,7 +189,7 @@ describe("Agent-specific sandbox config", () => {
|
||||
|
||||
it("should prefer agent config over global for multiple agents", async () => {
|
||||
const { resolveSandboxContext } = await import("./sandbox.js");
|
||||
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
agent: {
|
||||
sandbox: {
|
||||
@@ -213,4 +233,48 @@ describe("Agent-specific sandbox config", () => {
|
||||
expect(familyContext).toBeDefined();
|
||||
expect(familyContext?.enabled).toBe(true);
|
||||
});
|
||||
|
||||
it("should prefer agent-specific sandbox tool policy", async () => {
|
||||
const { resolveSandboxContext } = await import("./sandbox.js");
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
agent: {
|
||||
sandbox: {
|
||||
mode: "all",
|
||||
scope: "agent",
|
||||
tools: {
|
||||
allow: ["read"],
|
||||
deny: ["bash"],
|
||||
},
|
||||
},
|
||||
},
|
||||
routing: {
|
||||
agents: {
|
||||
restricted: {
|
||||
workspace: "~/clawd-restricted",
|
||||
sandbox: {
|
||||
mode: "all",
|
||||
scope: "agent",
|
||||
tools: {
|
||||
allow: ["read", "write"],
|
||||
deny: ["edit"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const context = await resolveSandboxContext({
|
||||
config: cfg,
|
||||
sessionKey: "agent:restricted:main",
|
||||
workspaceDir: "/tmp/test-restricted",
|
||||
});
|
||||
|
||||
expect(context).toBeDefined();
|
||||
expect(context?.tools).toEqual({
|
||||
allow: ["read", "write"],
|
||||
deny: ["edit"],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -226,9 +226,12 @@ function resolveSandboxScopeKey(scope: SandboxScope, sessionKey: string) {
|
||||
return `agent:${agentId}`;
|
||||
}
|
||||
|
||||
function defaultSandboxConfig(cfg?: ClawdbotConfig, agentId?: string): SandboxConfig {
|
||||
function defaultSandboxConfig(
|
||||
cfg?: ClawdbotConfig,
|
||||
agentId?: string,
|
||||
): SandboxConfig {
|
||||
const agent = cfg?.agent?.sandbox;
|
||||
|
||||
|
||||
// Agent-specific sandbox config overrides global
|
||||
let agentSandbox: typeof agent | undefined;
|
||||
if (agentId && cfg?.routing?.agents) {
|
||||
@@ -237,15 +240,19 @@ function defaultSandboxConfig(cfg?: ClawdbotConfig, agentId?: string): SandboxCo
|
||||
agentSandbox = agentConfig.sandbox;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
mode: agentSandbox?.mode ?? agent?.mode ?? "off",
|
||||
scope: resolveSandboxScope({
|
||||
scope: agentSandbox?.scope ?? agent?.scope,
|
||||
perSession: agentSandbox?.perSession ?? agent?.perSession,
|
||||
}),
|
||||
workspaceAccess: agentSandbox?.workspaceAccess ?? agent?.workspaceAccess ?? "none",
|
||||
workspaceRoot: agentSandbox?.workspaceRoot ?? agent?.workspaceRoot ?? DEFAULT_SANDBOX_WORKSPACE_ROOT,
|
||||
workspaceAccess:
|
||||
agentSandbox?.workspaceAccess ?? agent?.workspaceAccess ?? "none",
|
||||
workspaceRoot:
|
||||
agentSandbox?.workspaceRoot ??
|
||||
agent?.workspaceRoot ??
|
||||
DEFAULT_SANDBOX_WORKSPACE_ROOT,
|
||||
docker: {
|
||||
image: agent?.docker?.image ?? DEFAULT_SANDBOX_IMAGE,
|
||||
containerPrefix:
|
||||
@@ -281,8 +288,10 @@ function defaultSandboxConfig(cfg?: ClawdbotConfig, agentId?: string): SandboxCo
|
||||
enableNoVnc: agent?.browser?.enableNoVnc ?? true,
|
||||
},
|
||||
tools: {
|
||||
allow: agent?.tools?.allow ?? DEFAULT_TOOL_ALLOW,
|
||||
deny: agent?.tools?.deny ?? DEFAULT_TOOL_DENY,
|
||||
allow:
|
||||
agentSandbox?.tools?.allow ?? agent?.tools?.allow ?? DEFAULT_TOOL_ALLOW,
|
||||
deny:
|
||||
agentSandbox?.tools?.deny ?? agent?.tools?.deny ?? DEFAULT_TOOL_DENY,
|
||||
},
|
||||
prune: {
|
||||
idleHours: agent?.prune?.idleHours ?? DEFAULT_SANDBOX_IDLE_HOURS,
|
||||
|
||||
@@ -586,11 +586,18 @@ export type RoutingConfig = {
|
||||
model?: string;
|
||||
sandbox?: {
|
||||
mode?: "off" | "non-main" | "all";
|
||||
/** Agent workspace access inside the sandbox. */
|
||||
workspaceAccess?: "none" | "ro" | "rw";
|
||||
/** Container/workspace scope for sandbox isolation. */
|
||||
scope?: "session" | "agent" | "shared";
|
||||
/** Legacy alias for scope ("session" when true, "shared" when false). */
|
||||
perSession?: boolean;
|
||||
workspaceRoot?: string;
|
||||
/** Tool allow/deny policy for sandboxed sessions (deny wins). */
|
||||
tools?: {
|
||||
allow?: string[];
|
||||
deny?: string[];
|
||||
};
|
||||
};
|
||||
tools?: {
|
||||
allow?: string[];
|
||||
|
||||
@@ -236,6 +236,9 @@ const RoutingSchema = z
|
||||
z.literal("all"),
|
||||
])
|
||||
.optional(),
|
||||
workspaceAccess: z
|
||||
.union([z.literal("none"), z.literal("ro"), z.literal("rw")])
|
||||
.optional(),
|
||||
scope: z
|
||||
.union([
|
||||
z.literal("session"),
|
||||
@@ -245,6 +248,12 @@ const RoutingSchema = z
|
||||
.optional(),
|
||||
perSession: z.boolean().optional(),
|
||||
workspaceRoot: z.string().optional(),
|
||||
tools: z
|
||||
.object({
|
||||
allow: z.array(z.string()).optional(),
|
||||
deny: z.array(z.string()).optional(),
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
.optional(),
|
||||
tools: z
|
||||
|
||||
Reference in New Issue
Block a user