30 KiB
Agent Sandboxing
Run agent shell commands inside an isolated microVM instead of directly in the Manager container. Sandboxing is a hardening option for untrusted agent code, not a per-project permission boundary.
Overview
When sandboxing is enabled, every shell command an OpenCode agent runs through the bash tool is executed inside a microVM managed by msb instead of in the container that hosts the Manager and the OpenCode server. The agent still works with the same files in the same layout — the microVM sees the repositories through bind mounts — but the process runs under a different kernel and cannot read Manager or provider secrets from the host filesystem. OpenCode also exposes shell surfaces that bypass the bash tool hook; those are routed into the same microVM through a Manager-owned shell wrapper (see Host-Shell Surfaces Under Enforcement).
What Gets Sandboxed
Sandboxing covers both the OpenCode bash tool and every other path through which OpenCode's host process can start a shell. As a result:
| Execution path | Sandboxed |
|---|---|
Chat session bash tool calls |
Yes |
| Scheduled run commands | Yes |
Subagent bash tool calls |
Yes |
WebUI !command shell mode (POST /session/:id/shell) |
Yes |
Slash-command shell templates (!`cmd`, POST /session/:id/command) |
Yes |
| Skills and prompt-only slash commands | Yes (no shell to sandbox) |
PTY terminals (POST /pty, /pty/:id/connect) |
Blocked while enforced |
Local MCP server processes (mcp config with type: "local" / command) |
Disabled while enforced |
Dynamically added local MCP servers (POST /mcp) |
Blocked while enforced |
Formatter commands (formatter config) |
Disabled while enforced |
LSP server processes (lsp config, any enabling form) |
Disabled while enforced |
Experimental hook commands (experimental.hook config) |
Disabled while enforced |
Global custom tools (<config>/opencode/tool(s)/, $HOME/.opencode/tool(s)/) |
Quarantined while enforced |
Manager-side git operations (backend/src/services/repo.ts) |
No |
OpenCode read / write / edit file tools |
No |
Manager-side git operations (clone, fetch, worktree creation, credential setup) run directly in the container so the Manager can manage repositories regardless of the sandbox setting. OpenCode's file tools keep operating on the host filesystem — the same files the microVM sees through its bind mounts — so editing a file and running a command against it behave exactly as they do without sandboxing.
Host-Shell Surfaces Under Enforcement
The bash tool hook is not the only way OpenCode's host process can spawn a shell. The session-shell endpoint that powers the WebUI !command mode, slash-command shell templates (!`cmd` sequences expanded before the prompt is sent), and PTY sessions all resolve a shell through OpenCode's shell configuration rather than through tool.execute.before.
Rather than blocking those endpoints, the Manager redirects them into the same microVM. Enforcement already strips the shell key from every configuration source OpenCode reads, so OpenCode falls back to the SHELL environment variable of its own process — and the Manager stamps SHELL to a generated wrapper (<workspace>/.config/ocm/ocm-sandbox-shell, installed at every enforced start). The wrapper receives the command as -c <command>, asks the Manager's internal POST /api/internal/sandbox/command endpoint for a plan — the identical endpoint the bash tool hook uses — and executes the returned msb exec command. If the sandbox is unavailable for any reason it prints the reason and exits non-zero, so the surface fails closed instead of silently running on the host.
The wrapper's filename is deliberately not a recognized shell name, so OpenCode passes the command through verbatim instead of sourcing host startup files. It is installed outside every sandbox bind mount, so code running inside the microVM cannot modify it. A failure to install it aborts an enforced start.
PTY sessions remain blocked with 403: they require an interactive TTY that msb exec --no-tty cannot provide. The Manager's OpenCode proxy — both the authenticated /api/opencode/* surface and the internal-token /api/opencode-proxy surface — rejects PTY creation and connection with an actionable reason. The shared policy strictly canonicalizes request paths before matching: percent-encoded spellings of a blocked route (for example /%70ty) are blocked, and any path containing malformed, double-encoded, control-character, or encoded-separator sequences is refused as unsafe rather than forwarded. The same proxy surfaces refuse to dynamically add a local MCP server to the running process: POST /mcp is only forwarded while enforcement is on when the body is a provably remote server (type: "remote" with a URL) — a local (type: "local") or otherwise command-bearing config is rejected with 403, so neither surface can start an MCP process in the OpenCode host process. This boundary is enforced by the shared proxy policy in backend/src/services/opencode/proxy-policy.ts, which is covered by route tests that pin the blocked surface list.
!!! note "Slash-command shell templates run without a project working directory"
OpenCode expands !`cmd` sequences without setting a working directory, so the wrapper sees the OpenCode server's own directory rather than the session's project. That path is outside the sandbox mount roots, so such expansions fail closed with an explanatory message instead of running on the host. Skills and prompt-only slash commands contain no shell sequences and are unaffected.
!!! warning "Direct OpenCode port exposure bypasses the proxy boundary"
Enforcement is applied in two different places. The bash tool rewrite runs as a plugin hook (ocm-sandbox.js) inside the OpenCode process itself, and the shell wrapper is selected by the child process's own environment, so both still apply even when clients connect directly to the OpenCode server's own port. The blocked PTY surfaces and the config-mutation sanitization, by contrast, are applied by the Manager's OpenCode proxy. To keep the proxy authoritative, an enforced OpenCode server is always spawned with a loopback bind: when OPENCODE_HOST is not a loopback address, the Manager overrides it with 127.0.0.1 for the child (and logs the override), so callers cannot reach the PTY or unsanitized configuration endpoints on the OpenCode port directly — the only external path is the Manager proxy, which applies the enforcement policy. The Manager's own OpenCode client follows the same effective host (loopback while enforced, OPENCODE_HOST otherwise), so health checks and every proxied request keep targeting the address the child actually binds, including IPv6 loopback hosts. The override is active only while enforcement is on; with the toggle off, OPENCODE_HOST is honored as configured.
Host Requirements
Sandboxing requires KVM, which means a Linux host:
- The host must expose
/dev/kvmto the container. Start the Manager with the sandbox overlay:docker compose -f docker-compose.yml -f docker-compose.sandbox.yml up -d - Docker Desktop on macOS and Windows cannot provide
/dev/kvm. On those platforms the sandbox toggle stays disabled and the Settings UI shows the reason.
Because enforcement requires KVM, it is inherently limited to Linux. On non-Linux hosts the enforced code path fails closed, and unenforced production operation tracks the OpenCode server as a direct child instead of relying on /proc process identity attestation, so starting the Manager in production mode on macOS keeps working with the sandbox toggle off.
If the host meets the requirements, the Settings UI displays the detected msb version; otherwise it shows why sandboxing is unavailable.
Scope and Lifecycle
All projects share one microVM:
- The microVM is named
ocm-workspaceand is created detached (msb run -d), booted on the first sandboxed command, and reused for every subsequent command. - It mounts
/workspace/reposand/workspace/schedule-worktreesat identical guest paths. This covers every repository, every repository worktree, the assistant-mode project (repos/assistant), and every schedule worktree. - The assistant-mode
.opencodedirectory (repos/assistant/.opencode, which holds the internal API token plus the managed assistant skills and agents) is masked inside the microVM with a guest-memorytmpfsoverlay. Guest shell processes see an empty directory there and cannot read the token, while OpenCode's host-side config and skill loading keeps operating on the real directory. - Because the mount roots are the two parent directories, repositories cloned and schedule worktrees created after the microVM booted are visible inside it immediately — there is nothing to remount.
- Each command supplies its own working directory via
msb exec -w, socd-ing between projects behaves exactly as it does without sandboxing. - To tear the microVM down, remove it by label:
The next sandboxed command recreates it.
msb rm --force --label ocm.managed=true - A graceful Manager shutdown always attempts to stop any
ocm.managed=truemicroVM (msb stop --label ocm.managed=true, withignoreExitCodeand a finite timeout), independent of the current toggle state, so a VM is never left detached after a restart, a toggle change with a pending restart, or an earlier shutdown step failing. - Disabling sandboxing does not stop the microVM at toggle time: the stop runs when the OpenCode server restarts into the disabled state, so a background guest process cannot outlive an enforced-to-disabled restart. That stop is confirmed — it only completes once the managed microVM is listed as stopped or absent, and the restart fails closed if the VM cannot be confirmed stopped — and it is reversible: re-enabling sandboxing and restarting boots the microVM again.
- Before reusing an existing
ocm-workspacemicroVM, the Manager verifies it was created by the Manager: it must carry theocm.managed=truelabel, boot from the configuredSANDBOX_IMAGE, run with the configured CPUs/memory/workdir, keep networking enabled, mount exactly/workspace/reposand/workspace/schedule-worktreesas writable identical-path bind mounts, and carry exactly one tmpfs: the assistant.opencodemask. The effective network policy is also attested against the resolved policy of the configuredSANDBOX_NETprofile (public,private,host, or a comma-separated composition): the inspecteddefault_egress/default_ingressand every rule must match the profile's canonical policy exactly, so a sandbox labelled with the configured profile that actually carries an allow-all rule, a rule broadened to specific protocols or ports, stale rules from another profile, or altered defaults is recreated rather than reused. Any named volume, disk image, extra tmpfs (including one masking a project root), extra bind mount (for example one exposing/workspace/config), or missing mask fails the check. An instance that fails any of these checks is force-removed and recreated from the centralized create arguments, so an unmanaged or stale instance is never started or used. The attestation parses the realmsb inspect --format jsoncontract of the pinnedmsbCLI (MICROSANDBOX_VERSION), which is tested against fixtures copied from that contract.
A session rooted outside the two mounted roots (for example /workspace itself) is refused with a clear error rather than silently escaping the sandbox.
What Is Not Mounted
These host paths stay outside the microVM:
| Host path | Contents |
|---|---|
/workspace/config |
SSH known_hosts, ssh_config |
/workspace/.ssh-keys |
SSH private keys for repository access |
/workspace/.config |
OpenCode configuration and generated plugins |
/workspace/.opencode/state |
Provider credentials (auth.json) |
repos/assistant/.opencode (masked) |
Assistant internal API token — hidden inside the microVM behind a tmpfs overlay |
The consequence: sandboxed commands cannot read Manager or provider secrets, and they cannot modify the OpenCode configuration or the installed plugins. The assistant-mode .opencode directory is bind-visible on the host for OpenCode's own config and skill loading, but inside the microVM it is replaced by an empty in-memory filesystem, so the internal API bearer token it contains is unreadable by guest shell code.
Blast Radius
The microVM shares one kernel and one filesystem namespace across all projects. This boundary protects the Manager container and its secrets from agent code; it does not isolate one repository from another. Repositories remain mutually accessible inside the sandbox, exactly as they are on the host.
Enabling and Enforcement Model
Enforcement follows this chain:
- Toggle — Enable Sandbox in Settings. If
/dev/kvmis unavailable the toggle cannot be turned on and the capability reason is shown; if the runtime later becomes unavailable while the preference stays enabled, the toggle remains turned on and every sandboxed command fails closed with the reason instead of running on the host. - Restart — Changing the preference marks the OpenCode server restart as pending. The new enforcement value is only applied to newly spawned OpenCode processes, so a restart is required. When an enforced server starts, the Manager terminates the previous server's process group — the detached OpenCode child plus every host-process descendant it spawned, including a shell command or MCP process started while enforcement was off — waits for all members to exit, escalates to
SIGKILLon the group if needed, and refuses to start the enforced server if any member survives, so no unenforced host-executed process can outlive the transition. Non-detached development children are terminated by PID only, preserving the existing hot-reload behavior. - Stamp — On startup the Manager resolves the preference (not the runtime probe) and stamps every spawned OpenCode child environment with
OCM_SANDBOX_ENFORCED=truewhenever the preference is enabled. A missing or broken sandbox runtime therefore keeps enforcement active and blocks commands with the capability reason rather than silently running them on the host. If the preference itself cannot be read, the Manager fails closed the same way: it treats enforcement as active, terminates any attested predecessor and port-owning server so no unenforced child stays reachable, and aborts startup as non-recoverable with the settings error. User-defined environment variables cannot override this value. - OpenCode build — Sandbox enforcement rewrites
bashtool arguments through the plugintool.execute.beforehook, a behavior that depends on the OpenCode build. The container image ships a pinned OpenCode build (1.18.16), and the Manager refuses to start an enforced server unless the installed build is one of the versions verified by the black-box hook contract test (currently1.18.16), failing closed with a clear error instead of trusting an unverified build to run agent commands on the host. While enforcement is active (the persisted preference or a running enforced child), the Settings UI disables the Update action and the unverified rows in the Versions dialog, and the Manager rejectsPOST /opencode-upgradeand any unverifiedPOST /opencode-install-versionrequest, so the installed build can never be replaced with an unverified release; installing a verified version remains allowed. The container entrypoint also repairs a missing or below-minimumopencodeby reinstalling the bundled build rather than the latest release. - Fail closed — The installed OpenCode plugin rewrites every
bashtool command through the internal sandbox planning endpoint. If the sandbox cannot be prepared, the command is replaced with an error and exits non-zero instead of running on the host. A session outside the project roots fails with:Every other fail-closed reason uses the sameSandbox enforcement is on but the sandbox is unavailable: working directory is outside the sandboxed project roots (/workspace/repos, /workspace/schedule-worktrees)Sandbox enforcement is on but the sandbox is unavailable:prefix followed by its reason — for example, a sandbox that fails to boot reports the underlyingmsberror after the prefix.
The enforcement value is stamped into the OpenCode child process at startup and is authoritative for its lifetime. The plugin reports that stamp to the planning endpoint, so an enforced child can never fall back to host execution — even if the toggle is turned off before the required restart, the running OpenCode server keeps executing sandboxed until it is restarted.
Plugin Policy Under Enforcement
Plugins run as ordinary JavaScript inside the OpenCode process and can touch the host filesystem and process APIs without going through the bash tool, so sandbox enforcement must make sure that only Manager-owned plugin code is evaluated in the host process:
- The Manager installs exactly two plugins into the auto-discovery directory (
<workspace>/.config/opencode/plugin):ocm-sandbox.js(the enforcement hook) andocm-gh-env.js(host credential injection for theshell.envhook). Both are generated by the Manager and are the only trusted plugin files. - Repository plugins are never evaluated. Enforced OpenCode children are started with
OPENCODE_DISABLE_PROJECT_CONFIG=1, so OpenCode does not read projectopencode.jsonfiles or scan.opencode/plugin/.opencode/pluginsdirectories — including plugins planted in a repository by an agent using the host-sidewritetool or by a cloned repository itself. - Configured package plugins are disabled. On an enforced start the Manager removes the
pluginarray from the global OpenCode config file (<workspace>/.config/opencode/opencode.json) and writes a backup of the removed sections next to it; the backup is refreshed with the latest configured array whenever the file sanitizer sees new plugins during enforcement, and the entries are merged back when enforcement is turned off and the server restarts.installConfiguredPluginsis skipped while enforcement is on. The running process is additionally guarded by stateless enforcement sanitization: config reloads, settings-driven config saves, and mutatingPATCH /configrequests proxied through either Manager OpenCode proxy surface (/api/opencode/*or/api/opencode-proxy/*) have thepluginarray stripped from the patch before it reaches the OpenCode server — a body that is not valid JSON or not a JSON object is refused with403rather than forwarded — so a persisted config that still contains a plugin array can never re-activate plugins in the running process. Proxied patches are rewritten for that request only: their removed values are discarded rather than backed up, and the proxy never writes to the config file. - Config-source environment variables are blocked. OpenCode reads additional config and plugin sources from
OPENCODE_CONFIG,OPENCODE_CONFIG_CONTENT,OPENCODE_CONFIG_DIR,OPENCODE_AUTH_CONTENT,OPENCODE_TEST_HOME, andOPENCODE_TEST_MANAGED_CONFIG_DIR.OPENCODE_AUTH_CONTENTreplaces the whole auth file and can inject a well-known auth entry that triggers the.well-known/opencoderemote-config fetch;OPENCODE_TEST_HOMEredirects the home-based.opencodeplugin and agent discovery directories;OPENCODE_TEST_MANAGED_CONFIG_DIRredirects the system managed-config directory. The Manager blocks all six in user-definedserverEnvVars, removes the five overridable ones from the child environment entirely, and stampsOPENCODE_CONFIGto the Manager's own global config path, so neither a configured environment variable nor a leaked Manager process environment can introduce a third-party config, plugin, or auth source into a child process. OpenCode also skips external plugin discovery and installation whenOPENCODE_PUREis truthy, which would silently disable the Manager-ownedocm-sandbox.jsandocm-gh-env.jshooks; the Manager removes any inherited value from the child environment and stampsOPENCODE_PURE=falseafter the user environment spreads in both modes, so neither a configured entry nor an inherited value can switch pure mode on. Shell selection is protected by stamping rather than by blocking: the Manager spawns the verifiedopencodeexecutable by its absolute path, removes any inheritedSHELL,BASH_ENV, andENVfrom the child environment in both modes, and stampsSHELLto the Manager-generated sandbox shell wrapper after the user environment spreads when enforcement is on — so a configured or inherited value cannot select a repository-controlled shell binary or source repository-controlled startup files (.bashrc/$ENV/$BASH_ENV), and every host-shell surface that resolves a shell is routed into the microVM instead. Variables that only affect the child's own process (PATH,HOME,NODE_OPTIONS,BUN_OPTIONS,LD_PRELOAD,LD_LIBRARY_PATH) are not blocked inserverEnvVars, because the values the enforcement boundary depends on are stamped after the user environment and therefore always win. - User-dropped global plugins are quarantined. Any file other than the two Manager-owned plugins found in the global plugin directories (
<workspace>/.config/opencode/plugin,<workspace>/.config/opencode/plugins,$HOME/.opencode/plugin,$HOME/.opencode/plugins) is moved to a sibling.ocm-quarantinedirectory at an enforced start and restored when enforcement is turned off. - Fail closed. If the plugin directory cannot be quarantined or the config cannot be sanitized, the Manager refuses to start an enforced server.
- Defense in depth. In addition, the sandbox plugin locks the rewritten
bashcommand argument after planning and verifies the replacement by reading it back; if neither the planned command nor a blocking guard can be installed — for example another plugin froze the argument object — the hook aborts the tool call instead of letting the original command run on the host, and any detected bypass blocks every subsequent sandboxed command. - Host-execution config sections are disabled. OpenCode can spawn host processes outside the
bashhook from configuration sources, and enforcement disables all of them: local MCP servers (amcpentry withtype: "local"or acommand), the wholeformatterconfiguration, theshellconfiguration (removing it is what makes OpenCode fall back to the Manager-stamped sandbox shell wrapper), LSP servers (every enablinglspform, whether a non-empty section orlsp: true; only an explicitlsp: falsesurvives, since it disables built-in LSP processes), experimental hook commands (experimental.hook), and custom provider modules (anyproviderentry carrying annpmselector, including afile://module, which OpenCode would dynamically import in its host process); remote MCP servers and provider entries without annpmselector survive. On an enforced start the Manager removes these sections from the global config file and from every native global config file OpenCode loads (opencode.json,opencode.jsonc,config.jsonunder<workspace>/.config/opencode, plusopencode.jsonandopencode.jsoncunder$HOME/.opencode), backs up the removed sections next to each file, and merges them back when enforcement is turned off. The same sections are stripped, without any backup, from every live config reload, every settings-driven config save, and every mutating/configrequest proxied through either Manager OpenCode proxy surface. A proxiedPOST /mcpthat would add a local or command-bearing MCP server to the running process is refused with403, so only provably remote servers can be added while enforcement is active. Global custom tools — TypeScript/JavaScript modules under<config>/opencode/tool(s)/and$HOME/.opencode/tool(s)/that run in the OpenCode host process — are quarantined to a sibling.ocm-quarantinedirectory at an enforced start and restored when enforcement is off. Project-level.opencode/tool(s)/custom tools are covered byOPENCODE_DISABLE_PROJECT_CONFIG=1. The consequence: a configured local MCP server, formatter command, shell, LSP server, hook command, or custom provider module cannot read Manager or provider credentials from the host process during enforcement. - System managed configuration is sanitized. OpenCode reads the platform managed-config directory —
/etc/opencodeon Linux,/Library/Application Support/opencodeon macOS,%ProgramData%\opencodeon Windows — with the highest file-based priority. On an enforced start the Manager sanitizesopencode.jsonandopencode.jsoncfrom that directory with the same backup-and-restore contract as the native global files, so an org-managed config cannot supply a plugin, local MCP server, formatter, shell, LSP server, or hook to the host process. A managed config that contains a host-execution section and cannot be rewritten (for example a read-only bind mount) aborts the enforced start instead of running with unsanitized managed configuration. - Well-known remote configuration fails closed. OpenCode fetches organizational default configuration from
${provider}/.well-known/opencodewhenever an authenticated provider entry is a well-known auth entry, and merges the response — includingplugin, local MCP, formatter,shell, LSP, and hook sections — into the running process. That content is fetched by the OpenCode process at startup, so the Manager cannot sanitize it. On an enforced start the Manager inspects the auth file it owns (<workspace>/.opencode/state/opencode/auth.json) and refuses to launch whenever it contains a well-known provider entry, with an error naming the provider and the remediation (remove the provider authentication or disable sandboxing). TheOPENCODE_AUTH_CONTENTenvironment variable that could inject such an entry is blocked and stripped from the child environment, and both Manager OpenCode proxy surfaces additionally refuse aPUT /auth/{provider}write whose body is a well-known auth entry with403while enforcement is active — so a well-known entry cannot be added to the running process after the startup check, which would fetch remote host-executed configuration on a later enforced start. Ordinaryapiandoauthauth writes continue to pass through.
One documented consequence: because project config files are disabled while enforcement is on, the assistant-mode workspace's project-level opencode.json, agents, and skills (under repos/assistant/.opencode) are not loaded for assistant sessions during enforcement. The assistant workspace directory and its AGENTS.md guidance still load, but its default agent and managed skills are unavailable until sandboxing is disabled and the server restarts.
Worktree Placement
The Manager only creates agent worktrees under the two mounted roots, so every supported project plans in sandbox mode while the secret-bearing .opencode/state directory stays unmounted:
- Scheduled runs — OpenCode's experimental workspace API would place run worktrees beneath
.opencode/state. The schedule runner detects that location, deletes the API workspace, and falls back to a raw git worktree under/workspace/schedule-worktrees(job-<id>-run-<runId>), which is mounted. A worktree returned by the API that already lives under a mounted root is used as-is. - User-created OpenCode worktrees — the same API creates these outside the mounted roots, so while sandboxing is enforced the Manager refuses the creation with a clear error instead of handing the session a directory that cannot be sandboxed. Disable sandboxing (and restart) to use them.
- Local repositories outside
/workspace/repos— external repositories are symlinked intorepos/; the link target is not mounted, so an agent session in one of them is refused with the "outside the sandboxed project roots" error. Move the repository under/workspace/reposor disable sandboxing.
Caveats
- First-command latency — The first sandboxed command after a container start pays the image pull and microVM boot cost once for all projects. This is bounded by
SANDBOX_START_TIMEOUT_MS(default 5 minutes). - Image contents —
SANDBOX_IMAGEis the OCI image the microVM boots from. It must contain the toolchain the agent expects, includinggitif the agent runs git commands. - Workspace identity — Sandboxed commands run as the Manager's effective uid (and gid), which the entrypoint aligns to
PUID/PGID. The overlay defaultsSANDBOX_EXEC_USERtoPUID, so a non-1000PUID(for example1001) still writes to the mounted repositories as the workspace owner. If a configured numericSANDBOX_EXEC_USERcannot match the workspace owner, the toggle reports enforcement as unavailable with the reason instead of enabling a broken sandbox. - Permission patterns — OpenCode
permissionrules are matched against the rewritten command (msb exec ...), not the agent's original text. Command-pattern permission rules must be reviewed when enforcement is on. - Plugin lockdown — While enforcement is on, only the Manager-owned plugins load (see Plugin Policy Under Enforcement); repository, project, and user-configured plugins are not evaluated in the host process. As defense in depth, the sandbox plugin also locks the
commandargument after rewriting, so any plugin that did manage to load cannot unwrap a sandboxedbashcommand, and a detected bypass blocks every subsequent sandboxed command. - Host credentials — Credentials injected by the
shell.envhook (ocm-gh-env.js) are not forwarded into the microVM, so agent-rungit push/ghcalls that depend on those injected credentials fail inside the sandbox by design. Credentials that are themselves visible inside the microVM remain usable — a token or credential helper stored in a mounted repository, an authenticated remote URL, or a credential supplied directly to the command — because the microVM's networking is enabled. - Assistant internal API — The assistant-mode
.opencodedirectory is masked in the microVM, so the internal API token it holds is not readable there. Assistant sessions can still manage repos, schedules, notifications, and settings through the Manager's host-side integrations; agent-runcurlagainst the internal API using the masked token fails inside a sandbox by design.