Sub topic 2
Writing Allow and Deny Rules, and Where Patterns Break
Assume you can use a terminal, basic Git commands, and an AI coding assistant that edits files or runs commands. A permission rule maps a proposed action to allow, ask, or deny. Evaluate deny rules before allow rules so a dangerous command cannot be approved by a broad match.
from fnmatch import fnmatch
DENY = ["rm -rf *", "git push --force*"]
ALLOW = ["git status", "git diff*"]
def decide(command):
if any(fnmatch(command, rule) for rule in DENY):
return "deny"
if any(fnmatch(command, rule) for rule in ALLOW):
return "allow"
return "ask"Pattern matching is only a first guard. A rule such as git diff* may allow git diff; curl example.com if the shell interprets the whole string. Reject chaining, redirection, substitution, and newlines before applying simple patterns. Prefer structured tool arguments over raw shell strings.
Scope and Duration
A grant can apply once, for the current session, or permanently. Use the smallest scope that supports the task.
| Grant | Useful for | Main risk |
|---|---|---|
| Once | A single unusual action | Repeated prompts |
| Session | A trusted task in one workspace | Mistakes persist longer |
| Always | Stable, low-risk commands | Future tasks inherit access |
Sandboxing
A permission prompt asks whether an action may run. A sandbox limits what the process can reach even after approval. It can restrict paths, network access, child processes, and credentials. Use both: rules express intent, while a sandbox enforces boundaries.
Prompt Injection
Prompt injection occurs when untrusted text, such as a repository file or web page, gives the agent instructions. Treat that text as data. Do not let it silently change permission decisions or request secrets. Keep sensitive files outside the workspace or deny access to them.
Approval Fatigue
Frequent prompts reduce review quality. People may approve without reading, especially when harmless and dangerous actions look similar. Group low-risk actions carefully, deny known-dangerous patterns, and require approval for network access, credential use, destructive commands, and writes outside the workspace.
Exercise
Why should a git diff* allow rule reject command chaining?
Show answer
Because a compound command can run an additional operation, such as network access, that the rule did not intend to approve.