Guardrails for a Coding Agent: Allow, Ask and Deny Rules, a Git Hook, and What Never Gets Automated
Without guardrails the agent commits a red build when you say 'just commit it' and offers to push to main. A settings file lets mvn test run alone, asks before git commit, denies git push and .env, and a guard-git.sh hook blocks --no-verify before any rule is read.
What you will learn
- Write allow, ask and deny permission rules in .claude/settings.json
- Block a command no rule can describe with a PreToolUse hook that exits with 2
- Add a git pre-commit check that stops a red build for people and agents
- Keep a short never-automated list in AGENTS.md
Before you start
- Reading an AI Agent's Diff
- The Project Context File: AGENTS.md and CLAUDE.md
What may an AI coding agent do on its own, what should it ask first, and what should it never do? Here are real guardrails for a small Spring Boot project: permission rules, a hook, a git check, and a short list a person always writes.
Nothing stopped it
Start with a copy of the project that has no guardrails and a bug in the controller (a missing task now returns 200 instead of 404). The agent runs mvn test: 3 tests run, 1 failure. You're in a hurry, so you say "just commit it", and it does. No question, no prompt; the broken commit is in. It even offers to push to main. And the .env file with the database password sits right there in the project. The agent did nothing wrong. It simply had no guardrails.
A key card, not the master key
A new intern gets a key card. The kitchen opens: allow. The meeting room asks first: ask. The server room stays locked: deny. An agent gets the same three words. Here's the project's .claude/settings.json, in Claude Code's format:
{
"permissions": {
"allow": [
"Bash(mvn test)",
"Bash(mvn test *)",
"Bash(git status)",
"Bash(git diff *)"
],
"ask": [
"Bash(git commit *)",
"Edit(pom.xml)"
],
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)",
"Edit(.githooks/**)",
"Edit(.claude/**)",
"Bash(git push *)"
]
},
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "bash \"${CLAUDE_PROJECT_DIR}/.claude/hooks/guard-git.sh\""
}
]
}
]
}
}
The tests, git status and git diff run on their own. A commit or an edit to pom.xml stops for a permission prompt. Reading .env or anything under secrets, editing the guardrail folders themselves, and git push never run. Codex and Cursor have the same idea under other names.
A hook catches what a rule can't
A rule matches a command's shape. Some commands are dangerous because of one flag, so the settings also register a PreToolUse hook for every Bash call:
#!/bin/bash
# PreToolUse hook for Bash: git commands no agent runs in this repository, whatever the prompt says.
# The agent's tool call arrives as JSON on stdin. Exit 2 blocks the call and stderr goes back to the agent.
input=$(cat)
if printf '%s' "$input" | grep -Eq 'git +commit.*(--no-verify|[[:space:]]-n([[:space:]]|"|$))'; then
echo "Blocked by guard-git.sh: commits must run the pre-commit checks. Fix the failing test instead." >&2
exit 2
fi
if printf '%s' "$input" | grep -Eq 'git +push.*(--force|[[:space:]]-f([[:space:]]|"|$))'; then
echo "Blocked by guard-git.sh: no force pushes from an agent." >&2
exit 2
fi
if printf '%s' "$input" | grep -Eq 'git +reset +--hard'; then
echo "Blocked by guard-git.sh: git reset --hard throws work away. Ask the developer." >&2
exit 2
fi
exit 0
The tool call arrives as JSON on stdin. Exit code 2 blocks it, and what the script prints to stderr goes back to the agent, so it fixes the failing test instead of skipping the check. There's a longer walk through this pattern in hooks that block destructive actions.
Every command passes four checks
With the guardrails in place: "Run the tests" runs mvn test alone. "Commit the change" hits the ask list and you get a permission prompt. "Push it to main" is caught by Bash(git push *) and never runs. "Commit again, but skip the pre-commit checks" becomes git commit --no-verify ..., and the hook stops it before any list is read.
A git check for people and agents alike
#!/bin/bash
# Git runs this before every commit (once per clone: git config core.hooksPath .githooks).
# A red build stops the commit, for people and agents alike.
echo "pre-commit: mvn test"
if ! mvn -q test > /dev/null 2>&1; then
echo "pre-commit: tests failed - commit blocked. Run mvn test to see why." >&2
exit 1
fi
echo "pre-commit: tests passed"
Turn it on once per clone with git config core.hooksPath .githooks. A red build now stops the commit whoever types it. That's why --no-verify, which skips this check, is the one flag the agent hook watches for.
The short list a person always writes
## Never automated (a person writes and approves these)
- Login, roles and every other security setting
- Secrets and credentials: they never enter the repository or a prompt
- Merging to main and releasing
The guardrail files (`.claude/`, `.githooks/`) are denied to the agent in `.claude/settings.json`.
Ask for a login endpoint with roles and the agent may draft it, then stops for your review. Secrets never enter the repository or a prompt; the project ships a .env.example with empty keys, the real .env is in .gitignore, and reading it is denied. Merging to main and releasing stay with you. Never paste a secret into a prompt.
Common mistakes
- Allowing
Bash(*)to stop the prompts. That's the master key. - Leaving
.claude/editable. An agent that can edit its own settings can loosen them. - Trusting a deny rule to catch a flag inside an allowed command. That's what the hook is for.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
CLAUDE.md, settings.json and Plugins: Which Claude Code File Advises, Which Enforces, Which Packages
Three needs in one repo: a test command, a rule that Claude must never read .env, and a shared deploy skill. See which file each belongs in, how the settings layers merge, and what a plugin manifest carries.
Authoring a Claude Skill: SKILL.md, Frontmatter, Progressive Disclosure and the Description That Triggers It
Pack a support team's ticket-triage routine into one skill folder, then watch three requests load one, two or all three levels: the catalog, the SKILL.md body, and a script whose code never enters the context.
One Change, Four Environments: Where a Claude Config Edit Lands in Dev, Staging, Prod and CI
Change one line in a repo and ask where it lands. A small resolver for bookshop-bot shows that a new model id in settings.json reaches only CI, a prompt bump reaches dev and CI, and a prod block edit reaches only prod.
Claude in the SDLC: Prompts in Git, a Change Gate by Path, a Claude Review That Advises and a Person Who Merges
Follow one prompt edit from a branch to a merge: version prompts and the model pin like code, keep the API key in GitHub Secrets, gate changes by path, let Claude review the diff, and leave the merge to a person.
