📚 Encyclopedia

AI & Claude · updated 2026-10-26

Sandboxing Claude Code for untrusted users (bubblewrap, --restricted, token pipe)

claude-codesandboxbubblewrapbwrapsecuritymulti-tenantoauthprocrastinaiguest-mode

How to let someone you only partly trust run a headless Claude Code agent on your own machine, with your Claude login, without giving them your files, your credentials or your network. Built for TLDR's "AI guest mode" (October 2026): guest users' ProcrastinAI posts run in a private workspace, and whatever the agent puts in public/ is published as a static site on its own subdomain. Permission flags alone are not enough for this. The design uses two sandbox layers plus server-side rules that the guest cannot change.

Threat model

A guest controls the prompt and so, in effect, everything the agent does. Assume they will try to: read the host's home directory and the Claude credentials file; plant settings that loosen the next run; pass raw CLI flags (--dangerously-skip-permissions); reach the LAN or the internet from Bash; use MCP servers or claude.ai connectors attached to the main login; and use a long timeout or many runs to tie up a shared, sequential queue. The model's own refusals are not a control.

Layer 1: bubblewrap around the whole CLI

Run claude itself inside bwrap, so even the CLI process (and anything that escapes its tool rules) sees a minimal filesystem: - Mount only read-only /usr and /etc, the guest's workspace (bound at a neutral path like /home/guest/workspace), a private Claude config dir, and the claude binary. --tmpfs over /home, /tmp, /run/user, /var/tmp. - --unshare-pid --unshare-ipc --unshare-uts, --die-with-parent --new-session, --clearenv and set only the variables the run needs. - Read-only bind an empty dir over workspace/.claude. Otherwise the agent can write .claude/settings.json into its own workspace and loosen the next run. - Keep the outer process's environment empty too: /proc/1/environ of the bwrap process is readable from inside the sandbox, so nothing from the service's environment may be in it. - Network is left on at this layer only because the CLI must reach the Anthropic API.

Layer 2: Claude Code's own restrictions

Flags and a service-written settings file, mounted read-only: - --restricted, an explicit --tools list, --settings <ro file> (CLI-level settings outrank any settings file the agent could write), --strict-mcp-config --mcp-config '{"mcpServers":{}}' and ENABLE_CLAUDEAI_MCP_SERVERS=false so no MCP servers or connectors load. - Settings: permissions.deny for WebFetch, reads of the config dir and /proc; sandbox.enabled with allowUnsandboxedCommands: false, network.allowedDomains: [] (Bash gets no network), filesystem.allowWrite: [workspace]. The agent's Bash then runs in Claude Code's own nested sandbox. Also turn off auto memory and the auto-updater. - Ignore every caller-supplied CLI argument for guest runs. Never --dangerously-*.

The credential: pass an access token, never the refresh token

The run needs the host's Claude subscription, but the credentials file holds a refresh token, which would let a guest rotate (and lock out) the main login. Instead: - Read the main login's current access token and hand it in through a pipe: CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR=<fd>. The CLI reads it once; the pipe is drained before the agent's first tool call, and no file holds it. - When the access token is close to expiry, make the main CLI refresh itself (a one-turn, no-tools call outside the sandbox) and read the new token. Refreshing stays Claude Code's job.

Rules the server enforces (not the sandbox)

In TLDR these live in the API, which decides what runs; claude-service decides how: - A guest flag on the user that only an admin can set (self-service preferences cannot touch AI keys). An admin is never treated as a guest. - The project is forced to guest-<slug> on every turn, and a thread that previously ran in a real project gets its resume session cleared, so old threads do not keep repo access. - Model limited to non-restricted ones; no danger flag, no raw options, no effort or extra args; timeout capped (900 s). - A per-guest daily cap on user turns (25 by default, counted per local calendar day), so one guest cannot monopolize a sequential queue. Callback retries do not count; re-runs of a failed turn do. - The slug must match ^[a-z0-9][a-z0-9-]{0,30}$ or the run is refused; it becomes a hostname and a path.

Publishing what the agent builds

Testing a sandbox

Gotchas

See also

Linked from

Sources: tldr memory (feature-ai-guest-mode, audit-ai-permission-surface), claude-service CLAUDE.md "AI guest mode" + guest.py, post #1822