Skip to content

Commit 5b6e0e4

Browse files
committed
fix(sandbox): materialize protected skill mount roots
1 parent d51582a commit 5b6e0e4

3 files changed

Lines changed: 162 additions & 10 deletions

File tree

‎src/agents/sandbox/context.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import { updateRegistry } from "./registry.js";
2727
import { resolveSandboxRuntimeStatus } from "./runtime-status.js";
2828
import { resolveSandboxScopeKey, resolveSandboxWorkspaceDir } from "./shared.js";
2929
import type { SandboxContext, SandboxDockerConfig, SandboxWorkspaceInfo } from "./types.js";
30-
import { resolveMaterializedSandboxSkillsWorkspaceDir } from "./workspace-mounts.js";
30+
import {
31+
ensureReadOnlyWorkspaceSkillMountSources,
32+
resolveMaterializedSandboxSkillsWorkspaceDir,
33+
} from "./workspace-mounts.js";
3134
import { ensureSandboxWorkspace } from "./workspace.js";
3235

3336
async function syncSandboxSkillsToWorkspace(params: {
@@ -125,6 +128,13 @@ async function ensureSandboxWorkspaceLayout(params: {
125128
agentId: params.agentId,
126129
rawSessionKey,
127130
});
131+
await ensureReadOnlyWorkspaceSkillMountSources({
132+
workspaceDir,
133+
agentWorkspaceDir,
134+
skillsWorkspaceDir,
135+
workdir: cfg.docker.workdir,
136+
workspaceAccess: cfg.workspaceAccess,
137+
});
128138
}
129139

130140
return {

‎src/agents/sandbox/workspace-mounts.test.ts‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import fs from "node:fs";
44
import os from "node:os";
55
import path from "node:path";
66
import { afterEach, describe, expect, it } from "vitest";
7-
import { appendWorkspaceMountArgs } from "./workspace-mounts.js";
7+
import {
8+
appendWorkspaceMountArgs,
9+
ensureReadOnlyWorkspaceSkillMountSources,
10+
resolveReadOnlyWorkspaceSkillMounts,
11+
} from "./workspace-mounts.js";
812

913
const tmpDirs: string[] = [];
1014

@@ -104,6 +108,46 @@ describe("appendWorkspaceMountArgs", () => {
104108
]);
105109
});
106110

