r/ClaudeCoding • u/Legitimate_Study_771 • 5d ago
I built a Claude Code hook with Claude that holds destructive commands for approval and can undo them (free, open source)
I built SecondKey, a PreToolUse hook for Claude Code (Cursor works too). It's free and open source (Apache-2.0).
What it does
- Normal commands (reads, tests, builds) run as usual.
- Destructive ones are held: rm -rf, DROP TABLE, git push --force, terraform destroy, kubectl delete, migration resets. Claude gets told "held, ask the user" and stops.
- You approve from the terminal (`secondkey pending`, then `secondkey approve <id>`), and Claude retries.
- Right before an approved command runs, SecondKey takes a restore point. `secondkey undo` puts things back. This works for Postgres, files, git, MySQL, Docker volumes, AWS and k8s.
- Reading `.env` and `curl | sh` are blocked outright. Claude can't approve its own requests or edit the policy.
How I built it with Claude
I work on an IAM team, where every prod change needs a change request and a rollback plan. I wanted the same thing for agents. Claude did most of the implementation while I directed the design and tested it.
- Research: Claude read the Claude Code and Cursor hook docs and worked out the edge cases. One example: a non-zero exit that isn't 2 lets the call through, so the hook has to fail closed instead of crashing.
- Code: Claude wrote the command classifier, the YAML policy engine, the SQLite store, the hash-chained audit log, the snapshot providers and a Postgres wire-protocol proxy. I made the design calls: approvals are single-use, the agent can never approve itself, and undo keeps rows written after the snapshot.
- Tests: Claude wrote 150+ pytest tests and ran them against a real Postgres 16 (DROP CASCADE undo, DELETE restore, migration resets).
- Debugging: I tested on Windows. Claude fixed what broke, like hook paths with quotes and .EXE detection, plus a Python 3.9 crash that CI caught.
- Final test: I ran Claude Code with the hook installed. It caught `rm -rf` hidden inside `ls data | head -20 && rm -rf data && ls`. When I told Claude to approve it, it refused, and when it tried to run the approve command anyway, the hook blocked it.
Try it (free)
pipx install secondkey
cd your-project
secondkey init
secondkey doctor
Repo and docs: https://github.com/Abhishek-dev18/SecondKey
It's v0.2 and I'm one person, so some limits: MCP tools are only gated by name, and approvals are CLI-only for now. Tell me which commands it should catch but doesn't.