Compromised npm Package: What to Do Next
In this article
If you suspect you've installed a compromised npm package, immediately disconnect the affected machine from the network, then rotate all credentials and secrets that could have been exposed. This article walks you through the exact steps to contain the damage, assess the risk, and prevent future occurrences.
Understanding the Threat
npm (Node Package Manager) is the default package manager for Node.js and JavaScript, hosting over 2 million packages. A "compromised" package means that its source code has been maliciously altered, often to steal credentials, inject cryptocurrency miners, or deploy backdoors. This can happen when an attacker gains access to a maintainer's account, or when a package name is typosquatted (a lookalike name like "lodahs" instead of "lodash").
The supply chain attack vector is particularly dangerous because a single compromised dependency can affect every project that uses it, directly or transitively. For example, the event-stream incident in 2018 saw a malicious version of a popular package steal Bitcoin wallets from users who had installed it as a dependency.
Immediate Containment Steps
- Disconnect the machine: Unplug the network cable or turn off Wi-Fi to stop any ongoing data exfiltration.
- Preserve evidence: Take a snapshot of the project's
package-lock.jsonandnode_modulesdirectory (oryarn.lockif using Yarn). This helps identify the exact malicious version. - Rotate all secrets: Assume anything on the machine is compromised. Change passwords, API keys, and tokens for any service you use, especially those accessed from that machine.
- Revoke npm tokens: If you publish packages, revoke any npm automation tokens that might have been stolen.
Assessing the Damage
Once the immediate threat is contained, you need to figure out what the malicious code actually did. Look at the package's source code in node_modules and search for suspicious patterns like:
- Network calls to unknown domains (look for
http,https,ws,child_process). - Base64-encoded strings or obfuscated code.
- References to environment variables like
process.envthat are sent to remote servers. - Files that are written to unexpected locations (e.g.,
~/.ssh,/tmp).
Check the npm advisory database (npmjs.com/advisories) for the package name and version. If it's a known vulnerability, the advisory will tell you the affected versions and the fix. Also, review your own code for any data that might have been sent out—the malicious code often runs during install or when the package is required.
Removing the Package and Cleaning Up
After identifying the compromised package, remove it from your project:
- Uninstall: Run
npm uninstall <package-name>. This removes it fromnode_modulesand yourpackage.json. - Audit your lock file: Run
npm auditto check for other known vulnerabilities in your dependency tree. - Clean cache: Run
npm cache clean --forceto remove any cached copies of the malicious package. - Verify integrity: If you have a CI/CD pipeline, ensure it fails on any dependency with a known vulnerability (use
npm audit --audit-level=high).
If the malicious package was a transitive dependency (a dependency of one of your dependencies), you may need to update the parent package or use overrides in your package.json to force a safe version.
Preventing Future Incidents
Prevention is better than reaction. Here are practical steps to reduce the risk of installing a compromised package again:
- Pin exact versions: Use exact versions in
package.json(e.g.,"lodash": "4.17.21"instead of"^4.17.21") to avoid auto-updating to a malicious version. - Use a lock file: Always commit
package-lock.jsonoryarn.lockso that all installs use the exact same versions. - Run
npm auditregularly: Integrate it into your CI pipeline to catch known vulnerabilities early. - Check package popularity: Be wary of packages with few downloads but a large number of dependencies, or packages that have recently changed ownership.
- Use a private registry: Tools like Verdaccio or npm's private packages can proxy and cache only approved packages.
- Enable 2FA for npm accounts: If you publish packages, require two-factor authentication to prevent account takeovers.
Comparing npm Security Tools
Several tools can help you avoid compromised packages. Here's a quick comparison:
- npm audit: Built into npm, checks against the npm advisory database. Free and easy to use, but only catches known vulnerabilities.
- Snyk: A third-party tool that also scans for vulnerabilities and can automatically fix them. Offers a free tier for open-source projects.
- Socket: Scans for supply chain attacks by analyzing package behavior (e.g., network access, install scripts). Free for open-source.
- GitHub Dependabot: Automatically opens pull requests to update vulnerable dependencies. Free for GitHub repositories.
Each tool has its strengths; using a combination (e.g., npm audit + Socket) gives you broader coverage.
Common Misconceptions
"I only install popular packages, so I'm safe." Popular packages are prime targets because they have a large install base. The event-stream attack compromised a package with millions of weekly downloads.
"I can just delete the node_modules folder and reinstall." If you reinstall without updating the lock file, you'll get the same compromised version again. Always update the package version or lock file first.
"The malicious code only runs during installation, so I'm fine if it's already installed." Many malicious packages execute code when they are required (imported) in your application, not just during install. That means the risk persists until you remove it.
Frequently Asked Questions
How do I know if a package is compromised?
Look for unusual behavior like unexpected network requests, excessive CPU usage, or new files appearing. Use npm audit to check for known vulnerabilities, and consider using a tool like Socket that flags suspicious package behavior.
Should I report the compromised package to npm?
Yes. You can report a security issue on the npm website by clicking "Report a security vulnerability" on the package page. This helps protect other users.
What if the compromised package is a transitive dependency?
You can't directly uninstall it, but you can update the parent package to a safe version, or use npm's overrides feature to force a specific version of the transitive dependency.
Can I get malware from npm packages?
Yes. Malicious code can be hidden in the package's install script (preinstall, postinstall) or in the main module. Always review what a package does before installing it, especially if it's not well-known.
Is it safe to use npm install without a lock file?
No. Without a lock file, npm will install the latest version that satisfies your package.json range, which could be a compromised update. Always commit your lock file.
What should I do if I've already pushed code with the compromised package to production?
Immediately revert the deployment to a known-good state, rotate all secrets, and run a full security audit on your application logs for any signs of data exfiltration. Then follow the removal steps above.