111+
it("materializes missing protected skill roots before first rw sandbox launch", async () => {
112+
const agentWorkspaceDir = makeTempWorkspace();
113+
const skillsWorkspaceDir = path.join(makeTempWorkspace(), ".openclaw", "sandbox-skills");
114+
115+
await ensureReadOnlyWorkspaceSkillMountSources({
116+
workspaceDir: agentWorkspaceDir,
117+
agentWorkspaceDir,
118+
skillsWorkspaceDir,
119+
workdir: "/workspace",
120+
workspaceAccess: "rw",
121+
});
122+
123+
expect(fs.statSync(path.join(agentWorkspaceDir, "skills")).isDirectory()).toBe(true);
124+
expect(fs.statSync(path.join(agentWorkspaceDir, ".agents", "skills")).isDirectory()).toBe(true);
125+
expect(fs.statSync(path.join(skillsWorkspaceDir, "skills")).isDirectory()).toBe(true);
126+
127+
expect(
128+
resolveReadOnlyWorkspaceSkillMounts({
129+
workspaceDir: agentWorkspaceDir,
130+
agentWorkspaceDir,
131+
skillsWorkspaceDir,
132+
workdir: "/workspace",
133+
workspaceAccess: "rw",
134+
}),
135+
).toEqual([
136+
{
137+
hostPath: path.join(agentWorkspaceDir, "skills"),
138+
containerPath: "/workspace/skills",
139+
},
140+
{
141+
hostPath: path.join(agentWorkspaceDir, ".agents", "skills"),
142+
containerPath: "/workspace/.agents/skills",
143+
},
144+
{
145+
hostPath: path.join(skillsWorkspaceDir, "skills"),
146+
containerPath: "/workspace/.openclaw/sandbox-skills/skills",
147+
},
148+
]);
149+
});
150+
107151
it.runIf(process.platform !== "win32")("does not overlay symlinked workspace skill roots", () => {
108152
// Skill overlays must be real workspace directories; symlinks could expose
109153
// arbitrary host paths read-only inside the sandbox.
@@ -147,6 +191,44 @@ describe("appendWorkspaceMountArgs", () => {
147191
},
148192
);
149193

194+
it.runIf(process.platform !== "win32")(
195+
"does not materialize missing skill roots through a symlinked parent",
196+
async () => {
197+
const agentWorkspaceDir = makeTempWorkspace();
198+
const outsideDir = makeTempWorkspace();
199+
const skillsWorkspaceDir = path.join(makeTempWorkspace(), ".openclaw", "sandbox-skills");
200+
fs.symlinkSync(outsideDir, path.join(agentWorkspaceDir, ".agents"), "dir");
201+
202+
await ensureReadOnlyWorkspaceSkillMountSources({
203+
workspaceDir: agentWorkspaceDir,
204+
agentWorkspaceDir,
205+
skillsWorkspaceDir,
206+
workdir: "/workspace",
207+
workspaceAccess: "rw",
208+
});
209+
210+
expect(fs.existsSync(path.join(outsideDir, "skills"))).toBe(false);
211+
expect(
212+
resolveReadOnlyWorkspaceSkillMounts({
213+
workspaceDir: agentWorkspaceDir,
214+
agentWorkspaceDir,
215+
skillsWorkspaceDir,
216+
workdir: "/workspace",
217+
workspaceAccess: "rw",
218+
}),
219+
).toEqual([
220+
{
221+
hostPath: path.join(agentWorkspaceDir, "skills"),
222+
containerPath: "/workspace/skills",
223+
},
224+
{
225+
hostPath: path.join(skillsWorkspaceDir, "skills"),
226+
containerPath: "/workspace/.openclaw/sandbox-skills/skills",
227+
},
228+
]);
229+
},
230+
);
231+
150232
it("overlays project .agents skills read-only when workspaceAccess is rw", () => {
151233
const agentWorkspaceDir = makeTempWorkspace();
152234
fs.mkdirSync(path.join(agentWorkspaceDir, ".agents", "skills", "demo"), {

‎src/agents/sandbox/workspace-mounts.ts‎

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Creates Docker bind specs for writable workspaces and read-only skill source mounts.
55
*/
66
import fs from "node:fs";
7+
import fsp from "node:fs/promises";
78
import path from "node:path";
89
import { isPathInside } from "../../infra/path-guards.js";
910
import { SANDBOX_AGENT_WORKSPACE_MOUNT } from "./constants.js";
@@ -19,6 +20,10 @@ export type ReadOnlyWorkspaceSkillMount = {
1920
containerPath: string;
2021
};
2122

23+
type ReadOnlyWorkspaceSkillMountCandidate = ReadOnlyWorkspaceSkillMount & {
24+
rootDir: string;
25+
};
26+
2227
function formatManagedWorkspaceBind(params: {
2328
hostPath: string;
2429
containerPath: string;
@@ -41,6 +46,15 @@ export function resolveMaterializedSandboxSkillsWorkspaceDir(rootDir: string): s
4146
return path.join(rootDir, ...MATERIALIZED_SANDBOX_SKILLS_WORKSPACE_PARTS);
4247
}
4348

49+
function isCanonicalWorkspaceSkillMountSource(params: {
50+
rootDir: string;
51+
hostPath: string;
52+
}): boolean {
53+
const agentRoot = resolveSandboxHostPathViaExistingAncestor(path.resolve(params.rootDir));
54+
const canonicalSource = resolveSandboxHostPathViaExistingAncestor(path.resolve(params.hostPath));
55+
return isPathInside(agentRoot, canonicalSource);
56+
}
57+
4458
/** Returns true when a skill mount source exists inside the canonical mount root. */
4559
export function isExistingWorkspaceSkillMountSource(params: {
4660
rootDir: string;
@@ -54,27 +68,25 @@ export function isExistingWorkspaceSkillMountSource(params: {
5468
return false;
5569
}
5670

57-
const agentRoot = resolveSandboxHostPathViaExistingAncestor(path.resolve(params.rootDir));
58-
const canonicalSource = resolveSandboxHostPathViaExistingAncestor(path.resolve(params.hostPath));
59-
return isPathInside(agentRoot, canonicalSource);
71+
return isCanonicalWorkspaceSkillMountSource(params);
6072
}
6173

62-
/** Finds agent-workspace skill directories that should be mounted read-only in rw workspaces. */
63-
export function resolveReadOnlyWorkspaceSkillMounts(params: {
74+
function resolveReadOnlyWorkspaceSkillMountCandidates(params: {
6475
workspaceDir: string;
6576
agentWorkspaceDir: string;
6677
skillsWorkspaceDir?: string;
6778
workdir: string;
6879
workspaceAccess: SandboxWorkspaceAccess;
69-
}): ReadOnlyWorkspaceSkillMount[] {
80+
}): ReadOnlyWorkspaceSkillMountCandidate[] {
7081
if (params.workspaceAccess !== "rw") {
7182
return [];
7283
}
7384

7485
// RW workspaces mount the project as writable, but skill sources remain read-only so agent
7586
// instructions are visible without letting sandbox commands mutate them.
7687
const materializedSkillsWorkspaceDir =
77-
params.skillsWorkspaceDir ?? resolveMaterializedSandboxSkillsWorkspaceDir(params.agentWorkspaceDir);
88+
params.skillsWorkspaceDir ??
89+
resolveMaterializedSandboxSkillsWorkspaceDir(params.agentWorkspaceDir);
7890
const mounts = [
7991
{
8092
hostPath: path.join(params.agentWorkspaceDir, "skills"),
@@ -96,8 +108,56 @@ export function resolveReadOnlyWorkspaceSkillMounts(params: {
96108
rootDir: materializedSkillsWorkspaceDir,
97109
},
98110
];
111+
return mounts;
112+
}
113+
114+
/** Materializes protected skill mount roots before a first rw sandbox launch. */
115+
export async function ensureReadOnlyWorkspaceSkillMountSources(params: {
116+
workspaceDir: string;
117+
agentWorkspaceDir: string;
118+
skillsWorkspaceDir?: string;
119+
workdir: string;
120+
workspaceAccess: SandboxWorkspaceAccess;
121+
}): Promise<void> {
122+
for (const mount of resolveReadOnlyWorkspaceSkillMountCandidates(params)) {
123+
if (
124+
!isCanonicalWorkspaceSkillMountSource({ rootDir: mount.rootDir, hostPath: mount.hostPath })
125+
) {
126+
continue;
127+
}
128+
try {
129+
const stat = await fsp.lstat(mount.hostPath);
130+
if (stat.isDirectory()) {
131+
continue;
132+
}
133+
// Existing non-directory or symlink leaves stay rejected by the mount resolver.
134+
continue;
135+
} catch (error) {
136+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
137+
throw error;
138+
}
139+
}
99140

100-
return mounts
141+
await fsp.mkdir(mount.hostPath, { recursive: true });
142+
if (
143+
!isExistingWorkspaceSkillMountSource({ rootDir: mount.rootDir, hostPath: mount.hostPath })
144+
) {
145+
throw new Error(
146+
`Created sandbox skill mount source outside workspace root: ${mount.hostPath}`,
147+
);
148+
}
149+
}
150+
}
151+
152+
/** Finds agent-workspace skill directories that should be mounted read-only in rw workspaces. */
153+
export function resolveReadOnlyWorkspaceSkillMounts(params: {
154+
workspaceDir: string;
155+
agentWorkspaceDir: string;
156+
skillsWorkspaceDir?: string;
157+
workdir: string;
158+
workspaceAccess: SandboxWorkspaceAccess;
159+
}): ReadOnlyWorkspaceSkillMount[] {
160+
return resolveReadOnlyWorkspaceSkillMountCandidates(params)
101161
.filter((mount) =>
102162
isExistingWorkspaceSkillMountSource({
103163
rootDir: mount.rootDir,

0 commit comments

Comments
 (0)