r/ClaudeCode • • 3h ago

Help/Question How are you stopping Claude Code from installing package versions with known CVEs?

Asked Claude code to add JWT auth to a side project's Express API and this is what it ran

npm install jsonwebtoken@8.5.1

It went straight for an old version with known CVEs, probably whatever showed up most in its training data. /security-review on the branch came bac clean , which I guess makes sense since it looks at the code it wrote and not what version got installed.

And going back through older sessions and it'd done this a bunch, 9 that I counted , all stuff npm audit flags. I HAVE PostToolUse hook that was supposed to catch this and it never fired once, no idea why.

What are you using to catch these before they get merged?

0 Upvotes

6 comments sorted by

•

u/AutoModerator 3h 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.

1

u/BoundfoxStudios 3h ago

Put in your Claude.md that it must research the latest version before installing.
Add a CI that scans for CVEs on a schedule. They might still happen afterwards, so you can check regularly.

1

u/Effective-Curve-5754 3h ago

Good shout. Only issue is mine has ignored instructions I know were sitting right there in Claude.md mid-session. It reads your rules then decides it knows better anyway. CI scan is the real safety net, trust the audit not the model.

1

u/Scared-Letterhead949 3h ago

I always have it run npm audit when I'm ready to open a PR

1

u/VridhiMehta 3h ago

First thing I'd check on the hook that never fired: the matcher. PostToolUse matchers match against the tool name, not the command text, so the matcher has to be "Bash" (or a regex that covers it). A matcher written for "npm" or "npm install" never fires no matter how many installs run. If yours already matches on the tool name, the cause is somewhere else, but this is the cheap check.

Two fixes on top of that. Make it a PreToolUse hook instead: on a Bash call containing npm install, check the requested version against the current one and exit 2 with the findings when it's stale. Exit code 2 blocks the call and shows the output to Claude, so it corrects itself before anything lands. And keep npm audit in CI on the lockfile as the backstop, so a version that slips past every local check still fails the PR. /security-review reviews the code it wrote, as you found; which version got installed is a supply-chain question and belongs in CI.

1

u/shetritr 2h ago

security-review came back clean because the problem lives in the lockfile, not in the code it wrote. a Stop hook that runs npm audit --audit-level=high and blocks the turn on a hit would have caught all 9, no matcher to get wrong