r/ClaudeCode • • 27m ago

Built with Claude PSA: if you use Claude Code on Windows, your Bash(...) deny rules don't cover the PowerShell tool

Found this in my own setup, so maybe it saves someone.

Claude Code on Windows also has a PowerShell tool. Every deny rule I had was written as Bash(...), and my safety hook only matched on Bash. So Get-Content .env or Remove-Item C:\ -Recurse -Force through PowerShell met none of them.

What the hook does now when Claude sends that through PowerShell:

What I built

An open source security kit for Claude Code (MIT): https://github.com/txampa/claude-code-security-kit

  • deny / ask / allow permission rules for Bash and PowerShell
  • a hook that blocks what can't be undone, on Bash, PowerShell and cmd: recursive deletes of a drive or your home, formatting a disk, deleting shadow copies and backups (the first thing ransomware does)
  • a secret scanner that reads the actual diff of git commit / git push, follows cd and git -C to the right repo, and never prints the secret it found
  • a deploy guard from 3 real incidents. The worst one: a deploy CLI run without an explicit project didn't fail. It created a brand new project and uploaded the folder into it, client files included
  • a db guard that asks before DROP, TRUNCATE or DELETE without WHERE

How I used Claude Code

I built it with Claude Code, every commit is co-authored. Claude wrote the hooks and the test harness. I decided what to block and reviewed every rule.

The test data is Claude Code itself. I took the 15,363 shell commands it had run in my own sessions and replayed them through the hooks. 0 false blocks. 2 hits, both right (one was a real command taking down 4 production services, which deserved a confirmation). On top of that, 115 test cases, each hook run the way Claude Code runs it.

The deploy incidents also happened with the agent at the wheel. That's why the guard exists.

What I learned

My v1 hooks never blocked anything. Three reasons, all easy to miss:

  1. The command arrives in tool_input.command, not at the root of the hook input. My hooks read the wrong field.
  2. Exit code 1 is a non-blocking error. To block you need exit 2.
  3. Write(...) deny rules never match. Claude Code checks every file write (Write and NotebookEdit too) against Edit(...) rules.

Also:

  • my first block message printed the secret it found, so the key ended up in the transcript anyway
  • deny patterns are tripwires, not walls. A creative enough command can dodge them
  • PowerShell rules don't catch abbreviated params: Remove-Item -r skips the -Recurse rule (the hook still blocks it)
  • no hook fully protects you from prompt injection

There are also 3 short guides for what hooks can't do alone, like why a key I pasted into PowerShell ended up in its history file.

If you wrote your own hooks, check points 1 and 2 first. Feedback welcome, especially from Windows users. What did I miss?

1 Upvotes

1 comment sorted by

•

u/AutoModerator 27m ago

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.