Skip to content

Commit ff607ad

Browse files
authored
fix(sandbox): block home credential binds (#59157)
* fix(sandbox): block home credential binds * fix(sandbox): harden blocked credential bind checks
1 parent 4540eff commit ff607ad

3 files changed

Lines changed: 149 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ Docs: https://docs.openclaw.ai
125125

126126
### Fixes
127127

128+
- Sandbox/security: block credential-path binds even when sandbox home paths resolve through canonical aliases, so agent containers cannot mount user secret stores through alternate home-directory paths. (#59157) Thanks @eleqtrizit.
129+
130+
## 2026.4.1-beta.1
128131
- Providers/transport policy: centralize request auth, proxy, TLS, and header shaping across shared HTTP, stream, and websocket paths, block insecure TLS/runtime transport overrides, and keep proxy-hop TLS separate from target mTLS settings. (#59682) Thanks @vincentkoc.
129132
- Providers/OpenRouter: gate documented OpenRouter attribution to native OpenRouter endpoints or the default route so custom proxy base URLs do not inherit OpenRouter request headers.
130133
- Providers/Copilot: classify native GitHub Copilot API hosts in the shared provider endpoint resolver and harden token-derived proxy endpoint parsing so Copilot base URL routing stays centralized and fails closed on malformed hints. (#59644) Thanks @vincentkoc.

‎src/agents/sandbox/validate-sandbox-security.test.ts‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mkdirSync, mkdtempSync, symlinkSync } from "node:fs";
22
import { tmpdir } from "node:os";
33
import { join } from "node:path";
4-
import { describe, expect, it } from "vitest";
4+
import { afterEach, describe, expect, it, vi } from "vitest";
55
import {
66
getBlockedBindReason,
77
validateBindMounts,
@@ -16,6 +16,10 @@ function expectBindMountsToThrow(binds: string[], expected: RegExp, label: strin
1616
}
1717

1818
describe("getBlockedBindReason", () => {
19+
afterEach(() => {
20+
vi.unstubAllEnvs();
21+
});
22+
1923
it("blocks common Docker socket directories", () => {
2024
expect(getBlockedBindReason("/run:/run")).toEqual(expect.objectContaining({ kind: "targets" }));
2125
expect(getBlockedBindReason("/var/run:/var/run:ro")).toEqual(
@@ -26,9 +30,66 @@ describe("getBlockedBindReason", () => {
2630
it("does not block /var by default", () => {
2731
expect(getBlockedBindReason("/var:/var")).toBeNull();
2832
});
33+
34+
it("blocks sensitive home credential paths", () => {
35+
vi.stubEnv("HOME", "/home/tester");
36+
37+
const cases = [
38+
"/home/tester/.aws/credentials",
39+
"/home/tester/.cargo/credentials.toml",
40+
"/home/tester/.config/gcloud",
41+
"/home/tester/.docker/config.json",
42+
"/home/tester/.gnupg/private-keys-v1.d",
43+
"/home/tester/.netrc",
44+
"/home/tester/.npm/_logs",
45+
"/home/tester/.ssh/config",
46+
] as const;
47+
48+
for (const source of cases) {
49+
expect(getBlockedBindReason(`${source}:/mnt/test:ro`)).toEqual(
50+
expect.objectContaining({ kind: "targets" }),
51+
);
52+
}
53+
});
54+
55+
it("still blocks OS-home credential paths when OPENCLAW_HOME points elsewhere", () => {
56+
vi.stubEnv("HOME", "/home/tester");
57+
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
58+
59+
expect(getBlockedBindReason("/home/tester/.gnupg/secring.gpg:/mnt/gnupg:ro")).toEqual(
60+
expect.objectContaining({
61+
kind: "targets",
62+
blockedPath: "/home/tester/.gnupg",
63+
}),
64+
);
65+
});
66+
67+
it("blocks canonical OS-home aliases for credential paths", () => {
68+
if (process.platform === "win32") {
69+
return;
70+
}
71+
72+
const dir = mkdtempSync(join(tmpdir(), "openclaw-home-"));
73+
const realHome = join(dir, "real-home");
74+
const aliasHome = join(dir, "alias-home");
75+
mkdirSync(join(realHome, ".ssh"), { recursive: true });
76+
symlinkSync(realHome, aliasHome);
77+
vi.stubEnv("HOME", aliasHome);
78+
79+
expect(getBlockedBindReason(`${join(realHome, ".ssh", "config")}:/mnt/ssh:ro`)).toEqual(
80+
expect.objectContaining({
81+
kind: "targets",
82+
blockedPath: normalizePathForSnapshot(join(realHome, ".ssh")),
83+
}),
84+
);
85+
});
2986
});
3087

3188
describe("validateBindMounts", () => {
89+
afterEach(() => {
90+
vi.unstubAllEnvs();
91+
});
92+
3293
it("allows legitimate project directory mounts", () => {
3394
expect(() =>
3495
validateBindMounts([
@@ -102,6 +163,32 @@ describe("validateBindMounts", () => {
102163
expect(() => validateBindMounts(["/var:/var"])).not.toThrow();
103164
});
104165

166+
it("blocks sensitive home credential binds", () => {
167+
vi.stubEnv("HOME", "/home/tester");
168+
169+
expect(() => validateBindMounts(["/home/tester/.docker/config.json:/mnt/docker:ro"])).toThrow(
170+
/blocked path/,
171+
);
172+
expect(() => validateBindMounts(["/home/tester/.netrc:/mnt/netrc:ro"])).toThrow(/blocked path/);
173+
});
174+
175+
it("blocks credential binds through canonical home aliases", () => {
176+
if (process.platform === "win32") {
177+
return;
178+
}
179+
180+
const dir = mkdtempSync(join(tmpdir(), "openclaw-home-"));
181+
const realHome = join(dir, "real-home");
182+
const aliasHome = join(dir, "alias-home");
183+
mkdirSync(join(realHome, ".docker"), { recursive: true });
184+
symlinkSync(realHome, aliasHome);
185+
vi.stubEnv("HOME", aliasHome);
186+
187+
expect(() =>
188+
validateBindMounts([`${join(realHome, ".docker", "config.json")}:/mnt/docker:ro`]),
189+
).toThrow(/credential paths/);
190+
});
191+
105192
it("blocks symlink escapes into blocked directories", () => {
106193
if (process.platform === "win32") {
107194
// Symlinks to non-existent targets like /etc require
@@ -210,6 +297,10 @@ describe("validateBindMounts", () => {
210297
});
211298
});
212299

300+
function normalizePathForSnapshot(input: string): string {
301+
return input.replaceAll("\\", "/");
302+
}
303+
213304
describe("validateNetworkMode", () => {
214305
it("allows bridge/none/custom/undefined", () => {
215306
expect(() => validateNetworkMode("bridge")).not.toThrow();

‎src/agents/sandbox/validate-sandbox-security.ts‎

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* Enforced at runtime when creating sandbox containers.
66
*/
77

8+
import os from "node:os";
9+
import path from "node:path";
10+
import { resolveRequiredHomeDir, resolveRequiredOsHomeDir } from "../../infra/home-dir.js";
811
import { splitSandboxBindSpec } from "./bind-spec.js";
912
import { SANDBOX_AGENT_WORKSPACE_MOUNT } from "./constants.js";
1013
import {
@@ -32,6 +35,17 @@ export const BLOCKED_HOST_PATHS = [
3235
"/run/docker.sock",
3336
];
3437

38+
const BLOCKED_HOME_SUBPATHS = [
39+
".aws",
40+
".cargo",
41+
".config",
42+
".docker",
43+
".gnupg",
44+
".netrc",
45+
".npm",
46+
".ssh",
47+
] as const;
48+
3549
const BLOCKED_SECCOMP_PROFILES = new Set(["unconfined"]);
3650
const BLOCKED_APPARMOR_PROFILES = new Set(["unconfined"]);
3751
const RESERVED_CONTAINER_TARGET_PATHS = ["/workspace", SANDBOX_AGENT_WORKSPACE_MOUNT];
@@ -100,14 +114,17 @@ export function getBlockedBindReason(bind: string): BlockedBindReason | null {
100114
}
101115

102116
const normalized = normalizeHostPath(sourceRaw);
103-
return getBlockedReasonForSourcePath(normalized);
117+
return getBlockedReasonForSourcePath(normalized, getBlockedHostPaths());
104118
}
105119

106-
export function getBlockedReasonForSourcePath(sourceNormalized: string): BlockedBindReason | null {
120+
export function getBlockedReasonForSourcePath(
121+
sourceNormalized: string,
122+
blockedHostPaths: string[],
123+
): BlockedBindReason | null {
107124
if (sourceNormalized === "/") {
108125
return { kind: "covers", blockedPath: "/" };
109126
}
110-
for (const blocked of BLOCKED_HOST_PATHS) {
127+
for (const blocked of blockedHostPaths) {
111128
if (sourceNormalized === blocked || sourceNormalized.startsWith(blocked + "/")) {
112129
return { kind: "targets", blockedPath: blocked };
113130
}
@@ -116,6 +133,34 @@ export function getBlockedReasonForSourcePath(sourceNormalized: string): Blocked
116133
return null;
117134
}
118135

136+
function getBlockedHostPaths(): string[] {
137+
const blocked = new Set(BLOCKED_HOST_PATHS.map(normalizeHostPath));
138+
for (const home of getBlockedHomeRoots()) {
139+
for (const suffix of BLOCKED_HOME_SUBPATHS) {
140+
blocked.add(normalizeHostPath(path.posix.join(home, suffix)));
141+
}
142+
}
143+
return [...blocked];
144+
}
145+
146+
function getBlockedHomeRoots(): string[] {
147+
const roots = new Set<string>();
148+
for (const candidate of [
149+
resolveRequiredHomeDir(process.env, os.homedir),
150+
resolveRequiredOsHomeDir(process.env, os.homedir),
151+
]) {
152+
const normalized = normalizeHostPath(candidate);
153+
if (normalized !== "/") {
154+
roots.add(normalized);
155+
}
156+
const canonical = resolveSandboxHostPathViaExistingAncestor(normalized);
157+
if (canonical !== "/") {
158+
roots.add(canonical);
159+
}
160+
}
161+
return [...roots];
162+
}
163+
119164
function normalizeAllowedRoots(roots: string[] | undefined): string[] {
120165
if (!roots?.length) {
121166
return [];
@@ -183,9 +228,10 @@ function enforceSourcePathPolicy(params: {
183228
bind: string;
184229
sourcePath: string;
185230
allowedRoots: string[];
231+
blockedHostPaths: string[];
186232
allowSourcesOutsideAllowedRoots: boolean;
187233
}): void {
188-
const blockedReason = getBlockedReasonForSourcePath(params.sourcePath);
234+
const blockedReason = getBlockedReasonForSourcePath(params.sourcePath, params.blockedHostPaths);
189235
if (blockedReason) {
190236
throw formatBindBlockedError({ bind: params.bind, reason: blockedReason });
191237
}
@@ -221,7 +267,7 @@ function formatBindBlockedError(params: { bind: string; reason: BlockedBindReaso
221267
const verb = params.reason.kind === "covers" ? "covers" : "targets";
222268
return new Error(
223269
`Sandbox security: bind mount "${params.bind}" ${verb} blocked path "${params.reason.blockedPath}". ` +
224-
"Mounting system directories (or Docker socket paths) into sandbox containers is not allowed. " +
270+
"Mounting system directories, credential paths, or Docker socket paths into sandbox containers is not allowed. " +
225271
"Use project-specific paths instead (e.g. /home/user/myproject).",
226272
);
227273
}
@@ -240,6 +286,7 @@ export function validateBindMounts(
240286
}
241287

242288
const allowedRoots = normalizeAllowedRoots(options?.allowedSourceRoots);
289+
const blockedHostPaths = getBlockedHostPaths();
243290

244291
for (const rawBind of binds) {
245292
const bind = rawBind.trim();
@@ -266,6 +313,7 @@ export function validateBindMounts(
266313
bind,
267314
sourcePath: sourceNormalized,
268315
allowedRoots,
316+
blockedHostPaths,
269317
allowSourcesOutsideAllowedRoots: options?.allowSourcesOutsideAllowedRoots === true,
270318
});
271319

@@ -275,6 +323,7 @@ export function validateBindMounts(
275323
bind,
276324
sourcePath: sourceCanonical,
277325
allowedRoots,
326+
blockedHostPaths,
278327
allowSourcesOutsideAllowedRoots: options?.allowSourcesOutsideAllowedRoots === true,
279328
});
280329
}

0 commit comments

Comments
 (0)