# Stop an AI agent from running rm -rf or git push --force

> Deny rules, ask rules and hooks for Claude Code, Codex and Cursor on a Mac, with the exact files and keys, what each one misses and how to test it.

Use three layers. First, a rule in the agent’s own settings that denies or asks before the command. Second, the agent’s Mac sandbox, which limits what a shell command can reach whatever its text says. Third, branch protection on your Git host, which refuses a force push no matter who sends it. A rule that matches command text can be sidestepped by a different spelling, so no single layer is enough. Never rely on an instruction in a prompt or `CLAUDE.md` alone.

## Claude Code

Rules go in `permissions` in `~/.claude/settings.json` (all projects), `.claude/settings.json` (shared) or `.claude/settings.local.json` (yours). Rules are checked deny first, then ask, then allow, and the first match wins. An allow rule cannot carve an exception out of a deny.

```json
{
  "permissions": {
    "deny": ["Bash(git push --force *)", "Bash(git push -f *)"],
    "ask": ["Bash(rm *)", "Bash(git push *)"]
  }
}
```

Run `/permissions` to see every rule and the file it came from. The docs list what a rule does not stop. `Bash(rm *)` stops `rm -rf build/` but not `/bin/rm -rf build/` or `bash -c 'rm -rf build/'`, and `Bash(git push *)` does not stop `git -C . push origin main`. For enforcement that does not depend on the command text, turn on the sandbox with `/sandbox` or `"sandbox": {"enabled": true}`. On macOS it uses Seatbelt. It covers shell commands only, not Claude’s file tools. To inspect the full command yourself, use a `PreToolUse` hook that exits with code 2. [Claude Code hooks](https://www.excellent.so/guides/claude-code-hooks) · [Claude Code sandbox](https://www.excellent.so/guides/claude-code-sandbox) · [Claude Code permissions](https://www.excellent.so/guides/claude-code-permissions)

## Codex

Codex has two relevant controls. The sandbox and approval policy are set with `sandbox_mode` and `approval_policy` in `~/.codex/config.toml`, or with `--sandbox workspace-write --ask-for-approval on-request` on the command line. On macOS the sandbox uses Seatbelt. Command rules go in a `.rules` file such as `~/.codex/rules/default.rules`, and the docs mark rules as experimental. Each rule has a `decision` of `allow`, `prompt` or `forbidden`, and the most restrictive match wins.

```
prefix_rule(
    pattern = ["git", "push", ["--force", "-f", "--force-with-lease"]],
    decision = "forbidden",
    justification = "Force pushes are blocked here. Ask the owner.",
    match = ["git push --force origin main"],
    not_match = ["git push origin main"],
)
```

Test it before you trust it: `codex execpolicy check --pretty --rules default.rules -- git push --force origin main`. We ran that on Codex CLI 0.166.0 and got `forbidden`. The same rule returned no match for `git push origin --force`, because a pattern matches an exact prefix. Add one rule per spelling you care about. Restart Codex after you add a rules file. [Codex sandbox](https://www.excellent.so/guides/codex-sandbox) · [Codex hooks](https://www.excellent.so/guides/codex-hooks)

## Cursor

In the editor, **Settings > Agents > Approvals & Execution** sets the Run Mode. Auto-review runs allowlisted calls at once, runs other shell commands in the sandbox when it can, and sends the rest to a classifier. Allowlist runs only what you listed. Run Everything runs every call with no sandbox, and Cursor tells you to accept the risk first. Cursor calls these best-effort guardrails, not a hard boundary. On macOS its sandbox uses Seatbelt, protects `.git/config` and `.git/hooks` by default and blocks network until a network mode or `sandbox.json` opens it. Settings live in `~/.cursor/sandbox.json` and `.cursor/sandbox.json`.

For the Cursor CLI, `~/.cursor/cli-config.json` or `.cursor/cli.json` takes a `permissions` object with `allow` and `deny` lists, and deny wins. The docs describe `Shell(rm)` as a deny token for destructive removal. For a rule you write yourself, a `beforeShellExecution` hook in `~/.cursor/hooks.json` can answer `deny`, and exit code 2 also blocks. Set `failClosed` so a crash blocks instead of allowing. [Cursor hooks](https://www.excellent.so/guides/cursor-hooks)

## Side by side

| | Claude Code | Codex | Cursor |
| --- | --- | --- | --- |
| Deny or ask a command | `permissions.deny` and `permissions.ask` | `prefix_rule` with `forbidden` or `prompt` | CLI `permissions.deny`, or a `beforeShellExecution` hook |
| Mac sandbox | `/sandbox`, Seatbelt, shell only | `sandbox_mode`, Seatbelt | Run Modes, Seatbelt, `sandbox.json` |
| Hook that can block | `PreToolUse`, exit 2 | `PreToolUse`, `permissionDecision` deny | `beforeShellExecution`, `deny` or exit 2 |
| Known gap | Other spellings of the same command | Flags in a different position | Docs call Run Modes best-effort |

## Protect the branch on the host

A local rule cannot stop a push from another machine. In GitHub or GitLab, protect `main`, disallow force pushes and require reviews. [Git protection](https://www.excellent.so/docs/git) · [If a force push already happened](https://www.excellent.so/guides/ai-agent-force-pushed-main)

## Where Excellent fits

Excellent’s free scan reads supported logs on the Mac and flags a force push, a deleted never-delete folder and a key in a log after the fact. Excellent Pro adds a live check before supported actions in the agents listed on the [Agents](https://www.excellent.so/agents) page, including Cursor, and blocks or asks before the risky ones. In Claude Code the ask shows as a prompt. In Codex an ask refuses the action and tells the agent to ask you. Commands sent into an already running Codex shell fall outside that check. Pro is US-only for now. [Run the free scan](https://www.excellent.so/scan) · [Agent coverage](https://www.excellent.so/agents) · [Pricing](https://www.excellent.so/pricing)

## Checklist

- [ ] Deny or ask rules written for `rm` and `git push` in each agent.
- [ ] Each rule tested with a harmless command and one other spelling.
- [ ] The sandbox turned on in each agent that has one.
- [ ] `--dangerously-skip-permissions` and `--yolo` not in your shell aliases.
- [ ] Force pushes disallowed on `main` at the Git host.

Checked against vendor documentation on November 25, 2026. [Claude Code permissions](https://code.claude.com/docs/en/permissions) · [Claude Code sandboxing](https://code.claude.com/docs/en/sandboxing) · [Codex rules](https://learn.chatgpt.com/docs/agent-configuration/rules) · [Codex sandboxing](https://learn.chatgpt.com/docs/sandboxing) · [Cursor run modes](https://cursor.com/docs/agent/run-modes) · [Cursor CLI permissions](https://cursor.com/docs/cli/reference/permissions) · [Cursor hooks](https://cursor.com/docs/hooks)

Source: https://www.excellent.so/guides/stop-ai-agent-running-rm-rf-or-git-push-force
