Malicious npm Packages: How to Detect and Block Them
How malicious npm and PyPI packages get in (typosquats, hijacked maintainers, slopsquatting) and the settings, checks and scans that block them.
Malicious npm packages are packages published to steal credentials or plant backdoors, and most run the moment they are installed. You block them by installing only from a lockfile, turning off install scripts, waiting a few days before adopting new versions, and scanning every lockfile against malicious-package feeds continuously.
This guide covers the five ways they get in, including slopsquatting by AI coding agents, how to spot one, the exact settings for npm, pnpm and pip, and what to do if you installed one. It is written by Strac, a Y Combinator-backed data security company trusted by enterprises, which scans dependencies, secrets and cloud in one platform.
A malicious package rarely looks like malware. It is a normal package with one extra file and an install script in package.json. These are the routes in:

Typosquats are the oldest and most common: thousands of near-miss names are published every year. Hijacked maintainers are the most damaging, because the malicious version arrives under a name you already trust. Dependency confusion, described by researcher Alex Birsan in 2021, abuses build systems that check the public registry for private package names. Slopsquatting is the newest and is covered below. Rogue handovers happen when a tired maintainer gives a popular package to a stranger who later ships malware in an update.
Each of these routes has a real incident behind it. Every row links to the primary source.
| When | Incident | Route | What happened |
|---|---|---|---|
| Nov 2018 | event-stream | Rogue handover | A new maintainer added a dependency that targeted one cryptocurrency wallet app |
| Oct 2021 | ua-parser-js | Hijacked maintainer | Compromised versions of a widely used package installed a crypto miner and password stealer |
| Feb 2021 | Dependency confusion research | Dependency confusion | Public packages named after internal ones were installed inside dozens of large companies |
| Sep 2025 | chalk, debug and others | Hijacked maintainer | A phishing email posing as npm support led to malicious versions of packages with about 2 billion weekly downloads |
The 2025 incident is the one to remember. The packages were legitimate, the maintainer was experienced, and the phishing domain looked like npm's. Most malicious versions were pulled within hours, which is exactly why a short cooling-off period works so well. For the wider set of attacks, including the Shai-Hulud worm and CI compromises, see our guide to software supply chain security.
The two get lumped together, but they need different responses. A vulnerable package is a legitimate one with a mistake in it: you upgrade on a deadline set by severity, and you can often wait for the next release cycle. A malicious package is hostile on purpose: there is no safe version of a typosquat, and a hijacked release has to come out now, followed by rotating whatever the install script could reach.
Vulnerability advisories also arrive slowly by comparison, often weeks after a flaw is found. Malware reports arrive within hours of publication, and the package is usually pulled from the registry soon after. That timing is why malware detection has to be continuous and matched against what you already installed, not only what you are about to install.
Slopsquatting needs no typo. AI coding assistants sometimes suggest packages that do not exist, and attackers register those names:

A USENIX Security 2025 study generated 576,000 code samples with 16 models and found 205,474 unique hallucinated package names. On average, at least 5.2% of packages suggested by commercial models and 21.7% by open-source models did not exist. Many invented names came back again and again across runs, which is what makes registering them worthwhile for an attacker.
Coding agents raise the stakes because they run npm install themselves. Give agents the same guardrails as CI: a lockfile they cannot silently rewrite, install scripts off, a cooling-off period that rejects packages published yesterday, and a pull request check that flags any new dependency for review. Our guide to AI coding agent security covers the rest of the agent's permissions.
Before adding an unfamiliar package, spend a minute on these checks:
# how old is the package, and how old is the version you are about to install?
npm view some-package time --json | head -20
# who maintains it, and does it point at a real repository?
npm view some-package maintainers repository.url
# does it declare install-time scripts?
npm view some-package scripts
# verify registry signatures and provenance for what is already installed
npm audit signaturesRed flags: a name one character away from a popular package, a first publish in the last few days, a new maintainer on an old package, a postinstall script in a package that has no reason to build anything, obfuscated code, and a repository link that is missing or points at someone else's project. Packages with provenance show which repository and CI workflow built them, which makes a hijacked release built on an attacker's laptop stand out. None of this scales by hand across hundreds of transitive dependencies, which is why automated scanning matters.
A package in your tree is reported malicious tonight. Would you know by morning?
Book a demo of Strac dependency scanning · Run a free security scan
Six settings close most of the doors. None of them needs a new tool:

