Calendar Icon White
December 5, 2026
Clock Icon
9
 min read

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: How to Detect and Block Them
ChatGPT
Perplexity
Grok
Google AI
Claude
Summarize and analyze this article with:

TL;DR

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.

✨ Malicious npm packages: how they get in

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:

Five ways malicious npm packages get in: typosquats, hijacked maintainers, dependency confusion, slopsquatting and rogue handovers, and what they steal

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.

Malicious npm packages in the wild

Each of these routes has a real incident behind it. Every row links to the primary source.

WhenIncidentRouteWhat happened
Nov 2018event-streamRogue handoverA new maintainer added a dependency that targeted one cryptocurrency wallet app
Oct 2021ua-parser-jsHijacked maintainerCompromised versions of a widely used package installed a crypto miner and password stealer
Feb 2021Dependency confusion researchDependency confusionPublic packages named after internal ones were installed inside dozens of large companies
Sep 2025chalk, debug and othersHijacked maintainerA 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.

Malicious packages vs. vulnerable packages

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: the AI coding agent version of typosquatting

Slopsquatting needs no typo. AI coding assistants sometimes suggest packages that do not exist, and attackers register those names:

How slopsquatting works: a model invents a package name, an attacker registers it, an agent installs it, the install script runs

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.

npm malware detection: signals that give a package away

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 signatures

Red 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

✨ Block malicious packages before they run

Six settings close most of the doors. None of them needs a new tool:

Six settings that block most malicious packages: lockfile installs, no install scripts, cooling-off period, own your scope, verify signatures, scan continuously

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 privately

pnpm 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 immediately

The 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.

Why a lockfile alone is not enough

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.

Stop dependency confusion with scopes

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.

PyPI has the same problem

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.txt

The 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.

A lightweight policy for new dependencies

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.

You installed a malicious package. Now what?

Treat the machine or CI runner that installed it as compromised, because the install script ran with its access. In order:

  1. Find where it is. npm ls bad-package or pnpm why bad-package shows every path to it; git log -p package-lock.json shows when it arrived.
  2. Rotate every secret that machine could read. Cloud keys, npm and GitHub tokens, SSH keys, and every secret in the CI pipeline. Our guide to what to do in the first hour after an API key leak has the provider-by-provider steps.
  3. Remove and pin. Downgrade to the last clean version or remove the package, regenerate the lockfile, and rebuild CI runners and containers from clean images.
  4. Check for spread. If you publish packages, check whether any were republished with your tokens.
  5. Record it. What arrived, when, what was rotated. Your auditor will ask.

✨ How Strac detects malicious packages

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:

Strac dependency finding showing a critical package, the dependency chain that pulls it in, and an Open fix PR button
The chain that pulls a package in, and the fix in one click.

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.

Strac free scan report: malicious and vulnerable package counts alongside leaked secrets

Know within seconds when a package you depend on is reported malicious.

Book a demo of Strac dependency scanning  ·  Run a free security scan

🌶️ Spicy FAQs about malicious npm packages

What is a malicious npm package?

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.

What is slopsquatting?

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.

How can I tell if an npm package is malicious?

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.

Does npm audit detect malware?

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.

How do I stop npm install scripts from running?

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.

Customer story

“Don’t just alert on problems, fix them.”

Scott Roberts, Chief Information Security Officer, UiPath

  • UiPath
  • Crypto.com
  • Inmar Intelligence
  • thredUP
  • Paymentology
  • Enveda
  • Genesis Molecular AI
  • MeridianLink
More customer stories →
What is a malicious npm package?
What is slopsquatting?
How can I tell if an npm package is malicious?
Does npm audit detect malware?
How do I stop npm install scripts from running?
Discover & Protect Data on SaaS, AI, MCP, Endpoints & Cloud
Strac provides end-to-end data loss prevention for all SaaS and Cloud apps. Integrate in under 10 minutes and experience the benefits of live DLP scanning, live redaction, and a fortified SaaS environment.
Get compliant from $4,995

Latest articles

Browse all

Get Your Datasheet

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Close Icon