Is the npm Package Your AI Agent Added Safe? Hallucinated and Malicious Dependencies
AI coding assistants sometimes import packages that do not exist. Attackers know which names models tend to invent, register them, and wait for someone to run npm install. A package can run code on your machine the moment it is installed, before you import anything. Check every dependency you did not choose yourself: does it exist, how old is it, who publishes it, and does it run install scripts.
On this page
This attack has a name, slopsquatting. It is a cousin of typosquatting, where the bait is a misspelling (expres, reqeusts). Here the bait is a plausible name a model made up, such as react-stripe-checkout-utils or supabase-auth-helpers-next. Models repeat the same invented names often enough that registering them pays off.
The second route needs no hallucination. A real, popular package gets a malicious release after its maintainer's account is taken over. An agent that runs npm install some-package@latest an hour later pulls it in.
What a bad package can do
npm packages can define preinstall and postinstall scripts that run automatically with your user's permissions. On a developer laptop or in CI that means access to:
.envfiles and environment variables, including cloud and deploy credentials.~/.ssh,~/.npmrc,~/.aws, browser profiles, crypto wallets.- Your source code and git credentials.
When an agent adds the dependency and runs the install in the same step, nobody looked at the name at all.
The two-minute check
Before installing something an assistant suggested:
npm view <package> name version time.created time.modified maintainers repository.url
Then answer five questions:
- Does it exist? A 404 means the model invented it. Do not go looking for "the closest match". Ask what the code needed and find the real library for that.
- How old is it? A package created last month that claims to be a standard helper for a five-year-old framework is suspicious.
- Who uses it? Look at the weekly downloads on npmjs.com. Low numbers are not proof of anything, but a "popular" helper with 40 downloads is not what it claims.
- Does the repository match? The linked GitHub repo should exist, contain this package, and have real history. Attackers copy a README from a legitimate project.
- Is the name exactly right? Compare character by character with the official docs of the service it integrates with. Check the scope too:
@supabase/ssris published by Supabase, whilesupabase-ssrcould be anyone.
Check for install scripts:
npm view <package> scripts
A postinstall in a small utility library needs an explanation.
Safer defaults
Review dependency changes like code. Every pull request that touches package.json gets a human look at the added names. Add to your agent rules file: "Do not add dependencies without listing them and the reason. Prefer packages already in the project."
Turn off install scripts by default.
npm config set ignore-scripts true
A few packages need their scripts (native modules such as sharp, esbuild, prisma). Run those explicitly with npm rebuild <package>. pnpm goes further: since version 10 it does not run dependency lifecycle scripts unless you allow the package by name.
Do not install brand-new releases. Most malicious versions are found and removed within hours or days. Recent pnpm versions have a minimumReleaseAge setting that refuses versions younger than a set number of minutes:
# pnpm-workspace.yaml
minimumReleaseAge: 4320 # three days
Renovate and Dependabot have similar cooldown options for update pull requests.
Commit the lockfile and use npm ci in CI. npm ci installs exactly what the lockfile says and fails if package.json disagrees. That gives you a diff to review whenever versions change.
Keep secrets away from installs. CI install steps should not have deploy credentials in the environment. On your own machine, let agents work in a dev container or a VM, where a bad install can reach the project and not your SSH keys.
Scan. npm audit reports known vulnerabilities in what you already have. Tools like Socket look for malicious behaviour in new packages, which is the actual threat here.
If you already installed something suspicious
- Disconnect the machine or stop the CI runner.
- Assume every secret reachable from that environment was read:
.envvalues, npm and GitHub tokens, cloud keys, SSH keys. Rotate them from a different, clean machine. Our post on keys committed to GitHub has the rotation order. - Remove the package, delete
node_modulesand the lockfile entry, and reinstall with scripts disabled. - Check your npm and GitHub accounts for tokens, deploy keys or published versions you did not create.
Other ecosystems
The same applies to PyPI (pip install runs setup.py), RubyGems, crates and Docker images. Model-invented package names have been documented for Python as well as for npm.
Dependency checks are part of how we verify a coding agent's security work. If you want your dependency tree and install pipeline reviewed along with the rest of the app, see our app rescue and security audit.
