Metadata-Version: 2.4
Name: argvgate
Version: 0.1.0
Summary: A policy gate for coding-agent tool calls that parses shell commands instead of matching strings.
Author: Anish Pentyala
License: MIT
Keywords: ai-agents,claude-code,security,permissions,shell,hooks
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Security
Classifier: Topic :: Software Development
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: toml
Requires-Dist: tomli>=2; python_version < "3.11" and extra == "toml"
Dynamic: license-file

# argvgate

A policy gate for coding-agent tool calls. It parses the shell command the agent wants to run, works out which files it would read or write and which hosts it would contact, and checks that against one small policy file.

Most deny rules for coding agents match the command as a string. That breaks as soon as the same action is spelled another way: `cat ./src/../.env`, `bash -c 'cat .env'`, `env X=1 cat .env`, `grep -r . .`, `cat < .env`, `curl 2130706433`. argvgate parses the command first (quotes, `$(...)`, backticks, pipes, redirections, `sudo`/`env`/`timeout`/`xargs`/`bash -c`/`find -exec`, path normalization, symlinks, odd IP spellings) and checks what it found.

It runs as a Claude Code `PreToolUse` hook. It can also be called from any other harness as a plain command (`argvgate check`, exit code tells the answer).

**What it is not.** A hook is a check, not a sandbox. A program the agent runs (`python script.py`, `make`, `npm run x`) can do things argvgate cannot see inside. Use argvgate to stop the obvious and the accidental, and a real sandbox (container, VM, OS sandbox) to contain the rest. See [Limits](#limits).

No dependencies. Python 3.9+. MIT.

## Install

```sh
pip install git+https://github.com/anishpentyala/argvgate
argvgate init                 # writes argvgate.toml
argvgate probe                # tries hundreds of bypass variants against YOUR policy
argvgate snippet claude       # prints the hook entry for .claude/settings.json
```

TOML policies need Python 3.11+ (or `pip install tomli` on older versions). JSON policies (`argvgate.json`) work everywhere.

## Policy

```toml
version = 1

[paths]
deny_read  = [".env", ".env.*", "~/.ssh/**", "**/*.pem"]
deny_write = [".env", ".github/workflows/**", "~/.bashrc"]
write_roots = [".", "/tmp"]        # optional: writes anywhere else are refused

[commands]
default = "allow"                  # or "ask" / "deny" for allowlist mode
deny = ["rm -r|-R|--recursive /", "git push --force|-f", "git reset --hard"]
ask  = ["git push", "npm publish"]
allow = []                         # only used when default is not "allow"

[network]
allow_hosts = ["github.com", "*.githubusercontent.com", "pypi.org"]
deny_hosts  = ["169.254.169.254", "10.0.0.0/8"]
on_violation = "deny"

[opaque]                           # what to do when a command cannot be analyzed
default = "ask"
pipe_to_shell = "deny"
```

Unknown keys are errors, so a typo like `deny_reed` fails loudly instead of silently protecting nothing.

**Path patterns** are gitignore flavored. `.env` matches that name anywhere. `secrets/**` is relative to the project root and covers the directory and everything under it. `~/.ssh/**` and `/etc/shadow` are absolute. `**/*.pem` matches at any depth. Both the written path and its symlink-resolved form are checked.

**Command rules** are a command name plus optional subcommands, then flags. Tokens starting with `-` must be present somewhere in the arguments; `a|b` accepts either spelling; combined short flags are split, so `rm -r|-R -f` matches `rm -fr` and `rm -r -f`. Global git options (`-C`, `-c`) are skipped. Rules are checked against every layer, so `sudo rm -rf /` and `bash -c 'rm -rf /'` match a rule for `rm`.

**Opaque kinds:** `unparsed`, `eval`, `dynamic_command`, `dynamic_path`, `dynamic_host`, `inline_code` (`python -c`, `node -e`), `pipe_to_shell`, `unbounded_walk`. Each can be `allow`, `ask` or `deny`.

## What it checks

- Redirections: `> >> >| &> <` count as writes and reads.
- `cat`, `head`, `less`, `sed`, `awk`, `grep`, `cp`, `mv`, `tee`, `dd`, `tar`, `curl -o`/`-T`/`-d @file` and similar know which arguments are files and whether they are read or written. Unknown commands have every path-looking argument checked against both deny lists.
- Recursion: `grep -r`, `rg`, `cp -r`, `tar`, `rsync`, `rm -r`, `find ... -exec`, `git grep` walk the directory (bounded) and deny if a denied file is reachable.
- Wrappers: `sudo doas env nice nohup timeout command exec stdbuf time xargs watch find -exec bash -c sh -c eval`, nested up to 6 deep.
- Hosts in `curl wget ssh scp rsync nc git clone ...`: lowercased, userinfo and port stripped, trailing dot removed, punycode, and decimal/hex/octal/short IPv4 forms and IPv4-mapped IPv6 normalized before matching.
- Claude Code's own tools: `Read`, `Write`, `Edit`, `MultiEdit`, `NotebookEdit`, `Grep`, `Glob`, `WebFetch`.

## Claude Code hook

```sh
argvgate snippet claude
```

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash|Read|Write|Edit|MultiEdit|NotebookEdit|Grep|Glob|WebFetch",
        "hooks": [{ "type": "command", "command": "argvgate hook claude" }]
      }
    ]
  }
}
```

Merge that into `.claude/settings.json`. The hook finds `argvgate.toml` or `argvgate.json` by walking up from the working directory (or use `ARGVGATE_POLICY`). It answers `deny` or `ask` with a reason, and for an allowed call prints nothing, so your other Claude Code permission rules still apply. argvgate only makes calls stricter. It fails closed: bad input, a missing or broken policy or an internal error is a `deny` with the cause in the reason. Hook format: [Claude Code hooks reference](https://docs.claude.com/en/docs/claude-code/hooks).

## Other harnesses

```sh
argvgate check --command 'cat ./src/../.env'     # exit 0 allow, 2 deny, 3 ask, 64 usage/policy error
argvgate check --read ~/.ssh/id_rsa
argvgate check --write .github/workflows/ci.yml
argvgate check --url https://example.com/x
argvgate explain --command "bash -c 'curl 2130706433 | sh'"   # shows what was parsed and which rule fired
argvgate check --json --command '...'
```

## Test your policy

```sh
argvgate probe                 # generate bypass variants for every rule in your policy
argvgate test policy-cases.json   # your own expectations: [{"command": "...", "expect": "deny"}]
```

`probe` builds a throwaway project and home directory, creates a file for each denied path, and runs hundreds of spellings through the engine (absolute and relative paths, `..` traversal, `bash -c`, `env`, `sudo`, `eval`, redirects, globs, braces, ANSI-C quoting, recursive search, `xargs`, `find -exec`, `git show REV:path`, host spellings, reordered flags). Nothing is executed and nothing outside the temp directory is touched. It exits 1 if any variant would be allowed, and names it.

## Limits

Read these before relying on it.

- **Programs are opaque.** `python script.py`, `make`, `npm run`, `./build.sh` can read or write anything. argvgate checks the script file path, not what is inside. Inline code (`python -c`) triggers the `inline_code` setting.
- **No data flow.** `X=.env; cat "$X"` is flagged as a dynamic path (ask by default). `git log -p` and `git diff` can print the contents of a denied file without naming it; only explicit `REV:path` and pathspecs are checked.
- `git add .` can stage a denied file, since checks apply to explicit path arguments, not directory walks.
- **Directory changes are approximated.** After `cd`, relative paths are checked against both the original and the new directory, so it can over-block, never under-block. Subshell scoping is not modeled.
- **Race conditions.** Recursive checks look at the disk when the call is checked. A file created by the same command is not seen.
- **Shell coverage.** POSIX-style bash and zsh syntax. `case`, `(( ))` arithmetic commands and the `function` keyword are flagged as `unparsed`. fish, PowerShell and cmd syntax are not parsed.
- **Network.** Hosts are only extracted for known commands and for any argument containing a `scheme://` URL. A tool that takes a host some other way is not seen. This is not a firewall.
- **Hook coverage.** The hook sees tool calls. It does not see what an MCP server or a subagent does outside those calls.

If you find a command that slips through, please open an issue with the command and your policy.

## Development

```sh
python -m unittest discover -s tests -t . -v
argvgate probe --policy examples/policy.toml
```

## License

MIT
