# One CLAUDE.md Doesn't Fit Every Project: Split Your Templates by Situation, Not by Stack

> Source: <https://dev.to/osaka_dx_engineer/one-claudemd-doesnt-fit-every-project-split-your-templates-by-situation-not-by-stack-1jfi>
> Published: 2026-11-23 14:12:07+00:00

**Recommendation:** Pair settings.json deny rules on .env with a PreToolUse Bash hook that exits 2 on commands touching .env, .envrc or credentials files, stripping .env.example before matching.
More in the playbook "Securing agents and AI tools": <https://wpnews.pro/playbooks/agent-security>

Most CLAUDE.md templates you'll find are organized by stack: one for React, one for Laravel, one for Rails. When I started using Claude Code for freelance client work, I collected a few of those — and then barely used them.

The problem wasn't the stack sections. It was that the *working rules* I actually needed had almost nothing to do with the framework. They depended on the situation the project was in:

Same stack, three completely different rule sets. So I reorganized my templates around situations instead. This post walks through what changes between them, and then covers a second, more mechanical safeguard: a hook that stops `.env` files from leaking into the session through Bash.

On a new project, Claude Code will happily invent conventions if you don't provide any — a different error-handling style in every file, tests for some features and not others. The template's job is to be opinionated early, while decisions are still cheap:

Inherited codebases flip most of that on its head. Here the expensive mistake isn't inconsistency, it's breaking something that was working in production. The rules get defensive:

Notice that none of this mentions a framework. A legacy Laravel app and a legacy Next.js app need almost identical rules, because the risk profile is the same.

Billable work adds a layer that has nothing to do with code at all, and it's the layer where freelancers actually lose money. My client-project template carries it alongside the technical rules:

A few habits that made this work in practice:

`<angle brackets>` is a rule Claude can't act on yet. Project name, actual build/test commands, where backups live — the specifics are the point.`~/.claude/CLAUDE.md`. Rules that are about CLAUDE.md is instruction, not enforcement — it guides judgment, but it isn't a lock. Secrets are the clearest example of why that distinction matters.

The obvious safeguard is permission rules in `.claude/settings.json`: deny `Read` and `Edit` on `.env` files. That covers the file tools. It does **not** cover Bash. Secrets leak through shell commands all the time:

```
cat .env
grep -r "API_KEY" .
cp .env /tmp/backup/
```

A PreToolUse hook can watch the Bash tool specifically and block any command that touches `.env`-style files. Here's the one I use, in full:

``` bash
#!/usr/bin/env bash
# PreToolUse hook: keep .env files and credential files out of Claude's hands.
#
# Why a hook in addition to the Read/Edit deny rules in settings.json:
#   Permission rules govern the file tools, but secrets also leak through Bash —
#   `cat .env`, `grep -r KEY .`, `cp .env somewhere`. This hook watches Bash commands
#   that touch .env-style files and blocks them.
# How .env.example is handled:
#   The example file (dummy values, meant to be shared) is stripped from the command
#   string before matching, so `cat .env.example` passes. Anything left that still
#   mentions a real .env file is blocked — including `cp .env.example .env`. That one
#   is a legitimate first-run setup step; run it manually in your own terminal.
#   Creating the real secrets file is a human moment anyway.
# How it works:
#   Same pattern as block-dangerous-bash.sh — JSON on stdin, .tool_input.command,
#   exit code 2 blocks execution.
# Requires: jq must be installed.
# How to adjust: if your project stores secrets under a different name (.envrc,
#         credentials.json, service-account.json), add it to the pattern list below.

set -euo pipefail

input=$(cat)
tool=$(printf '%s' "$input" | jq -r '.tool_name // empty')

# Only Bash needs this hook; the file tools are covered by settings.json deny rules.
[ "$tool" = "Bash" ] || exit 0

command=$(printf '%s' "$input" | jq -r '.tool_input.command // empty')

# Remove the shareable template name before matching, so it alone never triggers a block.
rest="${command//.env.example/}"

case "$rest" in
  *".env"* | *".envrc"* | *"credentials.json"* | *"service-account"*.json* | *".npmrc"* | *".aws/credentials"*)
    echo "Blocked by hook (possible secret access): ${command}" >&2
    echo "Commands that touch .env / credential files are blocked. If you need a value from one, ask the user to provide it another way; if you were creating .env from .env.example, run that one command manually." >&2
    exit 2
    ;;
esac

exit 0
```

Wire it up in `.claude/settings.json` like this:

```
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/guard-env-files.sh" }
        ]
      }
    ]
  }
}
```

Save the script under `.claude/hooks/`, `chmod +x` it, and make sure `jq` is installed. Exit code 2 from a PreToolUse hook blocks the tool call and feeds the stderr message back to Claude, so it understands *why* it was stopped and can suggest an alternative instead of retrying blindly.

Two design choices worth pointing out:

`.env.example` stays readable.`cat .env.example` passes.`cp .env.example .env` is blocked on purpose.
And when you genuinely need to know what's in an `.env` — usually it's *which keys exist*, not their values — read it yourself and paste the key names into the conversation. The values rarely need to enter the chat at all.

If your project keeps secrets under different names (`.envrc`, `credentials.json`, a service-account file), add them in both places: the settings.json deny list *and* the hook's pattern list. They guard different doors — file tools versus Bash — and a secrets file needs both doors watched.

These templates and hooks are the core of a kit I put together for my own client work and now sell as the **Claude Code Freelance Field Kit** ($29 on Gumroad): the four situation-based CLAUDE.md templates, five skills (bug investigation, code review, refactor planning, test-adding, handover documents), three hooks including the one above and a dangerous-command blocker, plus the client-work paperwork — estimate research, progress update, and scope clarification prompts, delivery/security/handover checklists, and work-log and estimate templates.

Full disclosure: I'm the seller. The two ideas in this post — situation-based templates and the .env guard hook — are complete as they stand, whether or not the kit is useful to you.

[https://osakadxlabo.gumroad.com/l/claude-code-freelance-field-kit](https://osakadxlabo.gumroad.com/l/claude-code-freelance-field-kit)

*Disclosure: This post was drafted with AI assistance and reviewed and edited by me. The configurations and hook script are from my own working setup.*
