Claude Code Permissions: Securing .env Files, Git, and Dangerous Shell Commands
A practical guide to least-privilege permissions in Claude Code: isolating .env files, gating destructive Git and shell operations, and validating access boundaries in a test workspace.
When configuring Claude Code on production repositories, developers often encounter a frustrating trade-off: either constantly confirm trivial read commands, or enable full bypass mode (--dangerously-skip-permissions) and risk accidental .env leaks, unintended git push --force executions, or broken project dependencies.
Neither extreme is effective. To troubleshoot permission errors and establish safe AI coding practices, build your setup on the principle of least privilege: clearly delineating read access, write permissions, shell execution boundaries, and version control operations.
1. The 4-Tier Permission Map
Organize tools and commands into four distinct access tiers to fix security vulnerabilities:
2. Isolating .env Files and Project Secrets
Transport Layer Security (TLS) encrypts API traffic over the wire, but it does not prevent confidential credentials from entering the model context window, error logs, or handoff summaries.
To troubleshoot and ensure Claude Code does not ingest private credentials, apply these fixes:
- Add all environment files containing secrets to
.gitignore. - Provide a clean
.env.exampletemplate with dummy values so the agent understands variable names without seeing runtime secrets. - Add a strict boundary rule to
CLAUDE.mdorAGENTS.md:
Secret Protection Rule
- Never inspect, echo, or output contents from
.env,.env.local, or private key files. - To check required configuration keys, inspect
.env.example.
3. Prioritized Checklist to Troubleshoot and Control Commands
When executing tasks with Claude Code, check first and enforce a clear evaluation hierarchy to prevent root cause errors:
Step-by-Step Guardrails:
- Step 1: Gate Git mutations. Never allow automated direct pushes to
mainwithout a manualgit diff --checkand test run. - Step 2: Isolate package installation. Commands like
npm install <package>or remote curl scripts must be verified manually to prevent supply-chain vulnerabilities and fix untrusted dependencies. - Step 3: Constrain directory scope. Restrict the agent workspace to a specific feature folder or dedicated Git worktree.
4. Validating Permissions in a Test Workspace
Before granting Claude Code access to critical repositories, perform a sandbox validation to troubleshoot and verify security boundaries:
- Create a disposable test branch:
git checkout -b test/permission-check. - Add a dummy
.envfile containing a fake test secret. - Prompt the agent to refactor the authentication module.
- Verify:
- The agent does not read the dummy
.envfile during discovery; - No secret tokens leak into error logs, comments, or
git diff; - Unit tests run without accessing unauthorized files.
- Once verified, clean up the test branch.
This structured permission boundary prevents credential leakage while preserving fast, autonomous agent execution.