Contents
The risks you are controlling
- Destructive commands (
rm -rf,terraform destroy, a force push to main). - Secret exposure: reading
.envor cloud keys, then sending them out. - Prompt injection: text in a README, issue, web page or MCP result that steers the agent.
- Tampering: the agent (or a cloned repo) changing its own hook or permission config.
- No record: nobody can say later what the agent did and who approved it.
Sandboxes: limit what a command can reach
Claude Code's sandbox puts an OS-level boundary around the shell commands it runs (Bash, PowerShell and Monitor), using Seatbelt on macOS and bubblewrap on Linux and WSL2; on native Windows, commands run unsandboxed. By default writes stay inside the working directory, a per-user temp directory and directories you add, and network traffic goes through a local proxy that checks each host against your allowed domains. Codex has sandbox modes (read-only, workspace-write, danger-full-access) paired with an approval policy.
What a sandbox does well: contains a bad command even when nobody reviewed it.
Where it stops: it decides by path and host, not by intent, so git push --force to an allowed host or DROP TABLE over an allowed connection still runs. Claude Code's docs state that the sandbox covers shell commands, while file tools, MCP servers and hooks run outside it. And it is per developer, with no team approval flow or central record.
Gateways: see what leaves the machine
An MCP or network gateway sits between the agent and remote tools or APIs. It can log and filter calls to remote MCP servers and outbound HTTP.
Where it stops: local shell commands, file reads and writes, and local (stdio) MCP servers never pass through it. A gateway sees the request, not which agent session made it or what that session read before.
Hooks: check each action before it runs
All three agents can call your own check before a tool runs: Claude Code PreToolUse, Cursor beforeShellExecution, beforeMCPExecution, beforeReadFile and preToolUse, and Codex PreToolUse. The check sees the exact command, path or MCP input and can allow, deny, or ask.
What hooks add: decisions by intent (a push to main, a read of .env), approval by a person who is not the developer, one policy across agents, and an audit record per action.
Where they stop: a hook decides what the agent asks to run; it does not contain what a command does after it starts. Hooks fail open by default in Cursor (failClosed: true changes that per hook), and on Codex a hook that times out or crashes lets the action run (openai/codex#41979, open). A local administrator can remove hook files. So: hooks plus sandbox, not hooks instead of sandbox.
“The harness-enforced pattern is the right target.”
Coding agent hooks compared: Claude Code, Cursor, Codex
All three agents can run a script before a tool call. Claude Code: PreToolUse, with allow, deny, ask or defer, matchers per tool (Bash, Write, mcp__<server>__<tool>), and onFailure: "block" to fail closed. Cursor: beforeShellExecution, beforeMCPExecution and beforeReadFile, answering allow, deny or ask; set failClosed: true, because crashes and timeouts fail open by default. Codex CLI: PreToolUse hooks that allow or deny (no ask), and MCP calls pass through them. For a team, put the hook in managed settings (Claude Code), enterprise hooks.json (Cursor) or managed config (Codex) so users cannot remove it.
How we layer them in SecGuardRail (from our build)
- One policy set by tool class: shell, file read, file write, MCP, web, git. A rule written for
Bashalso covers PowerShell and Cursor's shell commands. - Fail mode per tool class: by default reads fail open; shell, writes and MCP fail closed. If our service cannot read its database it answers 503 and the agent's fail setting applies.
- Destructive Shell pack, measured on 551 dangerous and 576 safe commands: 100% caught, 1.6% false positives (9 cases, all with the dangerous text inside quotes).
- Tamper Protection: always-on denies for agent writes to hook and permission config, with a Critical alert.
- Monitor mode before enforcing: policies record what they would have done without blocking, so a team sees impact before turning them on.
- Approvals in the dashboard, with Slack alerts that link to the request; every decision in a hash-chained audit log that breaks if a record is edited or deleted.
Prompt injection: limit what injected text can make the agent do
Detection helps, but it misses plain-language instructions. Our rule-based detector: 0 false positives on 795 benign samples, 35% recall on public benchmarks plus our own canaries; every sample with an injection marker is caught, plain requests planted in data are not. So the stronger control is what happens next: once a session reads untrusted content (web, README, MCP result), it is marked tainted and its risky follow-ups (egress outside an allowlist, secret reads, git push, permission changes, the exact command the text asked for) are held for a person, with the source named on the approval card. Our full numbers and the hidden-text tricks are in prompt injection in coding agents.
MCP security for coding agents
MCP tool calls are actions like any other: check them before they run (mcp__* in Claude Code, beforeMCPExecution in Cursor), fail closed by default, treat MCP results as untrusted input that can taint the session, and record the server and tool for every call. A gateway adds value for remote servers; local stdio servers need the hook. Tool poisoning, allowlists and the OWASP MCP Top 10 are covered in MCP security for coding agents.
Monitor, then enforce
Monitor mode records what each policy would have done without blocking. Run it for a week, review what it would have stopped and adjust the rules, then switch policies to enforce.
Checklist for a team rollout
- Sandbox on for every developer (Claude Code sandbox or Codex
workspace-write); no--dangerously-skip-permissionsor bypass flags in shared setups (see a safe alternative to --dangerously-skip-permissions). - Hooks installed centrally (managed settings, Cursor Enterprise or Team level), not per user.
- Fail closed for shell, writes and MCP; explicit timeouts.
- Deny destructive commands and secret reads; hold push, deploy and permission changes for approval.
- Deny agent writes to hook and permission config.
- Taint and hold after untrusted content.
- Audit every decision off the laptop; run in Monitor mode for a week before enforcing.
FAQ
Is a sandbox enough for AI coding agent security?
For a single developer it removes most of the damage a bad command can do. A team also needs decisions by intent, approvals and an audit record, which a sandbox does not give.
Do I need an MCP gateway?
If you use remote MCP servers, it helps. It does not see local commands or local MCP servers, so it does not replace a per-action check.
Do hooks slow the agent down?
Policy evaluation in our tests: p50 0.11 ms, p95 0.21 ms. Server time per hook on the hosted service: about 85 ms (measured 21 Nov 2026), plus network time.
Which agents does this cover?
SecGuardRail applies one set of policies, approvals and one audit log across Claude Code, Cursor and Codex. See the setup docs.
How do I add guardrails to a coding agent?
Start with deny rules for actions that must never run, add a pre-call hook that checks every shell, file and MCP call and fails closed, hold risky actions for a person, and log every decision. Run new rules in monitor mode for a week before you enforce them.