For npm, put the defaults in the repository's .npmrc so they apply to every developer and every CI run:
# .npmrc
ignore-scripts=true # no install scripts; allow-list exceptions in CI
min-release-age=7 # only install versions published 7+ days ago
@yourco:registry=https://npm.yourco.example/ # private scope always resolves privatelypnpm sets the cooling-off period in minutes and does not run dependency build scripts unless you allow them:
# pnpm-workspace.yaml
minimumReleaseAge: 10080 # 7 days
minimumReleaseAgeExclude:
- '@yourco/*' # your own packages update immediatelyThe npm min-release-age and pnpm minimumReleaseAge settings apply to transitive dependencies too. Pick a window your team can live with; even three days removes most hijacked releases, and you can always exempt an urgent security fix.
A committed lockfile stops a new malicious version from sneaking into a build through a loose version range, and you should always have one. It does nothing when someone adds a typosquat on purpose, when a bot or an agent regenerates the lockfile and picks up a hijacked release published an hour ago, or when a package that was clean last month is reported malicious today. The lockfile tells you exactly what you have. Something still has to check whether what you have is hostile, and keep checking after the install.
Dependency confusion works when a build asks the public registry for a name that should only exist privately. Publish internal packages under a scope such as @yourco, register that scope on the public npm registry so nobody else can, and map the scope to your private registry in .npmrc as above. Never mix unscoped internal names with a fallback to the public registry. The same idea applies to Python: point pip at one index, and use --index-url rather than --extra-index-url for private packages, because pip treats extra indexes as equals and picks the highest version from any of them.
Malicious packages are not an npm-only story. PyPI sees the same typosquats, hijacked accounts and hallucinated names, and a source distribution can run code from setup.py during install. The equivalent controls are to pin with hashes, prefer wheels, and review new dependencies:
# install only what the lockfile says, verified by hash
pip install --require-hashes -r requirements.txt
# refuse source distributions, so no setup.py runs at install time
pip install --only-binary :all: -r requirements.txtThe pip secure installs guide covers hash-checking mode in detail. Malicious packages reported on either registry are collected by the OpenSSF malicious packages project and published to OSV, which is what good scanners match against.
The cheapest control is a person looking at each new dependency once. Write it down in a few lines so it actually happens: any pull request that adds a direct dependency gets a second reviewer; the reviewer checks the name, age, maintainers, install scripts and repository link from the list above; packages that need install scripts are added to the allow-list explicitly; and lockfile changes are never auto-merged without a scan. Dependency update bots stay on, with a cooling-off period.
This takes minutes per week and gives auditors something concrete. SOC 2 reviewers increasingly ask how third-party code is approved, and a short written rule plus scan results is a better answer than "we use popular packages". Put deadlines for removing reported packages in your vulnerability management policy.
Treat the machine or CI runner that installed it as compromised, because the install script ran with its access. In order:
npm ls bad-package or pnpm why bad-package shows every path to it; git log -p package-lock.json shows when it arrived.Strac reads every lockfile in your GitHub and GitLab repos, monorepos included, and builds an inventory of every direct and transitive package. It checks that inventory against vulnerability and malware databases, flags look-alike package names and end-of-life software, and when a new malware report lands it is matched against your inventory without rescanning, so you know in seconds whether you are affected:

Fixes arrive as a one-click pull request with the smallest safe upgrade, including for transitive dependencies. Code is scanned in a temporary environment that is deleted afterward, and every finding and fix is saved as SOC 2, ISO 27001 and HIPAA evidence. Because the same platform runs secret scanning, Strac also finds the tokens a malicious package would have gone looking for. For how this fits together, see our guides to software composition analysis, SBOMs and application security posture management. Strac's free scan checks up to 10 GitHub repos for malicious and vulnerable packages and leaked secrets: run it here, or see how Strac's dependency scanning works.

Know within seconds when a package you depend on is reported malicious.
Book a demo of Strac dependency scanning · Run a free security scan
A package published to the npm registry on purpose to harm whoever installs it, usually by stealing credentials, tokens or crypto wallets, or by installing a backdoor. Most run their payload from an install script, so simply installing the package is enough to trigger it.
Slopsquatting is registering package names that AI coding assistants invent. Models sometimes suggest packages that do not exist; if an attacker publishes a malicious package under that name, the next developer or agent who follows the suggestion installs it.
Check whether the name is a near-miss of a popular package, how old the package and the latest version are, who the maintainers are, whether it declares install scripts, and whether it links to a real source repository. Then scan your whole dependency tree against malicious-package databases, because a transitive dependency is as dangerous as a direct one.
Only after it has been reported. npm audit checks your tree against known advisories, so it catches malware that has already been identified and published as an advisory. It does not inspect what a package does, and it will not flag a brand-new malicious release in the hours before anyone reports it.
Set ignore-scripts=true in your project's .npmrc, or run npm ci --ignore-scripts, and allow-list the few packages that genuinely need a build step. pnpm goes further and does not run dependency build scripts unless you explicitly allow them.
.avif)
.avif)
.avif)
.avif)
.avif)


.gif)

