Skip to content

Set Up Claude Code Guardrails

Category AI Security
Status โœ… Completed
Repo nextwork-set-up-claude-code-guardrails
Original NextWork guide nextwork.ai

Learning project

Practice work following a NextWork guide, separate from my production projects.

๐Ÿ“š NextWork learning project. This is a practice project I built while working through NextWork's Set Up Claude Code Guardrails guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning โ€บ AI Security
Status: โœ… Completed

Overview

An AI coding agent with a terminal can delete files or leak secrets. This repo layers three defenses: permission rules (deny .env/secrets/, sudo, curl, force-push), hooks that inspect every edit and Bash command before it runs (catching tricks prefix rules miss, like cd /tmp && rm -rf * or base64 -d | sh), and a CLAUDE.md policy that tells the agent the rules.

Summary

The .claude/settings.json deny/ask/allow rules, two PreToolUse hooks, and the CLAUDE.md security policy are built, and the secret-mission red team (25 attack and allow cases) passes. In a live Claude Code session told to run git status, cat .env, rm -rf ~, a curl-pipe-to-shell, and to Read .env, only git status ran: the hook blocked the three attacks and the deny rule blocked the Read.

The checks behind this are in docs/verification.txt.

Skills & tools

Claude Code, Hooks, Permission rules, Red teaming, Python, pytest

What I built

  • .claude/settings.json: deny/ask/allow permission rules plus PreToolUse hook wiring.
  • .claude/hooks/validate_edit.py: blocks edits to secrets, keys, and the guardrails themselves, and blocks content that looks like a credential.
  • .claude/hooks/validate_bash.py: blocks broad recursive deletes, sudo, curl | sh, secret exfiltration, force-push, chmod 777, disk writes, and hidden base64 payloads.
  • CLAUDE.md: the security policy.
  • tests/test_guardrails.py: the red-team suite (secret mission).
  • docs/claude-code-session.md: the live attack drill in Claude Code, with each hook block message.

How to run

pip install pytest && pytest -q        # red-team the hooks
cp -r .claude CLAUDE.md /path/to/your/project/   # then start claude in that project

Verification

I ran the verification command on 2026-12-02. The full output is in docs/verification.txt.

PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -p no:cacheprovider tests && test -s docs/claude-code-session.md && grep -q 'Tool call' docs/claude-code-session.md && echo 'OK live Claude Code transcript present' && grep -q 'Blocked (reading secrets)' docs/claude-code-session.md && grep -q 'denied by your permission settings' docs/claude-code-session.md

Notes

Model substitution: the guide uses Claude Code with a Claude subscription. I don't have a Claude account, so I ran the real Claude Code CLI (v2.1.335) headlessly and pointed it at a substitute model, either a local Ollama model or a free OpenRouter model, through the Anthropic-compatible API (ANTHROPIC_BASE_URL). Claude Code's skills, hooks, settings, and tools behave the same; only the model answering is different. The full session transcript is in docs/claude-code-session.md.


Original project: https://nextwork.ai/projects/f961f28a-8128-510c-9a81-29f5c578b4c9 ยท Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -p no:cacheprovider tests && test -s docs/claude-code-session.md && grep -q 'Tool call' docs/claude-code-session.md && echo 'OK live Claude Code transcript present' && grep -q 'Blocked (reading secrets)' docs/claude-code-session.md && grep -q 'denied by your permission settings' docs/claude-code-session.md

..........................                                               [100%]
26 passed in 0.44s
OK live Claude Code transcript present

[exit code 0]