Keep AI coding agents from installing malicious npm packages
How npm supply chain attacks like Shai-Hulud reach coding agents, and how to stop them: no install scripts, a 7-day release age, lockfiles, a company registry, and Claude Code rules that ask before any install.
Updated
To keep coding agents from installing malicious npm packages, turn off install scripts (ignore-scripts=true), only install versions that are at least a few days old (min-release-age=7 in npm 11.10+), install from the lockfile with npm ci, and make the agent ask before any npm install or npx. Most malicious versions are caught and pulled within hours, and most run their payload from an install script.
As of November 2026, from the npm, pnpm, Yarn and Bun docs. Part of Keep coding agents safe.
Why agents make it worse
An agent installs packages the moment a task seems to need one, often with a name it guessed, and runs whatever the install runs. And the attacks now target agents directly:
- Nx “s1ngularity”, August 2025. Malicious Nx versions ran a script that called the victim’s own AI agents,
claude --dangerously-skip-permissions,gemini --yoloandq chat --trust-all-tools, to hunt for secret files and leak them (Snyk, Nx advisory). - Shai-Hulud, the npm worm, September and November 2025. It stole npm, GitHub and cloud tokens and republished itself through every package the victim maintained: over 500 packages in the first wave (CISA), about 700 in the second, which ran in
preinstall(Wiz). - ChainDrop, August 2026. A worm across 400+ packages that wrote
.claude/settings.jsonand.vscode/tasks.jsoninto projects, so later Claude Code or VS Code use started it again (Microsoft). - Hallucinated names.
react-codeshiftdidn’t exist until a researcher registered it in January 2026, after AI-written skills told agents to runnpx react-codeshift. Agents kept downloading it daily (Aikido).
Fix 1: no install scripts
Shai-Hulud, Nx and ChainDrop all ran from an install script. Turn them off:
# .npmrc
ignore-scripts=true
What your package manager already does:
| Tool | Install scripts |
|---|---|
| npm 12 | Blocked unless allowScripts in package.json lists them. Approve with npm approve-scripts |
| npm 11 and older | Run by default. Set ignore-scripts=true |
| pnpm 10+ | Not run unless allowed (allowBuilds in pnpm 11) |
| Yarn 4.14+ | Not run by default (enableScripts: false) |
| Bun | Run only for an allow list of popular packages, or your trustedDependencies |
Fix 2: wait a week for new versions
A release-age setting skips versions published in the last few days, which is when most malicious versions are found and pulled. Seven days is a common choice:
| Tool | Setting |
|---|---|
| npm 11.10+ | min-release-age=7 in .npmrc, in days |
| pnpm 10.16+ | minimumReleaseAge: 10080 in pnpm-workspace.yaml, in minutes. pnpm 11 defaults to 1 day |
| Yarn 4.10+ | npmMinimalAgeGate: 10080 in .yarnrc.yml, in minutes. Yarn 4.15 defaults to 1 day |
| Bun 1.3+ | minimumReleaseAge = 604800 under [install] in bunfig.toml, in seconds |
When you need an urgent security fix, exempt one package: min-release-age-exclude in npm, minimumReleaseAgeExclude in pnpm. On pnpm, also set trustPolicy: no-downgrade: it fails when a package suddenly loses its provenance, a common sign of a stolen token.
Fix 3: install from the lockfile
Commit package-lock.json (or pnpm-lock.yaml, yarn.lock, bun.lock) and install with npm ci, which installs exactly what’s locked and fails if package.json disagrees. New dependencies then show up as a lockfile change in a pull request, where a person reviews them. npm audit signatures checks that what you installed was signed by the registry and, where present, its provenance.
Fix 4: make the agent ask before installing
In Claude Code, add ask rules so every new package waits for you:
{
"permissions": {
"ask": [
"Bash(npm install *)",
"Bash(npm i *)",
"Bash(pnpm add *)",
"Bash(yarn add *)",
"Bash(bun add *)",
"Bash(npx *)",
"Edit(./package.json)"
],
"allow": ["Bash(npm ci)"]
}
}
When it asks, check the name before you say yes: does it exist, is it the one you meant, how old is it, who publishes it? A name the agent made up is the easiest attack there is. Rules match the command’s text, so they’re a checkpoint, not a wall; a sandbox and no secrets in the agent’s environment do the rest. More in Stop Claude Code from deleting files or touching production.
Also review changes to .claude/ and .vscode/ in pull requests, since ChainDrop hid there. A CODEOWNERS line for those folders makes the review required.
Fix 5: a company registry or a scanning proxy
Point every install at one place you control with a line in .npmrc:
registry=https://npm.your-company.com/
That covers dependencies of dependencies and npx too. Artifactory, Verdaccio and npm Enterprise all work this way. Free scanning wrappers exist too: Socket Firewall (sfw npm install ...) blocks confirmed malware, and Aikido Safe Chain waits 48 hours on new packages by default.
Fix 6: keep tokens out of reach
The worms spread by stealing tokens from the machine they ran on. Keep npm publish tokens, GitHub tokens and cloud keys out of the agent’s environment and out of ~/.npmrc where you can. Publish from CI with npm’s trusted publishing (OIDC) instead of a token on your laptop.
Safe default to paste
The coral-safety-kit has a packages/ folder with .npmrc, pnpm-workspace.yaml, .yarnrc.yml and bunfig.toml (no scripts, 7-day release age), plus claude/package-rules.json with the ask rules above.
Where Coral fits
Ask rules mean the agent stops at every install and waits for you. Coral shows that request in your Mac’s notch with the full command, so you can check the package name and answer with one key. Free on your Mac; answering from your iPhone straight to the agent is Pro.
Coming in Coral Terminal: package rules per agent, so agents install only from your approved registry and ask before any new dependency.
Questions
What is Shai-Hulud?
An npm worm, named after the sandworms in Dune. First seen in September 2025, it stole developers’ npm, GitHub and cloud tokens from install scripts, then published infected versions of every package the victim maintained. A second wave in November 2025 hit about 700 packages.
How do I protect against npm supply chain attacks?
Turn off install scripts, wait about 7 days before installing new versions (min-release-age), install from a lockfile with npm ci, review new dependencies in pull requests, and keep tokens off developer machines.
Does npm have a minimum release age?
Yes, since npm 11.10: min-release-age=7 in .npmrc installs only versions published more than 7 days ago. pnpm, Yarn and Bun have their own settings.
Can AI agents install malicious packages?
Yes. Agents install packages without asking unless you set rules, sometimes under names they guessed, and an install script runs with your permissions. Make installs ask, and turn scripts off.
Does ignore-scripts break packages?
Some packages need a build step, like esbuild or sharp. Allow just those: allowScripts in npm 12, allowBuilds in pnpm 11, trustedDependencies in Bun.