Skip to content

fix(security): a sandbox workspace can never expose the container engine socket or the user's runtime directory and its session bus #1115

Closed

Description

@dan-petty

Problem

Lines cited at dcafadf. At 132ecc5, ai/mcp/server.py:1317-1330 is at :1340-1353 and :1365-1381 is at :1388-1404; the other cited lines have not moved. Paths are under src/devops_cli/ unless they start with tests/ or .devcontainer/. Paths marked docker-py are in the locked docker 7.2.0 (pyproject.toml:24).

Three commands mount a host directory as a sandbox's /workspace:

  • devops docker sandbox and devops test, through WorkloadSandboxRunner._validate_workspace_dir (docker/sandbox.py:192-214, commands/test_cmd.py:374);
  • devops sandbox deploy, through WorkloadSandboxEngine.validate_workspace_dir (sandbox/engine.py:191-198).

The MCP tools docker_sandbox and sandbox_deploy run the first and the last with a workspace chosen by the MCP client (ai/mcp/server.py:1317-1330, :1365-1381). They check it only for a leading hyphen (:98-104). Neither workspace check keeps engine or session sockets out of the sandbox:

  • A substring test stands in for the engine-socket check. Both checks refuse a workspace only when its path contains the text docker.sock (docker/sandbox.py:210, sandbox/engine.py:161). A workspace that is the directory holding the socket, such as /run, passes, and so does every Podman socket (podman.sock).
  • No runtime directory is protected.
    • /run is not a forbidden root (config/constants.py:202-210, core/paths.py:99-103, sandbox/engine.py:51-62, docker/sandbox.py:179-190), and nothing reads XDG_RUNTIME_DIR. /var/run is a symlink to /run on current distributions.
    • Engine sockets live there: rootful Docker's /run/docker.sock, rootful Podman's /run/podman/podman.sock, and the rootless sockets under $XDG_RUNTIME_DIR (docker.sock, podman/podman.sock).
    • Measured in the devcontainer: /run/user/1000 holds the session bus (bus), gpg-agent's socket (gnupg/S.gpg-agent) and gnome-keyring's control socket (keyring/control). DBUS_SESSION_BUS_ADDRESS is unix:path=/run/user/1000/bus (.devcontainer/devcontainer.json:46), and gnome-keyring on that bus holds the devcontainer's real secrets (tests/conftest.py:306).
    • XDG_RUNTIME_DIR need not sit under /run. VS Code points it under /tmp (tests/test_devcontainer.py:1511); measured in the devcontainer as /tmp/user/1000, which holds VS Code's vscode-ipc-*.sock and vscode-git-*.sock sockets. So a workspace of /tmp mounts them.
  • The two checks differ.
    • docker/sandbox.py:197 compares the resolved path with its root list by exact match. /etc/ssl, or a /var/run that is a real directory, passes devops docker sandbox and devops test.
    • sandbox/engine.py:115-128 also refuses subpaths, through is_forbidden_system_path.
    • Only the deploy path runs the traversal check (sandbox/engine.py:96-112).
  • A read-only bind mount still allows connect() on a socket.

