Skip to content

fix(sandbox): propagate docker.user to all exec commands - #23112

Closed
dashed wants to merge 1 commit into
openclaw:mainfrom
dashed:alberto/sandbox-fs-bridge-user
Closed

dashed wants to merge 1 commit into
openclaw:mainfrom
dashed:alberto/sandbox-fs-bridge-user

Conversation

@dashed

@dashed dashed commented Feb 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #20979 (partially — addresses the propagation gap; see #20991 for the fallback default).

When agents.defaults.sandbox.docker.user is configured, the value was only used by buildSandboxCreateArgs for the initial docker create. All subsequent docker exec calls — FS bridge (Read/Write/Edit tools), bash exec runtime, pi-tools sandbox config, and the container setup command — ran as root (no -u flag). With capDrop: ["ALL"], root lacks DAC_OVERRIDE and gets "Permission denied" on the workspace directory owned by the configured user.

This PR propagates docker.user through all exec paths so every docker exec includes -u <user>.

Root Cause

docker.user was partially wired: buildSandboxCreateArgs passed --user to docker create, but four other exec call sites constructed their own docker exec args without consulting docker.user:

  1. fs-bridge.ts — Read/Write/Edit/apply_patch tools
  2. bash-tools.exec-runtime.ts — Bash tool runtime
  3. bash-tools.shared.ts (buildDockerExecArgs) — shared exec arg builder
  4. docker.ts (createSandboxContainer) — setup command

Changes

src/agents/bash-tools.shared.ts — Add user?: string to buildDockerExecArgs params and insert -u flag:

 export function buildDockerExecArgs(params: {
   ...
   tty: boolean;
+  user?: string;
 }) {
   const args = ["exec", "-i"];
+  if (params.user) {
+    args.push("-u", params.user);
+  }
   if (params.tty) {

src/agents/bash-tools.exec-runtime.ts — Pass sandbox.user through:

             tty: opts.usePty,
+            user: opts.sandbox.user,

src/agents/sandbox/fs-bridge.ts — Insert -u before container name:

-    const dockerArgs = ["exec", "-i", this.sandbox.containerName, "sh", "-c", script, ...];
+    const dockerArgs = ["exec", "-i"];
+    if (this.sandbox.docker.user) {
+      dockerArgs.push("-u", this.sandbox.docker.user);
+    }
+    dockerArgs.push(this.sandbox.containerName, "sh", "-c", script, ...);

src/agents/sandbox/docker.ts — Add -u to setup command exec:

-    await execDocker(["exec", "-i", name, "sh", "-lc", cfg.setupCommand]);
+    const setupArgs = ["exec", "-i"];
+    if (cfg.user) {
+      setupArgs.push("-u", cfg.user);
+    }
+    setupArgs.push(name, "sh", "-lc", cfg.setupCommand);
+    await execDocker(setupArgs);

src/agents/pi-tools.ts — Pass docker.user to BashSandboxConfig:

           env: sandbox.docker.env,
+          user: sandbox.docker.user,

src/agents/bash-tools.shared.ts (type) — Add user to BashSandboxConfig:

 export type BashSandboxConfig = {
   ...
   env?: Record<string, string>;
+  user?: string;
 };

Relationship to #20991

This PR and #20991 are complementary:

Aspect This PR PR #20991
Problem docker.user configured but not propagated to exec docker.user not configured at all
Fix Propagate configured user to all exec paths Fall back to gateway UID:GID
Scope 5 source files + 2 test files Different files (auto-detection logic)

Both are needed: #20991 provides a sensible default when no user is configured, this PR ensures the configured value actually reaches all exec commands.

Test Plan

  • 6 new unit tests for buildDockerExecArgs user flag (bash-tools.shared.test.ts)
  • 3 new unit tests for buildSandboxCreateArgs user flag (docker.test.ts)
  • All existing sandbox tests pass (no regressions)
  • oxlint — 0 errors
  • oxfmt — clean
  • tsgo — clean

Fixes #20979
Related: #20991, #17941, #16379

Greptile Summary

This PR systematically propagates the configured agents.defaults.sandbox.docker.user value to all docker exec commands, fixing a critical permission issue when capDrop: ["ALL"] is set.

Key changes:

  • Modified buildDockerExecArgs in bash-tools.shared.ts to accept and insert -u flag
  • Updated all four exec call sites (fs-bridge, bash exec runtime, pi-tools config, setup command) to pass docker.user
  • Added comprehensive unit tests for both buildDockerExecArgs and buildSandboxCreateArgs

Impact:

The fix resolves permission denied errors that occurred because root (default exec user) lacks DAC_OVERRIDE capability when capDrop: ["ALL"] is configured, preventing access to workspace directories owned by the configured user. All docker exec calls now run as the same user specified in the container creation.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation is thorough, well-tested, and addresses a clear security/permission bug. All four exec call sites have been systematically updated with the same pattern. The changes are type-safe (optional user?: string), backward-compatible (gracefully handles undefined/empty values), and include comprehensive unit tests covering edge cases (undefined, empty string, flag ordering). No breaking changes or risky refactoring.
  • No files require special attention

Last reviewed commit: cf9a7db

@dashed

dashed commented Feb 22, 2026

Copy link
Copy Markdown
Contributor Author

Note on Windows CI failure: The `checks-windows (node, test)` failure is a pre-existing issue on main — not caused by this PR. Fix: #23128

…ssion denied

When capDrop: ['ALL'] is used, root lacks DAC_OVERRIDE. The workspace
directory (/workspace) is owned by uid 1000 with mode 0700, so docker
exec as root gets 'Permission denied'.

Propagate the docker.user field through all exec paths:
- fs-bridge.ts: add -u flag to runCommand docker exec
- bash-tools.shared.ts: add user param to buildDockerExecArgs
- bash-tools.exec-runtime.ts: pass sandbox.user to buildDockerExecArgs
- pi-tools.ts: pass sandbox.docker.user to sandbox config
- docker.ts: add -u flag to setupCommand exec
@dashed
dashed force-pushed the alberto/sandbox-fs-bridge-user branch from 2cb5c2e to 91793d8 Compare February 22, 2026 05:20
@steipete

Copy link
Copy Markdown
Contributor

Closing as AI-assisted stale-fix triage.

Linked issue #20979 ("[Bug] Default sandbox config breaks file tools for non-root gateway users (post-#4026)") is currently CLOSED and was closed on 2026-02-22T22:34:02Z with state reason COMPLETED.
Given that issue state, this fix PR is no longer needed in the active queue and is being closed as stale.

If the underlying bug is still reproducible on current main, please reopen this PR (or open a new focused fix PR) and reference both #20979 and #23112 for fast re-triage.

@steipete

Copy link
Copy Markdown
Contributor

Closed after AI-assisted stale-fix triage (closed issue duplicate/stale fix).

@steipete steipete closed this Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Default sandbox config breaks file tools for non-root gateway users (post-#4026)

2 participants