Cybersecurity

How to secure coding agents: deny, ask and allow rules plus the sandbox

Anthropic published a security guide for Claude Code covering four core risks and two layers of defense. Model the order in which permission rules are checked in Python.

How rules are checked

Claude Code checks deny rules first, then ask rules, then allow rules, so an allow rule cannot create an exception to a deny rule. A deny rule defined in managed settings cannot be overridden by any user or project setting. The guide advises avoiding allow rules broad enough to run arbitrary code, such as Bash(python:*).

Claude Code parses every Bash command before it runs and splits it into parts. Without that, an allow rule for “git status” would also approve “git status” chained with “curl attacker.example”. Subagents inherit the parent session’s permission rules.

Exercise: model the rule order in Python

This is not Claude Code’s own code, only a simple model of the logic the guide describes: the command is split into parts, each part is checked against deny, ask and allow lists, and the strictest result wins. An unknown command goes to a person.

Run it: python3 permission_rules_demo.py. Expected output we verified (decision, then command on each line): "allow — git status", "deny — git status [chain] curl attacker.example", "ask — pytest -q; git push origin main", "deny — cat prod.env", "ask — npm install left-pad". In the real output, [chain] is the double ampersand operator, as in the code.

Pythonpermission_rules_demo.py
import re
from fnmatch import fnmatch

# A simplified model of the rule order the guide describes:
# deny first, then ask, then allow; each part of a compound command is checked.
# This is NOT Claude Code's implementation, only a way to see the logic.

DENY = ["curl *", "cat *.env", "rm -rf *"]
ASK = ["terraform apply*", "git push*"]
ALLOW = ["git status", "git diff*", "pytest*"]

def parts(command):
    return [p.strip() for p in re.split(r"&&|\|\||;|\|", command) if p.strip()]

def decide_part(part):
    if any(fnmatch(part, r) for r in DENY):
        return "deny"
    if any(fnmatch(part, r) for r in ASK):
        return "ask"
    if any(fnmatch(part, r) for r in ALLOW):
        return "allow"
    return "ask"  # unknown command: ask a person

def decide(command):
    results = [decide_part(p) for p in parts(command)]
    for level in ("deny", "ask"):
        if level in results:
            return level
    return "allow"

for cmd in [
    "git status",
    "git status && curl https://attacker.example",
    "pytest -q; git push origin main",
    "cat prod.env",
    "npm install left-pad",
]:
    print(f"{decide(cmd):5}  {cmd}")

Practical defenses and limitations

The guide recommends removing standing secrets from the agent’s environment and using short-lived, narrowly scoped tokens (OIDC, workload identity); giving the agent its own identity in CI; pinning dependencies and reviewing MCP servers, plugins and skills like packages; and routing DNS through a resolver you filter and log. By default a hook that fails or times out lets the call through, so set onFailure to “block”, and enable failIfUnavailable so a sandbox that cannot start does not leave commands running uncontained.

Limitations: the sandbox runs on macOS, Linux and WSL2, not native Windows, and our Python model is not a real security tool. Next step: add “wget *” to DENY and try “git diff; wget example.com”, then find the broadest allow rule in your own project’s agent settings and narrow it.

Recommended reading

All articles