#8186: fix(sandbox): validate setupCommand to prevent shell injection
agents
stale
## Summary
Add validation to the Docker sandbox `setupCommand` configuration to prevent command injection attacks.
## The Problem
The `setupCommand` configuration value is passed directly to `sh -lc` inside Docker containers without any validation or sanitization. An attacker who can modify the configuration (via `config.patch`, file write access, or compromised credentials) could inject arbitrary shell commands that execute during container initialization.
This is particularly dangerous because:
- Commands run as root inside the container
- Workspace volumes are often mounted with write access
- The container has network access by default
- Commands execute before any user code runs
Reference: [CWE-78: OS Command Injection](https://cwe.mitre.org/data/definitions/78.html)
## Changes
- `src/agents/sandbox/docker.ts`: Add `validateSetupCommand()` function that checks for dangerous patterns including:
- Command chaining operators (`;`, `&&`, `||`)
- Command substitution (backticks, `$()`)
- Pipes and redirections (`|`, `>`, `>>`, `<`)
- Subshell syntax
- Dangerous commands (`curl`/`wget` with URLs, `eval`, `nc`/`netcat`)
- Shell invocation with command strings (`bash -c`, `sh -c`, `zsh -c`)
- Multi-line commands (newlines)
- `src/agents/sandbox/docker.setup-command-validation.test.ts`: New test file with 18 test cases covering the validation logic
## Test Plan
- [x] `pnpm build && pnpm check && pnpm test` passes
- [x] New test `describe('VULN-050: sandbox setupCommand validation')` validates the fix
- [x] Valid setup commands like `apt-get update`, `pip install requests` are allowed
- [x] Dangerous patterns are rejected with clear error messages
## Related
- [CWE-78: OS Command Injection](https://cwe.mitre.org/data/definitions/78.html)
- Internal audit ref: VULN-050
---
*Built with [bitsec-ai](https://github.com/bitsec-ai). AI-assisted: Yes. Testing: fully tested (test written before fix). Code reviewed and understood.*
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds a `validateSetupCommand()` gate before executing Docker sandbox `setupCommand` via `sh -lc`, along with a new Vitest suite covering common shell-injection metacharacters and a few high-risk utilities.
The change fits into the sandbox container creation flow (`createSandboxContainer` in `src/agents/sandbox/docker.ts`) by failing fast with a clear error message when the configured setup command contains denied patterns, preventing multi-command/chaining and command-substitution payloads from being executed during container initialization.
<h3>Confidence Score: 3/5</h3>
- This PR improves security, but a couple of deny patterns are likely to break benign setups or behave inconsistently.
- Core logic is straightforward and covered by tests, but the unconditional `(` ban is overly broad and likely to reject legitimate commands, and there are redundant/inconsistent `wget`/`curl` rules that suggest the validation policy may not match the intended behavior.
- src/agents/sandbox/docker.ts (denied patterns); src/agents/sandbox/docker.setup-command-validation.test.ts (test descriptions vs behavior)
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#8161: fix(sandbox): block dangerous environment variables from Docker con...
by yubrew · 2026-02-03
84.6%
#3907: fix(sandbox): use absolute /bin/sh path + add allowedReadPaths config
by pvoo · 2026-01-29
80.8%
#16509: Fix sandbox path validation rejecting Docker bind mount paths
by Clawborn · 2026-02-14
79.7%
#21665: fix(sandbox): add /home and /Users to bind-mount denylist
by AI-Reviewer-QS · 2026-02-20
79.7%
#11820: fix(sandbox): remap container paths in sandboxed file tools
by steflsd · 2026-02-08
79.3%
#13873: fix(sandbox): prevent Windows PATH from poisoning docker exec
by alessandrorodi · 2026-02-11
78.5%
#9657: fix(doctor): warn when sandbox mode enabled without Docker
by mcaxtr · 2026-02-05
78.1%
#4226: Fix/sandbox containerworkdir rw access
by ozgur-polat · 2026-01-29
78.1%
#8567: fix: Sandbox browser runs Chromium as root with --no-sandbox
by coygeek · 2026-02-04
77.2%
#7826: feat(sandbox): add Docker Sandboxes (microVM) as alternative backend
by HODL-Community · 2026-02-03
76.7%