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
- After a successful run the service rsyncs
public/to the web server:-rt --delete --no-links --chmod=D755,F644 --max-size=..., plus a total size cap.--no-linksmatters: a symlink in the workspace must not publish a host file. - Serve it on its own origin (
<slug>.ai.example.com), never a path on an origin that also hosts logged-in apps, or agent-written JavaScript becomes XSS against those apps. - Static only:
SetHandler noneso PHP is served as text, no cookies, CSP andnosniffheaders. On the TLDR server this is a wildcard Apache vhost withVirtualDocumentRoot(one directory per slug) behind nginx; vhost sources are in claude-servicedeploy/ai-guest/. - Because of
--delete, anything added to a guest site by hand must go into the workspacepublic/, or the guest's next run removes it.
Testing a sandbox
- Probe with scripts, not adversarial prompts: the model refuses most "try to escape" requests,
which proves nothing. Replace
claudewith a shell to test the outer layer, then have the agent run a probe script through Bash (inner layer), then exercise Read/Write directly. - Check: no host home directory, no credentials file, token pipe already empty,
/proc/*/memdenied, config dir hidden or read-only,workspace/.clauderead-only, DNS and raw sockets fail from Bash, init lists no MCP servers, file tools confined to the workspace, and the main credentials file unchanged afterward (back it up first). - End to end, send a guest post carrying every forbidden option (danger tag, raw flags, a restricted model, a real project, a huge timeout) and confirm it ran with the guest project, no extra args, the default model and the capped timeout.
Gotchas
- Guest sessions live in the private config dir, so a service cleanup that only scans
~/.claude/projectsnever expires them; setcleanupPeriodDaysin the guest settings. - Moving an existing user into guest mode cuts their old threads off from the repos they ran in; each reply on an old thread starts a fresh session from the replayed conversation.
- The app may gate the AI composer on a separate access flag, so a guest can need both flags.
See also
- Permission modes in headless Claude Code (plan mode wedges, skip-permissions, tool scoping)
- Securing a shared AI agent that other users can drive
- claude-service (ProcrastinAI's webhook runner for headless Claude)