Playbooks · Coding agents
Security-review your own code with Claude Code
Written October 26, 2026 · Edited November 11, 2026
For a solo developer × reviewing their own recent changes for security holes × Claude Code, ending with a fix and a regression test per real finding — and a hook that refuses to let the session end while a finding is still open.
Before you start
You need a branch with a small, reviewable diff — a feature or a fix, not a month of work — and a test runner you can actually run. Work on a branch, not on main, so a bad repair is cheap to throw away. An agent review is a second opinion, not a merge gate on its own: it can miss context you know and it can be confidently wrong about something subtle, so a human still reads the final diff before it merges.
The one discipline the whole loop rests on: a finding without a reproduction is a hypothesis, not a vulnerability. You do not repair hypotheses. You reproduce them first, and the ones that do not reproduce get dropped, not fixed.
What "done" looks like
At the end of one session you have a short findings list where every surviving item has a reproduction, a fix on the branch, and a regression test that fails without the fix and passes with it — plus a completion hook that re-scans the changed lines and sends the session back to work while anything is still open. Findings that did not reproduce are listed as dropped, with the reason, so the next session does not re-litigate them.
Steps
- Run the review on the diff, not the whole repo. In Claude Code, run the built-in security review command on your pending changes. The command ships with Claude Code and reviews pending changes; the GitHub Action version of the same reviewer is diff-aware for pull requests, analyzing only changed files. A whole-repo scan buries the three real findings in a hundred old ones you are not about to fix today.
- Triage the findings against the reviewer's own filter. The reviewer already excludes low-impact, false-positive-prone classes: denial of service, rate limiting, memory and CPU exhaustion, generic input validation without proven impact, and open redirects. Drop anything in those classes without argument, then drop anything whose explanation does not name the exact code path an attacker would take. What survives should be a short list, each item naming a vulnerability class the reviewer documents — injection, broken authentication or authorization, data exposure, weak cryptography, bad input validation, business-logic flaws, insecure configuration, supply-chain risk, code execution, or cross-site scripting.
- Reproduce each surviving finding before touching code. For each item, write the smallest proof that it is real: the crafted input, request, or call sequence that reaches the vulnerable code and shows the bad outcome. If you cannot produce that proof, the finding is dropped and recorded as not reproduced. This is the step that stops the loop from becoming a fix-everything-the-model-said session, and it is also what makes the regression test in step 5 possible: a reproduction is a test waiting to be written down.
- Repair on the branch, one finding at a time. Fix the reproduced findings one by one, smallest fix first, and keep each fix in its own commit where the change allows it. A repair that touches five findings at once cannot be reverted for one of them, and a repair you cannot revert is a repair you cannot trust.
- Add one regression test per finding. For each repaired finding, add a test that fails on the code before the fix and passes on the code after it. Run it both ways at least once — against the fixed code, and by temporarily reverting the fix — so you know the test actually guards the finding rather than passing regardless. A finding with no regression test will come back the next time someone refactors the same code, and nobody will notice.
- Re-run the review on the fix diff only. Run the review command again, scoped to the changes your repairs made. New findings introduced by the repairs themselves are the most embarrassing kind, and they are also the easiest to catch, because the diff is small and fresh. Anything new goes back through steps 3 to 5; nothing skips the reproduction step because it arrived in a repair.
- Gate completion with a Stop hook that scans only changed lines. Add a hook that fires when Claude Code finishes a response, scans only the lines the session changed (the diff against HEAD plus new files), and sends Claude back to work with its findings while any remain. The hook pattern is: scan changed lines, exit with code 2 and the report on stderr while findings remain, exit 0 when clean. The official hooks reference confirms the mechanics this relies on: exit code 2 on the Stop event sends Claude back to keep working instead of stopping. Put an attempt cap on it — after three blocked completions, stop blocking and write the report to a file for a human instead. A gate with no cap can loop forever on a finding nobody can fix, and a session that cannot end is worse than a session that ends with a report.
- Read the final diff yourself before merging. Go through the branch diff once, by eye, with the findings list beside it. Confirm every fix is one you understand, every regression test is one you believe, and nothing unrelated rode along in a repair commit. Then merge. The loop's output is a reviewed branch, not a verdict: the verdict is yours.
Check the agent's work
- Check that every surviving finding has a reproduction you ran, not just a description of one. A finding whose proof is "the model says it is reachable" is still a hypothesis.
- Check that every regression test fails without its fix. A test that passes on the unfixed code guards nothing.
- Check that the Stop hook scans the diff, not the whole repo. A hook that scans everything will block on old code you never touched, and you will learn to ignore it.
- Check that the hook has an attempt cap and a human fallback. A gate that can loop forever will be deleted the first time it loops, and then you have no gate at all.
- Check that no repair commit contains unrelated changes. An unrelated change inside a security fix is a second change nobody reviewed wearing the first one's justification.
What can go wrong
- Fixing findings that were not reproduced. A fix for a vulnerability that was never proven real changes code for no benefit and can introduce a real bug. The reproduction step exists to stop this, and skipping it is the loop's most common failure.
- A Stop hook that never lets the session end. Without an attempt cap, a finding the agent cannot fix blocks completion indefinitely. Cap it, write the report to a file, and hand it to a human — a report is a result, a loop is not.
- Scanning everything instead of the diff. Old code has old findings. A gate that fires on them trains you to bypass the gate, and then the gate protects nothing. Scope it to changed lines only.
- Reviewing untrusted code with the reviewer. The review action's own security note says it is not hardened against prompt injection and should only review trusted pull requests. Do not point this loop at code from contributors you do not trust without a maintainer's approval step first.
Limits
- This loop reviews code you wrote or changed. It does not audit your dependencies, your infrastructure, or your running service, and a clean loop is not a claim that the system is secure.
- The reviewer's false-positive filter is a documented choice, not a guarantee: classes it excludes can still matter for your system, and classes it reports can still be wrong. The reproduction step is what turns its output into evidence.
Related playbooks
- Claude Code 2.1.325 fixes a zstd security issue
- Copilot code review goes Adaptive, checks secrets
- Decide if Copilot Code Review Max is worth it
- Vera: unlimited AI code review, free at every tier
Sources
Sources: anthropics/claude-code-security-review, Claude Code hooks reference, a published Stop-hook write-up.
Back to: Coding playbooks hub · Also: Review a pull request with a coding agent