Malicious packages
| Class | Software supply chain |
|---|---|
| Forms | Typosquatting, slopsquatting, dependency confusion, re-registered names |
| CWE | CWE-506: Embedded Malicious Code |
| OWASP Top 10 | A03:2025 Software Supply Chain Failures |
| MITRE ATT&CK | T1195.001 Compromise Software Dependencies and Development Tools |
| Typical severity | Critical |
| Affects | Developer machines and CI servers that install packages from public registries such as npm and PyPI, and the applications built on them |
A malicious package is a package published to a public registry to harm the people and systems that install or use it. Its harmful code is there by design,1 and pip and npm packages are one of the ways attackers deliver such code.2 The OpenSSF Malicious Packages list draws the line at a package whose installation or use would call for incident response because of lost confidentiality, integrity or availability, or one that sends out an identifier that enables a follow-on attack.3
Description
A vulnerable dependency contains a mistake, which is usually fixed in a later version. Malware is written to do harm, so GitHub's malware advisories carry no fixed version, and the only safe action is to stop using the package.4
The attacker has to get the package name into a project. The common routes are:
- typosquatting: a name close to that of a popular package, such as
crossenvforcross-env.5 - Slopsquatting: a name that an AI coding assistant invented when suggesting a package. Attackers watch for such names and publish packages under them, for example
node-fetch-promisein place of the realnode-fetch.6 - Dependency confusion: a public package with the same name as a company's private package and a higher version number, which the installer may prefer.6 Most of GitHub's malware advisories describe this kind of substitution.4
- Re-registered names: a package removed by its owner whose name is taken over by someone else.2
A legitimate package that starts shipping a harmful version after its maintainer account is taken over is covered in Software supply chain compromise.
Mechanism
npm lets every package declare lifecycle scripts in its package.json. During npm install and npm ci, npm runs each dependency's preinstall, install and postinstall scripts after the files are placed in node_modules, and it does the same for global installs with npm install -g.7 A package needs only one line to run a file of its choosing:
{
"name": "example-pkg",
"version": "1.0.0",
"scripts": {
"postinstall": "node setup.js"
}
}The script can run any command when the package is installed.6 Any Node.js code can read environment variables through process.env, which makes the secrets held there the usual target.6
Impact
On a developer machine or a CI server, environment variables commonly hold the passwords, tokens and keys that open applications, cloud services and APIs.8 A package that sends them out hands those credentials to the attacker. The eslint-scope incident collected npm tokens.6 In 2025 the Shai-Hulud worm used a post-install script to copy sensitive data to public GitHub repositories, then used any npm token it found to publish infected versions of the victim's own packages. It reached more than 500 package versions before npm stopped it.9
Notable incidents
On 19 July 2017 an npm account named hacktask published about 40 packages with names close to popular ones, including crossenv, mongose and jquery.js. On 1 August a user reported on Twitter that crossenv sent environment variables from the machine it was installed on to npm.hacktask.net, and npm removed all of the account's packages.5 The package's configuration ran a file named package-setup.js that turned the environment variables into a string and sent it in a POST request.8 crossenv was downloaded 679 times between 19 and 31 July, most of them by registry mirrors; npm estimated at most 50 real installations and told anyone affected to revoke and replace every credential in their shell environment. npm also wrote that deciding at publication time whether a package is malicious is "equivalent to the halting problem".5
Detection
The GitHub Advisory Database imports malware advisories from the npm security team and the OpenSSF Malicious Packages repository. With malware alerts turned on, Dependabot raises an alert when a dependency matches one, and a search for type:malware lists them.4 The OpenSSF reports are published in the Open Source Vulnerability (OSV) format.3 Software composition analysis tools keep track of every component automatically.9
By hand, check package.json for dependencies given as git URLs, tarball URLs or file: paths, which bypass the registry. A typosquatted name is much harder to see this way.6
Prevention
npm
Setting ignore-scripts stops npm from running the scripts in package.json files; npm start, npm test and npm run still run the script they name.10 It is the safest setting, though it only postpones scripts and does nothing when the package's own code is hostile.6 Packages that need a build step can be listed in the allowScripts field of package.json, which npm matches against the package's resolved identity rather than the name it reports.10 The min-release-age setting makes npm install only versions published more than the given number of days ago.10
# .npmrc in the project root
ignore-scripts=true
min-release-age=7In CI, npm ci installs exactly what the lockfile records and stops on any mismatch.6 For private packages, own the matching scope on the public registry, so that nobody else can publish under the same name.10
pnpm
pnpm does not run dependency build scripts unless the package is listed in allowBuilds, and with strictDepBuilds, on by default, an install fails when an unlisted package has one. Its minimumReleaseAge setting, counted in minutes, defaults to 1440, one day; 10080 waits a week. The pnpm documentation reasons that malware is usually found quickly, so a delay of 24 hours most likely avoids a bad version. blockExoticSubdeps, also on by default, refuses indirect dependencies from git repositories or tarball URLs.11
After an installation
Remove the package, since no safe version exists, and replace every key and token that was present in the environment where it ran. The steps are in Responding to a malicious package installation.
Prompt for a coding agent
Check this project for malicious or risky npm dependencies. List every dependency in package.json and the lockfile whose name is one or two characters away from a well-known package, every dependency installed from a git URL, tarball URL or file: path, and every package that has a preinstall, install or postinstall script. Do not install or run anything. Then add ignore-scripts=true and min-release-age=7 to .npmrc, make CI use npm ci, and list the packages that need their install scripts allowed so I can review them.
In SecFlash
SecFlash reports a dependency version published to attack the people who install it as SF-SEC-713 on the Security page, and as SF-THR-201 on the Threats page.
See also
- Software supply chain compromise
- Vulnerable dependencies
- Software composition analysis
- Software bill of materials
- Responding to a malicious package installation
- Secret exposure in CI/CD workflows
- Category: Software supply chain
References
- back MITRE (30 April 2026). "CWE-506: Embedded Malicious Code". Common Weakness Enumeration.
- a b MITRE (3 November 2026). "Supply Chain Compromise: Compromise Software Dependencies and Development Tools, T1195.001". MITRE ATT&CK.
- a b "OpenSSF Malicious Packages". Open Source Security Foundation, GitHub.
- a b c "About the GitHub Advisory Database". GitHub Docs.
- a b c Silverio, C J (2 August 2017). "crossenv malware on the npm registry". npm Blog (archive).
- a b c d e f g h "NPM Security Cheat Sheet". OWASP Cheat Sheet Series.
- back "scripts". npm Docs, CLI v11. GitHub.
- a b Claburn, Thomas (3 August 2017). "This typosquatting attack on npm went undetected for 2 weeks". The Register.
- a b "A03:2025 Software Supply Chain Failures". OWASP Top 10:2025. OWASP Foundation.
- a b c d "config". npm Docs, CLI v11. GitHub.
- back "Mitigating supply chain attacks". pnpm documentation.
External links
- OpenSSF Malicious Packages, GitHub
- Malware advisories, GitHub Advisory Database