Skip to content

Commit c6fa27a

Browse files
fix(sandbox): allow reads from declared binds (#130540)
1 parent 67d22a5 commit c6fa27a

2 files changed

Lines changed: 59 additions & 27 deletions

File tree

‎src/agents/agent-tools.create-openclaw-coding-tools.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ import { createOpenClawTools } from "./openclaw-tools.js";
4949
import { expectReadWriteEditTools } from "./test-helpers/agent-tools-fs-helpers.js";
5050
import { createAgentToolsSandboxContext } from "./test-helpers/agent-tools-sandbox-context.js";
5151
import { stubTool } from "./test-helpers/fast-tool-stubs.js";
52-
import { createHostSandboxFsBridge } from "./test-helpers/host-sandbox-fs-bridge.js";
52+
import {
53+
createHostSandboxFsBridge,
54+
createSandboxFsBridgeFromResolver,
55+
} from "./test-helpers/host-sandbox-fs-bridge.js";
5356
import { buildEmptyExplicitToolAllowlistError } from "./tool-allowlist-guard.js";
5457
import { DEFAULT_PLUGIN_TOOLS_ALLOWLIST_ENTRY, normalizeToolPolicyName } from "./tool-policy.js";
5558
import { replaceWithEffectiveCronCreatorToolAllowlist } from "./tools/cron-tool.js";
@@ -1082,6 +1085,54 @@ describe("createOpenClawCodingTools", () => {
10821085
expect(latestCreateOpenClawToolsOptions().fsPolicy).toEqual({ workspaceOnly: true });
10831086
});
10841087

1088+
it("allows workspace-only reads from declared sandbox bind mounts", async () => {
1089+
const workspaceDir = tempDirs.make("openclaw-sandbox-workspace-");
1090+
const bindRoot = tempDirs.make("openclaw-sandbox-bind-");
1091+
const boundFile = path.join(bindRoot, "example", "tree-index.json");
1092+
await fs.mkdir(path.dirname(boundFile), { recursive: true });
1093+
await fs.writeFile(boundFile, '{"ready":true}\n', "utf8");
1094+
1095+
const sandbox = createAgentToolsSandboxContext({
1096+
workspaceDir,
1097+
workspaceAccess: "none",
1098+
dockerOverrides: { binds: [`${bindRoot}:/cache/repos:ro`] },
1099+
});
1100+
sandbox.fsBridge = createSandboxFsBridgeFromResolver((filePath) => {
1101+
const relativePath = path.posix.relative("/cache/repos", filePath);
1102+
return {
1103+
hostPath: path.join(bindRoot, relativePath),
1104+
relativePath,
1105+
containerPath: filePath,
1106+
};
1107+
});
1108+
1109+
const tools = createOpenClawCodingTools({
1110+
workspaceDir,
1111+
config: { tools: { fs: { workspaceOnly: true } } },
1112+
sandbox,
1113+
});
1114+
1115+
const { readTool, writeTool, editTool } = expectReadWriteEditTools(tools);
1116+
const result = await readTool.execute("read-declared-bind", {
1117+
path: "/cache/repos/example/tree-index.json",
1118+
});
1119+
expect(extractToolText(result)).toContain('{"ready":true}');
1120+
await expect(
1121+
writeTool.execute("write-declared-bind", {
1122+
path: "/cache/repos/example/tree-index.json",
1123+
content: "overwritten",
1124+
}),
1125+
).rejects.toThrow(/Path escapes sandbox root/i);
1126+
await expect(
1127+
editTool.execute("edit-declared-bind", {
1128+
path: "/cache/repos/example/tree-index.json",
1129+
oldText: "true",
1130+
newText: "false",
1131+
}),
1132+
).rejects.toThrow(/Path escapes sandbox root/i);
1133+
await expect(fs.readFile(boundFile, "utf8")).resolves.toBe('{"ready":true}\n');
1134+
});
1135+
10851136
it("uses the canonical spawn workspace for follow-up task suggestions", () => {
10861137
const createOpenClawToolsMock = vi.mocked(createOpenClawTools);
10871138
createOpenClawToolsMock.mockClear();

‎src/agents/core-coding-tools.ts‎

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,21 @@ import { createLazyExecTool } from "./lazy-exec-tool.js";
2222
import { createLazyProcessTool } from "./lazy-process-tool.js";
2323
import type { MemoryWriteProvenanceObserver } from "./memory-write-provenance.js";
2424
import type { SandboxContext } from "./sandbox.js";
25-
import { SANDBOX_AGENT_WORKSPACE_MOUNT } from "./sandbox/constants.js";
26-
import {
27-
resolveReadOnlyWorkspaceSkillMounts,
28-
type ReadOnlyWorkspaceSkillMount,
29-
} from "./sandbox/workspace-mounts.js";
25+
import { buildSandboxFsMounts } from "./sandbox/fs-paths.js";
26+
import { resolveReadOnlyWorkspaceSkillMounts } from "./sandbox/workspace-mounts.js";
3027
import type {
3128
createEditTool,
3229
createReadTool as CreateReadTool,
3330
createWriteTool,
3431
} from "./sessions/tools/index.js";
3532
import { createReadTool } from "./sessions/tools/read.js";
3633

37-
function readOnlySandboxReadMounts(
34+
function sandboxReadMounts(
3835
sandbox: SandboxContext,
39-
readOnlyWorkspaceSkillMounts: readonly ReadOnlyWorkspaceSkillMount[],
4036
): Array<{ containerRoot: string; hostRoot: string }> | undefined {
41-
const mounts: Array<{ containerRoot: string; hostRoot: string }> = [];
42-
if (sandbox.workspaceAccess === "ro" && sandbox.agentWorkspaceDir !== sandbox.workspaceDir) {
43-
mounts.push({
44-
containerRoot: SANDBOX_AGENT_WORKSPACE_MOUNT,
45-
hostRoot: sandbox.agentWorkspaceDir,
46-
});
47-
}
48-
if (sandbox.workspaceAccess === "rw") {
49-
mounts.push(
50-
...readOnlyWorkspaceSkillMounts.map((mount) => ({
51-
containerRoot: mount.containerPath,
52-
hostRoot: mount.hostPath,
53-
})),
54-
);
55-
}
37+
const mounts = buildSandboxFsMounts(sandbox)
38+
.filter((mount) => mount.source !== "workspace")
39+
.map((mount) => ({ containerRoot: mount.containerRoot, hostRoot: mount.hostRoot }));
5640
return mounts.length > 0 ? mounts : undefined;
5741
}
5842

@@ -166,10 +150,7 @@ export function createCoreCodingTools(options: CoreCodingToolsOptions): AnyAgent
166150
sandboxRoot ?? options.containmentRoot,
167151
sandboxRoot
168152
? {
169-
additionalContainerMounts: readOnlySandboxReadMounts(
170-
sandbox,
171-
readOnlyWorkspaceSkillMounts,
172-
),
153+
additionalContainerMounts: sandboxReadMounts(sandbox),
173154
containerWorkdir: sandbox.containerWorkdir,
174155
bridge: sandboxFsBridge,
175156
}

0 commit comments

Comments
 (0)