Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 3 min read

4 Ways AI Coding Agents Bypass .env Rules (And How to Stop Them in

If you run autonomous coding agents like Claude Code, Cursor, Codex CLI, or Windsurf, you have probably added a quick hook or rule to block reading .env files. The problem is that naive string/glob rules are surprisingl

4 Ways AI Coding Agents Bypass .env Rules (And How to Stop Them in

If you run autonomous coding agents like Claude Code, Cursor, Codex CLI, or Windsurf, you have probably added a quick hook or rule to block reading .env files.

The problem is that naive string/glob rules are surprisingly easy for an autonomous agent (or a prompt-injected workflow) to bypass β€” often unintentionally while debugging.

While building HookGuard, a local <50ms PreToolUse / PostToolUse guardrail for AI coding agents, community feedback pushed me to red-team four specific bypasses. Here is how each one works and how to close it locally with zero external dependencies.

HookGuard Terminal Demo

1. The Symlink Bypass (ln -s .env leak.txt)

If your file-read rule only matches **/.env against the literal path passed to read_file, an agent can run:

ln -s .env leak.txt

…and then read leak.txt right past the rule. Even sneakier: if you allow-list .env.example, a symlink .env.example -> .env can exploit your allow rule!

The Fix

Before evaluating file rules, resolve the path with fs.realpathSync (falling back to the nearest existing parent directory for new writes, plus ~ expansion) and evaluate against both the original path and the canonical path:

  • block / warn rules trigger if either the original or canonical path matches.
  • allow rules only pass if all candidate paths match (so .env.example -> .env never short-circuits protect-env-files).
  • Also block shell commands creating links to sensitive files (ln -s, mklink, New-Item -ItemType SymbolicLink|HardLink|Junction).

2. Shell Globs & Obfuscated Reads (cat .en*)

A regex checking for cat .env completely misses:

cat .en*
cat .env?
Get-Content .en*
head *.pem
cat .e""nv

The Fix

Block wildcard and quote/escape-obfuscated read commands targeting .env, .pem, .key, and id_rsa, while using a negative lookahead (?!\.(?:example|sample|template)\b) so reading .env.example stays frictionless without needing a broad command-level allow rule that could be abused with &&.

3. Git History & Raw Blob Leaks (git show HEAD:.env)

Even if .env is protected on disk (or was deleted after an accidental commit), git still knows about it:

git show HEAD:.env
git log -p -- .env
git diff HEAD~1 .env
git cat-file -p <blob-hash>

The Fix

Block git show, git log, git diff, git blame, and git grep when targeting sensitive file patterns (excluding .env.example/.sample/.template), and block raw git cat-file blob dumps.

4. Config & Hook Tampering

An agent trying to "unblock itself" during a long run can simply edit or delete the guardrail config:

sed -i 's/enforce/audit/' hookguard.yaml
echo '{}' > .claude/settings.json

The Fix

Protect hookguard.yaml/yml, .claude/settings.json, .cursor/hooks.json, .agents/hooks.json, .codex/hooks.json, and ~/.hookguard/* against both file_write tools and mutating shell commands (rm, mv, sed -i, >, Set-Content), while still allowing read-only inspection (cat hookguard.yaml).

Bonus: Fast (<5ms) Secret DLP on Code Writes

Blocking .env reads doesn't stop an agent from hardcoding a token (sk-..., ghp_..., AKIA..., AIza..., xox..., PEM private keys, or JWTs) directly into src/config.ts or a curl command.

Running a lightweight zero-dependency regex + optional Shannon entropy scan on file_write payloads and shell arguments catches hardcoded secrets before they touch disk β€” and redacts the matched value (ghp_12...wxyz) so raw secrets are never written to logs.

Try It Locally (MIT, Zero C++ Build Deps)

I packaged all of these guards β€” plus retry-loop detection, token/turn budgets (with exact JSONL transcript ingestion), hookguard test --replay, and a live SSE web dashboard over Node's built-in node:sqlite β€” into HookGuard (v0.3.0).

It supports 8 agents out of the box (Claude Code, Cursor, Codex CLI, Antigravity, Gemini CLI, Windsurf, OpenCode, and Cline/Roo Code):

npm install -g hookguard-cli
hookguard init
hookguard doctor

Test any command or symlink against your rules without touching your repo:

hookguard test --cmd "cat .en*"

If you've run into other creative ways coding agents bypass local hooks, I'd love to hear them in the comments!

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.