Skip to content

[BUG] sandbox: agent can write .git/ and .devcontainer/, which the host then runs #289

In review

Description

@gatezh

Summary

The sandbox variant bind-mounts the whole repository read-write at /workspace, including .git/ and .devcontainer/. Claude Code runs there with permission prompts skipped, so the agent can write files that the host later executes or trusts. The README's stated mitigation is that workspace edits "show in git diff" (claude-code/README.md:354, and :182 for the allowlist). That holds for tracked files, but not for anything under .git/, which git diff never shows.

Paths the sandbox agent can write, and what the host does with them:

Path Host effect Visible in git diff
.git/config (core.fsmonitor, core.hooksPath, core.sshCommand, aliases) Runs a command on the host's next git call. core.fsmonitor fires on git status, which VS Code runs by itself. No
.git/hooks/* Runs on the host's next commit, checkout, merge or push No
.devcontainer/devcontainer.json → initializeCommand Runs on the host at the next rebuild or reopen Yes, if someone looks before rebuilding
.devcontainer/claude-sandbox/allowed-domains.txt The firewall allows the added domains on the next container start Yes, if someone looks before restarting

This is the same class as #208 (workspace-writable firewall script), one level out: the firewall itself is now safe, but the host-side trust in the workspace is not.

Reproduction

Not run end to end. It follows from the mount, and each step is ordinary git or devcontainer behaviour.

  1. Open a project in the sandbox variant.
  2. In the container: git config core.fsmonitor 'touch /tmp/pwned-by-sandbox; false' (writes /workspace/.git/config).
  3. On the host, in the same repo: git status.
  4. /tmp/pwned-by-sandbox exists on the host. git diff in the repo shows nothing.

Verification

# the mount
claude-code/.devcontainer/claude-sandbox/docker-compose.yml:12
      - ../..:/workspace:cached

# prompts are skipped in the sandbox
claude-code/.devcontainer/claude-sandbox/devcontainer.json:100
        "claudeCode.allowDangerouslySkipPermissions": true

# the host runs this from a workspace file
claude-code/.devcontainer/claude-sandbox/devcontainer.json:12
  "initializeCommand": "docker pull ghcr.io/gatezh/devcontainers/claude-code-sandbox:latest || exit 0",

# the firewall reads its allowlist from the workspace
claude-code/.devcontainer/init-firewall.sh:14
ALLOWLIST=/workspace/.devcontainer/claude-sandbox/allowed-domains.txt

Suggested fix

Options, none tested:

  1. Mount .devcontainer read-only in the sandbox. Add - ../../.devcontainer:/workspace/.devcontainer:ro to the sandbox docker-compose.yml. Closes the initializeCommand and allowlist rows. Cost: devcontainer config can't be edited from inside the sandbox, which is arguably the point.
  2. Mount .git/config and .git/hooks read-only in the sandbox. Closes the two rows git diff can't see, and the agent can still commit. Costs: git config and git remote add fail in the sandbox, and the mount breaks when the workspace is a git worktree, where .git is a file, not a directory.
  3. Document the gap. Change README.md:354 to say that .git/ is shared and not covered by git diff, and tell users to check git config --local --list and .git/hooks/ after a sandbox session. Cheapest, but relies on the user.

Option 1 plus option 3 looks like the smallest change that doesn't break worktrees.

Affected files in this repo

  • claude-code/.devcontainer/claude-sandbox/docker-compose.yml:12
  • claude-code/.devcontainer/claude-sandbox/devcontainer.json:12
  • claude-code/.devcontainer/init-firewall.sh:14
  • claude-code/README.md:182, claude-code/README.md:354

Related

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions