Skip to main content
Blitz
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

007 · Shell

Shell commands, command policy, OS sandbox, background processes (spec_007_shell)

Status Implemented. Reverse-engineered from 53f8c53 (2026-09-24) and kept with the code since; its paths checked 2026-09-27 (//tools/specs).
Source pkg/engine/tools/shell.go, exec.go, cmdpolicy.go, ossandbox*.go, bwrap.go, background.go, procgroup_*.go, capped_buffer.go
Tests pkg/engine/tools/shell_test.go, cmdpolicy_test.go, ossandbox_test.go, bwrap_test.go, guard_test.go
Depends on spec_006_filetools (roots, blocked patterns), spec_005_approvals

1. Purpose

The agent runs shell commands through run_shell_command. Three layers apply: a parsed command policy (guardrail), approval, and the OS sandbox (the boundary). Every child process is in its own process group, has credentials scrubbed from its environment, and is guarded so it dies with Blitz — even on SIGKILL.

2. run_shell_command

Args: command, cwd? (inside the workspace), timeout_seconds?, background?. Result: output, exit_code, duration_ms, is_background, process_id, truncated, error.

  • SH-01 Empty command → error. cwd must resolve inside the sandbox roots and exist.
  • SH-02 Policy deny → “blocked by command policy: ”, nothing runs. auto_approve → runs without asking. Otherwise approval (kind run_command, key = exact command in this workspace, target = the command). The detail shows (in <cwd>), [background], and the policy’s reason when it could not auto-approve.
  • SH-03 Runs bash -c <command> in the cwd, stdout+stderr interleaved into one buffer capped at 100 KiB (excess discarded while running, with a truncation notice that never splits a UTF-8 rune).
  • SH-04 Timeout: tools.shell_timeout_seconds (default 120 s), per-call override, capped at 30 min (clamped before conversion to avoid overflow). Timeout → exit −1 “command timed out after …”; cancellation → exit −1 “command cancelled”; otherwise the process exit code.
  • SH-04a 2026-11-05 (scan N26-engine-tools-2#19: the time-out didn’t say how to allow longer, and a setting over the cap was cut without a word). The time-out reads “command timed out after 2m0s (set timeout_seconds, up to 1800, for a longer one, or run it in the background)”, and a tools.shell_timeout_seconds over 1800 is logged as over the limit when the workspace opens. The 120 s default is config.DefaultShellTimeoutSeconds, used by both the settings and the tool.
  • SH-05 After a kill, Wait waits at most 2 s for pipes held open by descendants.

3. Command policy

  • SH-10 The script is parsed as Bash (mvdan.cc/sh) and every simple command is checked: pipelines, lists, subshells, command substitutions, functions, sh|bash|zsh|dash|ksh -c <literal> (recursively, max depth 3), find -exec/-execdir/-ok/-okdir, and wrappers env, nohup, command, builtin, exec, time, nice, timeout, xargs, stdbuf, sudo, doas, caffeinate (option values skipped per wrapper; env VAR=x skipped; timeout duration skipped). Wrappers are checked against deny themselves but are otherwise transparent.
  • SH-11 Patterns match a simple command’s words joined by single spaces, in three forms (tools.CommandPatternForm): words alone name a command with any arguments (ls matches ls -la, git log matches git log --oneline but not git logs); with * (any text) or ? (one character) the pattern is a glob over the whole command, and a trailing * also matches the bare command (git * matches git); re: starts an RE2 regular expression that must match the whole command (re:git (log|show)( .*)?; an invalid one is an error). A command given by path also matches by base name (/bin/rm -rf x matches rm). This applies to sandbox.commands and to shell(…) permission rules alike.
  • SH-11a A redirection that writes a file (>, >>, &>, &>>, >|, <>, and >& to a name; not /dev/null, /dev/stdout, /dev/stderr, /dev/tty or a descriptor like 2>&1), including in a subshell, a bash -c string or with a computed target, is not a command: no pattern matches it, and it keeps auto_approve and allow rules from approving the script (it needs approval, the reason naming the file).
  • SH-12 Words are literal only if fully static. Unquoted globs, brace expansion ({r,}m), $'…'/$"…", parameter/command expansions are dynamic. Backslash escapes are removed (s\udo → sudo). A dynamic command name is unverified.
  • SH-13 eval, source and . are unverified (code can’t be checked).
  • SH-14 Verdicts: any command matching deny → deny (always wins). With an allow list: a parse error, an unverified command, or a command not matching → deny. auto_approve applies only if every non-transparent command matches it and none uses runtime expansion or is unverified. Otherwise → needs approval. A parse error without an allow list → needs approval with the parse error as reason. A nil policy always needs approval.
  • SH-14b 2026-11-05 (scan N26-engine-tools-1#1: a parse error skipped the deny rules, so git push --force; bash -c 'if' went to approval and ran in bypass, with auto_approve_commands or a remembered approval). A script that doesn’t parse whole, or has a bash -c script in it that doesn’t, is still checked against every deny pattern ([sandbox.commands] deny, the defaults, [permissions] deny shell(…)) on what did parse: the statements before the error and the commands of the outer script around the bad nested one. A match is denied. Otherwise, with an allow list or any deny pattern at all, it’s denied (“could not parse the whole command to check it against the deny rules”), since a denied command can’t be ruled out; with neither it needs approval with MustAsk, so a person is asked even in bypass, with auto_approve_commands or a remembered approval (and refused where nobody can be). A hook’s if = "shell(…)" runs for such a script (HK-64a).
  • SH-14a Since v0.6.3. A recursive search of the whole disk or the user’s home folder is denied whatever the rules, mode or approvals (diskSweep, checked before the policy, with no policy too): find, gfind, fd, fdfind, rg, du and tree (and grep, egrep or ls with -r/-R/--recursive) given /, ~, $HOME, the home folder’s path, /Users, /home, /Volumes or /System/Volumes/Data, wrapped or nested included. Agents ran find / -name uv looking for tools, which on macOS asked the user about each private folder (SH-22a). The reason tells the agent to use command -v <name> (or which) for an executable, else to search the workspace or name a folder; the shell tool’s description and the blitz agent’s rules say the same. A named folder (find /opt/homebrew, find ~/.cache/uv) is fine.
  • SH-15 The policy is a guardrail, not a security boundary: an allowed interpreter can do anything.

4. OS sandbox (sandbox.shell)

  • SH-20 Modes: auto (sandbox if available, else run unsandboxed and report why), required (startup fails if unavailable: api.ErrSandboxUnavailable, the service’s SANDBOX_UNAVAILABLE, with how to get the sandbox or set auto), off. Unknown mode is a startup error. Unset in the settings, it’s required on Linux and auto elsewhere (config.DefaultShellMode, applied when settings load; DefaultConfig stays auto); BLITZ_SANDBOX_SHELL overrides the settings (the tests run with auto).
  • SH-20b pkg/sandboxsetup checks Linux’s sandbox from outside the engine (bwrap probed as the script sandbox runs it, with --unshare-net): ready, no_bwrap, restricted (bwrap fails, /proc/sys/kernel/apparmor_restrict_unprivileged_userns is 1 and apparmor_parser is there), broken, unsupported (not Linux). For restricted, it installs /etc/apparmor.d/blitz-bwrap, a profile named blitz-bwrap for bwrap’s real path, flags=(unconfined) with userns (Ubuntu’s pattern for programs that need user namespaces), with one elevated command (install -m 0644 -o root -g root of a temporary copy, then apparmor_parser -r), then checks again. The CLI’s blitz security reports the state and blitz security fix-apparmor fixes it with sudo (neither opens a workspace); the desktop’s Settings › Service has an OS sandbox row on Linux only (SandboxStatus, FixSandbox) whose Allow bubblewrap uses pkexec, the system’s password dialog (never sudo, which has no terminal to ask in), then restarts the service; a workspace that didn’t open for want of the sandbox offers the same in a notice above it. Without either, or when it fails, the commands to run by hand are shown. The required-sandbox error (SH-20) points to both.
  • SH-20b 2026-11-05 (scan N26-safety-enterprise#10: the profile, written as root, was for whatever bwrap came first on PATH, which could be one in ~/.local/bin the person, or anything running as them, can replace). Fix writes the profile only for a bwrap that’s a system program: it and every folder above it owned by root and writable by no one else (ErrNotSystemBwrap otherwise, saying to install bubblewrap from the system’s packages or put /usr/bin first on PATH).
  • SH-21 Writable dirs: writable file roots + defaults (OS temp dir; on macOS its per-user parent under /var/folders; /tmp; /var/tmp; the user cache dir) + existing sandbox.shell_writable_paths. Read-only roots stay read-only even inside writable dirs. Blocked paths are unreadable and unwritable. Network is off when sandbox.allow_network = false.
  • SH-22 macOS: Seatbelt via /usr/bin/sandbox-exec -p <profile>. Profile: (allow default), (deny file-write*), allow writes to writable subpaths and /dev/null|zero|tty|dtracehelper, /dev/ttys*, /dev/fd/*; deny writes to read-only dirs; deny read/write to blocked regexes (last, so they win); without network (deny network*) except unix sockets. The profile is probed once with /usr/bin/true.
  • SH-22a Since v0.6.3. The Seatbelt profile also denies reading inside, or listing, the folders macOS guards with a prompt (privateDirs): ~/Desktop, ~/Documents, ~/Downloads, ~/Movies, ~/Music, ~/Pictures, iCloud Drive (~/Library/Mobile Documents), ~/Library/Mail, Messages, Calendars, Application Support/AddressBook, Safari, and /Volumes. Their own metadata stays readable (ls -la ~ works), and a writable or read-only root inside one is allowed again (a workspace in ~/Documents works). A command that sweeps them gets “Operation not permitted” instead of making macOS ask “Blitz would like to access files in your Music folder”, which, Blitz being ad-hoc signed, it asks again after every update.
  • SH-23 Linux: bubblewrap: --die-with-parent --ro-bind / / --dev-bind /dev /dev, --bind writable dirs, --ro-bind read-only dirs, blocked files masked with /dev/null, blocked dirs with an empty read-only tmpfs, --unshare-net without network. Probed once with /bin/true (needs unprivileged user namespaces; Ubuntu 24.04 AppArmor and Docker’s seccomp profile block them).
  • SH-23a 2026-11-05 (scan N26-engine-tools-2#1: a sandboxed command could connect to the service’s socket, which only its 0600 mode guards, and call RunShell outside the sandbox or Approve its own requests). Sandboxed commands (shell, forged tools, stdio MCP servers, skill scripts) can’t reach the service’s socket, whatever allow_network says: Seatbelt denies network-outbound and file-write* (so a socket can’t be moved away) in Blitz’s run folder (~/.blitz/run) and on each service socket ($BLITZ_SOCKET, the default, and the one the service listens on, tools.GuardSocket), as given and as its real path, last so it wins over the Unix sockets SH-22 allows; bubblewrap puts an empty read-only tmpfs over the run folder (made first if missing), binds the credentials copies (SH-32) back in, and puts /dev/null over a service socket outside it. Other Unix sockets (an SSH agent, Docker, a database) still work.
  • SH-23b 2026-11-21 (the owner, hardening; security review terminal#1: the agent could write .git/config or a hook, and the person’s own terminal, git GUI or shell prompt ran it outside the sandbox). A repository’s files git runs commands from stay the person’s: .git/config, config.worktree, hooks/ and info/attributes, of the repository at the top of each writable root and its submodules’ (.git/modules/…); a repository the agent makes further down (git clone, git init) is its own. Seatbelt denies writing them (a regex rule after the writable dirs), bubblewrap binds those that exist read-only, and the file tools refuse them, through a link too, any case on a case-insensitive disk (ErrGitControl). Git’s other files (HEAD, index, objects, refs) are written as usual, so commits, branches and pushes work; git config, git remote add, git push -u’s upstream and installing a hook don’t. On Linux, bubblewrap binds them read-only only where they exist, so a repository without hooks/ or info/attributes gets an empty one first, made by Blitz outside the sandbox (an empty attributes file is as none to git): a command can’t make one git would run.
  • SH-23c 2026-11-21 (security review enterprise#4: the blocked defaults name ~/.blitz/.env.toml, so with MODENV_PREFIX or --config Blitz’s own settings, provider keys and gateway headers among them, were readable by the model and the shell). The settings files of the folder the settings were read from (Config.Dir) are blocked too: its .env.toml, .env.*.toml and workspaces/*/ ones.
  • SH-24 Because bwrap masks paths not patterns, blocked name patterns are expanded before every sandboxed command by scanning the writable and read-only roots (max 50 000 entries, depth 12, skipping .git, node_modules, vendor, target, __pycache__); absolute patterns are globbed. A file created by a command that matches a blocked pattern is visible to that same command (macOS blocks it immediately).
  • SH-24a Each sandbox keeps its scan between commands (blockedScan): every directory walked is checked with one lstat (modification time, size, inode), and only those that changed are read and matched again; a directory read within 2 s of its modification time is read again next time (file systems keep coarse times). It walks in the same order and within the same limits as a full scan, so it masks the same existing paths (tested against the full walk). A command’s start-up for a 50,000-entry workspace is about 1 ms after the first scan; CI’s Linux job fails above 20 ms (BenchmarkBlockedScan), and the parallel-cap test runs with the sandbox on. The first scan can take seconds (about 4.6 s for that workspace under the race detector); it checks the command’s context every 256 entries, so a time limit or a cancelled turn stops it and the command doesn’t start (command timed out after … / command cancelled), and what it read stays cached.
  • SH-24b 2026-11-05 (scan N26-engine-tools-1#7: what lay past the limits stayed readable, silently). What the scan doesn’t reach isn’t masked: names past 50 000 entries or 12 levels, under the skipped folders (.git, node_modules, vendor, target, __pycache__), and absolute patterns with **. When the entry or depth limit stops a scan, the service’s log says so once per sandbox (“blocked names past it … aren’t masked from commands”), with the limit hit. macOS’s Seatbelt matches patterns and has no such gap.
  • SH-24c 2026-11-05 (scan N26-engine-tools-2#16: skill scripts built a new sandbox for each run, probing sandbox-exec or bwrap again and scanning for blocked paths from cold, seconds on a large workspace). A script box keeps what its runs share: the platform is probed once, and one blocked-path scan serves every run (bubblewrap and gVisor), walking each run’s roots and reading again only directories that changed.
  • SH-24d 2026-11-21 (security review engine-tools-1#7: the scan skipped node_modules, vendor, target and pycache, so a blocked name there wasn’t masked on Linux, and a scan stopped at its limit said so only in the log). The scan skips .git alone, and once it stops at a limit the turn is told (once): blocked files past it aren’t hidden from commands; a smaller folder as the workspace hides them all.
  • SH-25 Other platforms have no OS sandbox (auto runs unsandboxed; required fails).
  • SH-26 The same exec environment runs forged tools and stdio MCP servers (spec_009_mcp, spec_014_agents).

5. Process guard and environment

  • SH-30 Children start in their own process group (Unix); cancellation kills the whole group with SIGKILL, so grandchildren (sleep &) can’t outlive a timeout or hold pipes open.
  • SH-31 Parent-death guard: every command is wrapped by bash -c '( read -r -u 3 _ ; kill -KILL 0 ) … & exec 3<&- ; exec "$@"' with a pipe’s read end as fd 3; Blitz holds the write end. EOF — Blitz closing it after the command exits, or Blitz dying for any reason — kills the group. Not available on non-Unix platforms.
  • SH-32 Environment variables matching sandbox.scrub_env globs (case-insensitive) are removed from children. Blitz’s own sign-ins are blocked paths: gcloud’s directory (a default) and the ant command’s (AnthropicConfig.OAuthProfile’s directory) unless Claude signs in with it. With sandbox.share_adc (never from a project) and a provider on Google’s ADC (auth = "adc"), each child gets a copy of the ADC file (GOOGLE_APPLICATION_CREDENTIALS, else gcloud’s application_default_credentials.json in $CLOUDSDK_CONFIG or ~/.config/gcloud), read as it starts, owner-only in its own run-* directory under ~/.blitz/run/credentials, beside the service’s socket: readable by sandboxed commands but not writable by them (not the cache directory, where a command could put a link that Blitz, unsandboxed, would follow); the directory must be a real one only its owner can enter, or the command doesn’t start. GOOGLE_APPLICATION_CREDENTIALS names the copy; it is removed when the child ends, and run directories over a day old (left by a crash) when a workspace opens, without following a link in the directory’s place. The blocked paths are unchanged. Without the file (a Google Cloud machine’s own credentials) nothing is copied. Commands run in the workspace root unless they set a directory.
  • SH-32a 2026-11-05 (scan N26-engine-tools-1#8: a malformed glob scrubbed nothing, silently). A sandbox.scrub_env pattern that isn’t a valid glob (MY_[TOKEN) stops the workspace from opening, naming the pattern.

6. Background processes

  • SH-40 background: true starts the command under the process manager and returns its ID immediately. Limits: 8 running at once (“too many background processes running”), lifetime 1 h, 256 KiB captured output, 16 finished processes retained.
  • SH-40a 2026-11-05 (scan N26-engine-tools-1#10). The background limits are fixed (no setting), and the refusal says so (“a fixed limit of 8”). A watched process its lifetime stopped is reported as “was stopped after 1h0m0s, the fixed lifetime of a background process”, not by its exit code alone.
  • SH-42 Notices (PAR-TOOL-04): with background, notify: true reports the process’s exit (“Background process (<command>) exited with code after . Its last output: …”, the last 1,500 characters) and notify_pattern (a Go regular expression; invalid is an error before anything starts) each output line matching it, up to 20 ("… printed: ", 500 characters; the last line counts without a newline), to the session that started it. A turn running in that session reads it with its next tool result, like a message sent while it runs ((background process) …); otherwise it waits (up to 50) and goes with the session’s next prompt as background-processes context, and the REPL, idle at its prompt, asks for waiting notices every 2 s (TakeProcessNotices) and runs a turn with them ((background) …). The desktop app gives them to the next prompt.
  • SH-41 manage_background_process actions: list (id, command, running, exit_code, runtime_ms), output (captured output + status), kill (group kill, waits ~3 s; error if it doesn’t exit).
  • SH-41a 2026-11-05 (scan N26-engine-tools-1#9: one chat’s agent could read and kill another chat’s processes). manage_background_process sees only the calling chat’s processes (ListIn its session); output and kill of another chat’s process are refused (“belongs to another chat”). The person still sees and stops every process in the app (spec_052 PR-31).
  • SH-42 Shutdown kills all and refuses new ones. Front ends account for running processes at exit: the REPL/one-shot asks to kill or wait; a second Ctrl+C force-quits; non-interactive exits kill them (spec_019_tui).

7. User shell (!cmd)

  • SH-50 In the REPL, !<command> runs the user’s own command in the workspace with the user’s environment, outside the agent’s sandbox, policy and approvals. The agent doesn’t see it; the audit log records it (user_shell, exit code, start error).

8. Known gaps