Skip to content

Commit 1c4fd46

Browse files
fix(sandbox): allow reads from declared binds
1 parent 1f2e998 commit 1c4fd46

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
@@ -21,37 +21,21 @@ import { createLazyExecTool } from "./lazy-exec-tool.js";
2121
import { createLazyProcessTool } from "./lazy-process-tool.js";
2222
import type { MemoryWriteProvenanceObserver } from "./memory-write-provenance.js";
2323
import type { SandboxContext } from "./sandbox.js";
24-
import { SANDBOX_AGENT_WORKSPACE_MOUNT } from "./sandbox/constants.js";
25-
import {
26-
resolveReadOnlyWorkspaceSkillMounts,
27-
type ReadOnlyWorkspaceSkillMount,
28-
} from "./sandbox/workspace-mounts.js";
24+
import { buildSandboxFsMounts } from "./sandbox/fs-paths.js";
25+
import { resolveReadOnlyWorkspaceSkillMounts } from "./sandbox/workspace-mounts.js";
2926
import type {
3027
createEditTool,
3128
createReadTool as CreateReadTool,
3229
createWriteTool,
3330
} from "./sessions/tools/index.js";
3431
import { createReadTool } from "./sessions/tools/read.js";
3532

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

@@ -165,10 +149,7 @@ export function createCoreCodingTools(options: CoreCodingToolsOptions): AnyAgent
165149
sandboxRoot ?? options.containmentRoot,
166150
sandboxRoot
167151
? {
168-
additionalContainerMounts: readOnlySandboxReadMounts(
169-
sandbox,
170-
readOnlyWorkspaceSkillMounts,
171-
),
152+
additionalContainerMounts: sandboxReadMounts(sandbox),
172153
containerWorkdir: sandbox.containerWorkdir,
173154
bridge: sandboxFsBridge,
174155
}

0 commit comments

Comments
 (0)