Who can open a mounted socket (read from the code and the engines' user mapping; not measured):

  • Rootful Docker, as in the devcontainer, whose docker-in-docker engine runs inside it.
    • --rootless is the default and the only mode the MCP tools use. It runs the sandbox as the host user's uid:gid (docker/sandbox.py:260-264, sandbox/engine.py:80-84).
    • A workspace of /run/user/<uid> or $XDG_RUNTIME_DIR therefore gives the sandbox the user's session bus (and gnome-keyring through it), gpg-agent and VS Code's sockets.
  • Rootful Docker with --root (devops docker sandbox, devops test). The sandbox runs as UID 0 with all capabilities dropped (sandbox/models.py:368-369). UID 0 still owns root-owned sockets, so a workspace of /run gives it the engine socket, which controls the host.
  • Rootless Podman or rootless Docker with --root. Container root is the host user, so a workspace of $XDG_RUNTIME_DIR gives it the user's own engine socket and session bus.
  • Rootless engines with the default mapping. The sandbox runs as a subordinate UID, which cannot enter the user's 0700 runtime directory. feat(docker): container commands find a Podman engine before Docker's default socket, and rootless sandboxes run as the host user #1117 changes that mapping so that a rootless sandbox writes its workspace as the host user. That change makes these sockets reachable, so this fix lands first.

Acceptance criteria

  • One check. devops docker sandbox, devops test and devops sandbox deploy validate the workspace through one shared function. Each command keeps its own error type and exit code.
  • Refused paths. A test parametrised over both runners refuses each of these before any engine call (the client mock records no call):
    • /run, /run/user/1000, /run/user/1000/gnupg and /var/run;
    • with XDG_RUNTIME_DIR set to a directory rt made by tmp_path_factory: rt itself, a directory under it, and the directory that holds it;
    • with DOCKER_HOST=unix://<d>/engine/docker.sock, where <d> is under tmp_path: <d>/engine, a directory under it, and <d>;
    • /etc/ssl under devops docker sandbox, as devops sandbox deploy already refuses it.
  • Accepted paths. A tmp_path workspace beside those directories is accepted:
    • with DOCKER_HOST unset;
    • with DOCKER_HOST=tcp://192.0.2.1:2375 and private networks allowed (the suite's default, tests/conftest.py:408). A tcp endpoint names no socket.
  • The refusal names the workspace and the protected directory.
  • MCP. The docker_sandbox and sandbox_deploy tools run the shared check on workspace before building the command line. With workspace="/run/user/1000", each raises the check's error and _run_mcp_cmd is not called.
  • No substring test is left. rg -n 'docker\.sock' src/devops_cli/docker/sandbox.py src/devops_cli/sandbox/engine.py prints nothing.
  • The shared constant is unchanged. CONST_FORBIDDEN_SYSTEM_DIRS keeps its entries, and _validate_subprocess_cwd (core/process.py:202) still accepts a directory under /run.
  • Test isolation.
    • Today the autouse isolate_session_bus fixture sets XDG_RUNTIME_DIR to tmp_path (tests/conftest.py:304-308), and most sandbox tests use tmp_path as the workspace (for example tests/test_workload_sandbox.py:55-58). The fixture moves XDG_RUNTIME_DIR to tmp_path_factory.mktemp("run"), which neither holds nor sits inside tmp_path.
    • An autouse fixture unsets DOCKER_HOST, so the check never reads a developer's own endpoint or runs the egress check on it. Tests that need the variable set it themselves.
    • With these two changes, the existing sandbox and MCP tests pass unchanged.
  • Release.
    • A changelog.d fragment lists the workspaces that are now refused.
    • Tests are offline, start no container and build no real DockerClient. The new tests take well under 1 s together, and uv run devops ci passes.
  • Pending a person: in the devcontainer, on a build that includes this fix, run devops docker sandbox --workspace /run/user/$(id -u) -- true, the same with --workspace "$XDG_RUNTIME_DIR", and the MCP docker_sandbox tool with workspace set to /run/user/<uid>. Confirm that each is refused before any container is created, and record the results in the task file.
  • Pending a person: for the record, on the release before this fix, run a sandbox with workspace /run/user/$(id -u) and the command python3 -c 'import socket; socket.socket(socket.AF_UNIX).connect("/workspace/bus")'. Record whether the connect succeeds. It only connects and sends no bus message.

Key questions

  • Which directories does the check protect? Proposed: three. The workspace is refused when it is, holds or sits under any of them. Paths are compared with Path.resolve() and Path.is_relative_to:

    • /run. /var/run resolves to it where it is a symlink, and falls under the /var root where it is a real directory.
    • $XDG_RUNTIME_DIR, when it is set to an absolute path.
    • The directory of the engine's unix socket.

    /run/user/<uid>, the runtime directory used when the variable is unset, lies under /run.

  • Why also refuse a workspace under a protected directory? Proposed: one rule for all three directories is simpler to state and test. Subdirectories hold sockets of their own: gnupg/S.gpg-agent and keyring/control (measured in the devcontainer), and podman/podman.sock.

  • Where does the check get the engine socket? Proposed: from the engine's own endpoint resolution, without connecting.

  • Why not add /run to CONST_FORBIDDEN_SYSTEM_DIRS? Proposed: the sandbox check adds it to its own root list instead.

    • The constant reaches 19 other call sites in 17 modules through is_forbidden_system_path. Among them are subprocess cwd checks (core/process.py:202), the output writer, settings discovery and devcontainer mount parsing (commands/devcontainer.py:432).
    • Refusing /run there changes behaviour this fix does not need to change.
  • Does the check follow a session bus at a custom path? Proposed: no; that is out of scope.

    • The bus in the devcontainer and under a systemd user session is at /run/user/<uid>/bus, which is covered.
    • Following DBUS_SESSION_BUS_ADDRESS would need either a new direct dependency for D-Bus address parsing or a hand-written parser. The existing prefix test (commands/devcontainer.py:1265-1270) is not to be reused.
    • An abstract-socket bus is not on the filesystem. Abstract sockets are scoped to a network namespace, and every sandbox network mode has its own (sandbox/models.py:52-59 offers no host mode).
  • Why check in the MCP tools when the commands check too? Proposed: the tool refuses before it builds a command line, so the client gets the refusal as a tool error and no command runs. The check is the same function, so there is no second rule set.

Strategic alignment

Update 2026-10-08: delivered scope

Delivered on fix/1115-sandbox-workspace-runtime-dirs, branched from release/v0.2.29 at 910b823. Where the delivered scope differs from the body above:

  • Engine endpoint without the egress check. The body proposed calling DockerEngineService.resolve_host. That runs the egress check, which looks up a tcp host's DNS when private networks are disallowed. devops sandbox deploy --dry-run validates the workspace, so that design would have added a request to a dry run. Instead, resolve_host is split:
    • configured_host() reads DOCKER_HOST or the default socket, with no check, and the workspace check uses it.
    • resolve_host() builds on it and keeps the egress check for client().
    • So the body's statement that the tcp egress check "now runs before the workspace is accepted" no longer holds: a tcp endpoint names no socket and is not checked here.
  • Which socket is followed. The check follows the socket that DOCKER_HOST names, or the default /var/run/docker.sock. A socket chosen through a Docker context is followed once fix(docker): the Docker endpoint check covers the endpoint the SDK dials, including contexts and the TUI, and pushes accept registry ports #1108 extends configured_host().
  • Socket path from docker-py's adapter, not urlsplit. DockerEngineService.unix_socket_path() uses parse_host and then UnixHTTPAdapter(base_url).socket_path. urlsplit(...).path disagrees with docker-py on the legacy unix://tmp/engine/docker.sock form, which is among the tested cases.
  • Symlinked socket. When the socket file is itself a symlink, as Docker Desktop's WSL integration makes /var/run/docker.sock, the check protects both the directory that holds the link and the directory the link leads to. A tested case covers this for both runners.
  • Unparseable endpoint. A DOCKER_HOST that docker-py cannot parse refuses every workspace with SandboxValidationError. devops docker sandbox and devops test sandbox re-raise it as DockerSandboxError. This covers both an unsupported scheme such as fd:// and a malformed endpoint such as tcp://host:abc, for all three commands and both MCP tools.
  • Refusal message. The new refusal names both the workspace and the protected directory. /var/run is still refused first by the symlink rule, and /etc/ssl by the system-directory rule.
  • Command name. devops test in the body is devops test sandbox.
  • Test changes. "Existing tests pass unchanged" did not hold:
    • the two blocks that asserted the old docker.sock substring refusal now refuse the workspace that holds the DOCKER_HOST socket;
    • the two MCP tests that used the default workspace . now pass tmp_path, because a runner's checkout lies under /home, which sandbox.exclude_home_dir refuses;
    • a dry-run test shows devops sandbox deploy --dry-run with a tcp endpoint makes no host lookup.
  • Also removed. The duplicated _FORBIDDEN_ROOTS sets, which is_forbidden_system_path already covers.
  • Behaviour change. devops docker sandbox and devops test sandbox now also refuse .. workspaces and system subpaths such as /etc/ssl, as devops sandbox deploy already did. The changelog says so.
  • Follow-ups. fix(cli): docker sandbox and test sandbox dry runs check the workspace as the real run does #1383: the two other dry runs still skip the workspace check. fix(security): a sandbox workspace never holds the user's ssh agent or X11 socket #1384: the SSH agent and X11 sockets.
  • Person steps. Both "Pending a person" checks run after merge, and their results are recorded on this issue.

Activity

  1. added
    type/security
    scope/security
    priority/p1-high
    on Oct 3, 2026
  2. added this to the v0.2.29 milestone on Oct 8, 2026
  3. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Moved to v0.2.29: v0.2.29 started with fewer than its 12 items and was topped up to 12 from Ready items, critical fixes and P0 features first. (devops roadmap reprioritize)

  4. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    What changed

    #1385, merged into release/v0.2.29 as e3fff79.

    Closes #1115

    • One check: validate_sandbox_workspace is shared by devops docker sandbox, devops test sandbox, devops sandbox deploy and both MCP tools, and each command keeps its own error type. It refuses a workspace that is, or holds, the directory of the engine socket that DOCKER_HOST (or the default) names. When the socket file is a symlink, as with Docker Desktop's WSL integration, it also refuses the directory the link leads to. It refuses the user's runtime directory (XDG_RUNTIME_DIR, with its session bus) too.
    • Request-free: the socket path comes from docker-py's own parsing (parse_host and UnixHTTPAdapter.socket_path) through a new configured_host(), with no egress lookup. So devops sandbox deploy --dry-run stays request-free.
    • Unparseable endpoints (fd://, tcp://host:abc) refuse every workspace instead of escaping as a raw error.
    • Behaviour change: the docker and test sandboxes now also refuse .. and system subpaths such as /etc/ssl, as deploy already did.
    • Docker contexts are followed once fix(docker): the Docker endpoint check covers the endpoint the SDK dials, including contexts and the TUI, and pushes accept registry ports #1108 extends configured_host().

    The tests cover 27 refused and 4 accepted workspaces for both runners and the MCP tools. They are offline, and the slowest new call phase is 0.37 s. uv run devops ci passes locally (3m 59s).

    Follow-ups found while building this: #1383 (the other dry runs skip the check), #1384 (the SSH agent and X11 sockets).

    12 file(s) changed.

    How it was verified

    Check runs at 8ce4fce:

    • Prune PR DevContainer Images: pass
    • CodeQL: pass
    • Static Analysis: pass
    • Tests & Coverage: pass
    • Analyze (actions): pass
    • Analyze (python): pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-1115-sandbox-workspace-never-exposes-engine-socket-or-runtime-dir.md:

    • One check. The three commands validate the workspace through validate_sandbox_workspace. devops docker sandbox and devops test sandbox still raise DockerSandboxError, and devops sandbox deploy still raises SandboxValidationError.
    • Refused paths. test_a_workspace_overlapping_a_socket_directory_is_refused_before_any_engine_call (tests/test_sandbox_lifecycle.py), parametrised over both runners, refuses each case before any engine call, and the client mock records no call. The cases are /run, /run/user/1000, /run/user/1000/gnupg and /var/run; XDG_RUNTIME_DIR itself, a directory under it and the directory that holds it; the DOCKER_HOST socket directory, a directory under it and the directory that holds it, in both the unix:///abs and the unix://rel form; the directory a symlinked DOCKER_HOST socket leads to; fd:// and tcp://host:abc; and /etc/ssl.
    • Accepted paths. A tmp_path workspace is mounted by both runners with DOCKER_HOST unset and with DOCKER_HOST=tcp://192.0.2.1:2375.
    • The refusal names the workspace and the protected directory. This holds for every runtime-directory and socket-directory refusal. /var/run is refused by the symlink rule and /etc/ssl by the system-directory rule, and each of those messages names the workspace.
    • MCP. With workspace="/run/user/1000", or with DOCKER_HOST set to fd:// or tcp://host:abc, each tool raises SandboxValidationError and _run_mcp_cmd is not called.
    • No substring test is left. rg -n 'docker\.sock' src/devops_cli/docker/sandbox.py src/devops_cli/sandbox/engine.py prints nothing.
    • The shared constant is unchanged. CONST_FORBIDDEN_SYSTEM_DIRS keeps its entries, and run_subprocess(["pwd"], cwd=Path("/run")) still runs (tests/test_process.py).
    • Dry run makes no request. devops sandbox deploy --dry-run with DOCKER_HOST=tcp://example.com:2375 and private networks disallowed exits 0 without a host lookup.
    • Test isolation. The two docker.sock substring blocks (tests/test_sandbox_lifecycle.py, tests/test_security_sandbox_and_protection.py) now refuse the workspace that holds the DOCKER_HOST socket. The two MCP tests that used the default workspace . pass workspace=str(tmp_path), because a GitHub runner's checkout lies under /home. Every other sandbox and MCP test passes unchanged.
    • Release. changelog.d/1115.md lists the workspaces that are now refused. The tests are offline, start no container, build no real DockerClient and each run well under 1 s.
    • Not done here: a socket chosen through a Docker context. The check follows the socket DOCKER_HOST, or the default, names; fix(docker): the Docker endpoint check covers the endpoint the SDK dials, including contexts and the TUI, and pushes accept registry ports #1108 extends configured_host() to Docker contexts, and the check follows it then.
    • Pending a person: in the devcontainer, on a build that includes this fix, run devops docker sandbox --workspace /run/user/$(id -u) -- true, then devops docker sandbox --workspace "$XDG_RUNTIME_DIR" -- true, and the MCP docker_sandbox tool with workspace set to /run/user/<uid>. Confirm that each is refused before any container is created, and record the results on the issue.
    • Pending a person: for the record, on the release before this fix, run a sandbox with workspace /run/user/$(id -u) and the command python3 -c 'import socket; socket.socket(socket.AF_UNIX).connect("/workspace/bus")'. Record on the issue whether the connect succeeds. It only connects and sends no bus message.
  5. added a commit that references this issue on Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/securitytype/security

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions