Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 0 additions & 146 deletions .claude/agent-guard.toml

This file was deleted.

55 changes: 28 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,21 @@ echo '{"tool_input":{"command":"git status"}}' | agent guard
}
```

**Blocked patterns** (configurable via `.claude/agent-guard.toml`):
**What it refuses.** The guard splits the command line the way a shell does (quotes, `;`, `&&`, `|`, `&`, newlines, `$(…)`, backticks, heredocs, `((…))` and `$((…))` as opaque arithmetic, `case` outside `$(…)`, function bodies), unwraps `sudo`, `doas`, `env` (and the words of `env -S`), `nohup`, `nice`, `time`, `timeout`, `setsid`, `stdbuf`, `command`, `exec`, `sh -c` (also `bash`, `zsh`, `dash`, `ksh`), `eval`, `find -exec`, `meta [options] exec` and `meta [options] git`, skips git's global options (`git -C dir`, `-c k=v`, `--git-dir`), and reads options the way git and getopt do: `-Df` is `-D -f`, an option's value is not read as flags (`-ekeep`, `-o x`), and any unique prefix of a long option counts (`--ha` is `--hard`). An option that makes a command safe (`--dry-run`, `--staged`) counts only spelled in full or as its letter, and not after its `--no-…`. When a shell, `eval` or `source` appears anywhere in a command, or it runs `.`, every heredoc and here-string in it is judged as a script, wherever it is piped. So every spelling of these is refused:

| Pattern | What it blocks | Safe alternative |
| Rule id | What it blocks | Safe alternative |
|---------|----------------|------------------|
| `git push --force` | Force push | `--force-with-lease` |
| `git reset --hard` | Hard reset | `git stash` or commit first; `git reflog` |
| `git clean -fd` | Clean force | `git clean -nd` (dry run) |
| `git checkout .` | Checkout all | Specific files |
| `git branch -D` | Force delete branch | `git branch -d` |
| `git stash drop/clear` | Drop stashes | `git stash list` first |
| `rm -rf .` / `*` / `.meta` | Dangerous rm | Specific paths |
| `meta.git.force_push` | `git push` with `-f`/`--force` (even next to `--force-with-lease`), a `+refspec`, `--mirror`, `--delete` or `:branch` | `--force-with-lease` on its own |
| `meta.git.reset_hard` | `git reset --hard` | `git stash` or commit first; `git reflog` |
| `meta.git.clean_force` | `git clean` with both `-f` and `-d` | `git clean -nd` (dry run) |
| `meta.git.checkout_dot` | `git checkout .` (any pathspec meaning everything), `checkout -f`, `restore .` (except `--staged` alone), `switch -f` | Specific files |
| `meta.git.branch_force_delete` | `git branch -D`, or `-d` with `-f` | `git branch -d` |
| `meta.git.stash_drop`, `meta.git.stash_clear` | `git stash drop` / `clear` | `git stash list` first |
| `meta.rm.dangerous_paths` | `rm -r` (with or without `-f`) of `.`, `..`, `/`, `~`, `$HOME`, `*`, `.*`; of `.meta`, `.meta.yaml` or `.meta.yml`; of a path with a `.git` part (`.git/*`), where a dot-glob such as `.*` or `.g*` counts as both; of a folder holding `.git`, looked up on disk from the hook's `cwd` and from every folder a `cd` or `pushd` anywhere in the command names (from the `cwd` or from another such folder; relative paths are not looked up when the payload has no `cwd`); any `rm -r` in a command whose `cd` names a folder it can't know (`cd "$DIR"`, `cd $(…)`, `cd -`, a glob); `find` with any argument that has a `.git` part, in any case (`find .git`, `-iname .GIT`), and `-delete` or `-exec rm -r {}` | Specific paths |

Text that only mentions a command (`echo "git push --force"`, a commit message) is not refused. Hook input it cannot read, a Bash call without a command string, a broken policy file and `git -c alias.…` are refused. So is a command it does not model exactly, with "cannot parse this command": an unclosed quote or heredoc; substitutions, `sh -c` or `find -exec` nested deeper than 4 levels; `case` inside `$(…)`; a quote, backslash, backtick, `$(`, `<<` or newline inside `((…))` or `$((…))` (so `$((1<<2))` too); an option it does not know for one of the wrappers above, or a lone `-` (`env - cmd`); an `env -S` string with a quote, a backslash or a leading `-`, or a second `-S`; a shell option starting with `+` (`bash +o posix -c …`). Because a heredoc is judged as a script whenever a shell appears, one that is not valid shell (prose with an apostrophe) is refused in such a command.

**What it can't see.** It judges the text of the command, so it is a safety net, not a sandbox. It does not see: values of variables (`$cmd`, `git reset $flag`, `CDPATH`) or strings `eval` builds at run time; other interpreters (`python -c`, `node -e`) and scripts; git aliases stored in config or set through `GIT_CONFIG_*`; `xargs`, `git submodule foreach`, and `find -delete` on anything but `.git`; `.git` deeper than one level below an `rm -r` target (`rm -rf services` with `services/api/.git`); brace expansion in an `rm` path (`rm -rf {.git,x}`); symlinks (`rm -rf link`); a folder changed by `env -C`, `sudo -D`/`-R` or `git -C` before an `rm`; stdin that is not a heredoc or here-string (`printf … | sh`); and the Write, Edit and MCP tools. If the hook's stdout is closed, the deny it writes is lost and the call goes ahead (Rust's standard library treats writing to a closed stdout as success, and the crate forbids `unsafe`); Claude Code always connects a pipe.

### `agent score`

Expand Down Expand Up @@ -90,30 +94,27 @@ agent score --json

## Configuration

### Guard patterns
### Guard rules

Create `.claude/agent-guard.toml` in your project to customize patterns:
The built-in rules are in the binary. `~/.claude/agent-guard.toml` and then the project's `.claude/agent-guard.toml` change them by id; they never replace them. This file turns one built-in off and adds one rule:

```toml
schema_version = "1.0"

[[patterns]]
id = "custom.my_pattern"
priority = 100
enabled = true
matcher = { type = "regex", pattern = 'dangerous-command' }
message = "This command is blocked because..."
schema_version = "2"

[[rules]] # turn a built-in off
id = "meta.git.checkout_dot"
enabled = false

[[rules]] # add one
id = "custom.no_tf_destroy"
deny = '^terraform destroy\b'
message = "Run terraform destroy yourself."
```

**Pattern options:**
- `id`: Unique identifier (namespaced recommended)
- `priority`: Higher = checked first (default: 100)
- `enabled`: Toggle pattern on/off
- `matcher`: Currently only `regex` type supported
- `validator`: Optional additional checks (`not_contains`, `flags_present`, `args_match_any`, `all_of`, `any_of`, `not`)
- `message`: Shown to Claude when command is blocked
- A built-in id may set only `enabled` and `message`.
- A new id needs `deny`, a regex matched against each command's words after unwrapping (`sudo git -C x push` is matched as `git push`), and `message`.

The guard reads the project file, else `~/.claude/agent-guard.toml`, else its built-in defaults. A file that exists but cannot be read or parsed denies every Bash command, with a reason naming the file, until it is fixed or removed.
A file that exists but cannot be read, is not `schema_version = "2"` (a schema 1.0 `[[patterns]]` file must be rewritten as above), has an unknown key, a bad regex, a `deny` on a built-in id, a repeated id, or a new id without `deny` and `message` denies every Bash command, with a reason naming the file, until it is fixed or removed.

## Development

Expand Down
Loading
Loading