Developers Use PreToolUse Hooks to Stop Claude Code from Overwriting .env
11/15/2026 — 11/16, 09:17·1 sources (all)·2 reports (all)
Key takeaways
- A developer on DEV Community has shared a Claude Code PreToolUse hook that reads tool-call JSON from stdin and blocks writes with exit code…
Background & analysis
On November 15, 2026, a developer published a retrospective on DEV Community about moving mandatory rules out of prompts and into Claude Code hooks. In the piece, the developer explained that a rule written in CLAUDE.md is a request the model usually follows, whereas a Claude Code hook is a handler the harness runs every single time a lifecycle event fires. That distinction made hooks the right place for requirements that must never be skipped, such as blocking access to secret files, formatting edited code, or refusing to finish when a type-check is failing, while CLAUDE.md should be kept for guidance and context only. The developer noted that for months their CLAUDE.md had stated never to touch .env files and to run the formatter after editing, and Claude Code honored both most of the time, but not every time.
On November 16, 2026, the same developer shared a concrete PreToolUse hook designed to stop Claude Code from overwriting .env files. Permission deny rules can already stop Claude Code from reading .env and key files, the developer pointed out, but that still leaves a hole: a Write or Edit call can create or overwrite those paths. The hook, saved as .claude/hooks/block-secret-writes.sh and made executable with chmod +x, reads the tool call as JSON on stdin and extracts .tool_input.file_path or .tool_input.path with jq. When the path matches .env or files such as .pem, .key, .p12, or .pfx, the script blocks the write with exit code 2 and returns the reason to the model; it is then registered for use. The material does not describe any later adoption or updates to the approach, so the work stops at this point.
AI-generated from 2 reports · updated 1 day ago
Latest turnA developer on DEV Community shares a Claude Code PreToolUse hook: a script reads the tool-call JSON from stdin and exits with code 2 when the path matches .env or files such as .pem and .key, blocking the write and returning the reason to the model. The author notes that read deny rules alone still leave Write or Edit calls able to create or overwrite those files.
Related AI tools
- Heat index
- 46
- All sources
- 1
- All reports
- 2
- First seen
- 2 days ago
Reports on this story headlines open the original
A developer on DEV Community shares a Claude Code PreToolUse hook: a script reads the tool-call JSON from stdin and exits with code 2 when the path matches .env or files such as .pem and .key, blocking the write and returning the reason to the model. The author notes that read deny rules alone still leave Write or Edit calls able to create or overwrite those files.
DEV Community · AIAI score 63
Writing on DEV Community, a developer argues that rules in CLAUDE.md are requests the model usually follows, while a Claude Code hook is a handler the harness runs every time a lifecycle event fires. He moved must-never-skip rules such as blocking .env files and running the formatter after edits into hooks, leaving CLAUDE.md for guidance and context.
DEV Community · AIAI score 57
Other stories people are talking about
- Heat index 92NewDeveloper benchmarks Claude Code plugins across four tech stacks1 sources
- Heat index 90NewDeveloper builds a five-step post-generation check pipeline around Claude Code1 sources
- Heat index 111Claude Code Code Mode can be enabled via an environment variable2 sources
- Heat index 41Goldman Sachs revokes trader's Claude Code access, sparking workflow debate1 sources
- Heat index 85NewNadella says Claude Code caught Microsoft off guard as IDEs fade1 sources
How is heat calculated?About the methodHide
Heat counts how many independent sources covered a story in the last 48 hours: one source counts once no matter how many posts it published, decaying with a 24-hour half-life. What ranks first is what many people are talking about.
This page aggregates public feeds. Headlines and summaries are machine-organized and remain the property of the original authors; verify important facts at the source. If you believe a headline or summary infringes your rights, email the contact address in the footer with the page URL and basis of your claim, and we will remove or replace it after review.
- Surge
- Discussion rising fast
- New
- First report within 6 hours
- Rising
- Still gathering discussion

