Dependency confusion and typosquatting
| Class | Software supply chain |
|---|---|
| Forms | Typosquatting, slopsquatting, dependency confusion |
| OWASP Top 10 | A03:2025 Software Supply Chain Failures |
| MITRE ATT&CK | T1195.001 Compromise Software Dependencies and Development Tools |
| Typical severity | Critical |
| Affects | Projects that install from npm, PyPI or RubyGems, and companies that keep private packages next to public ones |
A package manager installs what a name points to. npm install or pip install asks a registry for a name, and npm lets the package that comes back run its own install script straight away.1 Two related attacks exploit this by publishing a harmful package under the right name. Typosquatting picks a name close to a popular package and waits for someone to mistype or misread it.2 Dependency confusion takes the exact name of a company's private package and publishes it on the public registry, so that the company's own installer fetches the public copy.1 Both are ways of delivering a malicious package; the name does the work that a stolen maintainer account does in a Software supply chain compromise.
Names that look right
A typosquat relies on a typing slip or a visual likeness. The usual target is credentials: any installed package can read the environment variables of the machine, where tokens and keys sit.2 On npm, crossenv imitated cross-env and arrived together with 36 other typosquatted packages;2 Malicious packages gives the details. The same trick on domain names is covered under Look-alike domains.
Registries screen new names. Under rules announced on 26 December 2017, npm removes the characters ., - and _ from a new package name and refuses it if the result equals an existing name, so react-native blocks reactnative and react_native.3 npm states that it detects and blocks typosquats at publish time.4 The punctuation rule alone lets through a name with a swapped or added letter.
Slopsquatting is the variant aimed at AI coding assistants. A model can suggest a package that has never existed, and an attacker who notices which invented names come up registers them. One example is node-fetch-promise, offered in place of the real node-fetch.2
How the public copy wins
Dependency confusion needs no mistake from a person. It works when an installer consults a private index (Python's word for a registry) and a public one for the same name and chooses between them by version number. pip does this with --extra-index-url: it searches every configured index with no order of priority and takes the best match, which in practice means the highest version.5
# Searches the company index AND PyPI for every name
pip install --extra-index-url https://pypi.internal.example.com/simple acme-billingIf the company publishes acme-billing 1.4.0 internally and an attacker uploads acme-billing 9000.0.0 to PyPI, pip installs the attacker's version.1 pip's own documentation warns that this use of the option is unsafe for exactly this reason.5 npm install can make the same choice for a plain internal name, resolving it to a public package with a higher version.2 In 2021 the virtual repositories of JFrog Artifactory, which merge internal and public packages, were reported to choose between them by the same rule.1
The attacker first needs the private names. They leak through package.json files pushed to public repositories, job adverts, error messages and stack traces.2 The richest source in the 2021 research was public JavaScript: build tools had copied internal package.json contents into bundled scripts on sites run by Apple, Yelp and Tesla.1 Client-side code exposure and Source maps in production cover how bundles expose this kind of detail.
Cases
The 2021 research
In mid-2020 the researcher Alex Birsan saw a PayPal package.json on GitHub that mixed public packages with names absent from npm. He registered such names on npm, PyPI and RubyGems with a preinstall script that sent the user name, host name and working directory home inside DNS queries. Every target had a bug bounty programme or had agreed to the test. Shopify's build system installed his gem shopify-cloud a few hours after upload, and code he published to npm in August 2020 ran on several machines inside Apple. By February 2021 he had found the flaw in more than 35 organisations; about three quarters of the callbacks came from npm, and Apple, PayPal and Shopify each paid 30,000 US dollars.1
torchtriton, December 2022
PyTorch's nightly builds depended on a package called torchtriton, served from PyTorch's own index. Someone uploaded a package with that name to PyPI, and because pip preferred the PyPI copy, Linux users who installed PyTorch-nightly with pip between 25 and 30 December 2022 received it. Its binary ran when the triton module was imported, collected system details, DNS resolver settings, the files in ~/.ssh and up to 1,000 small files from the home directory, and sent them out through encrypted DNS queries.6 The PyPI package passed 2,300 downloads in a week.7 PyTorch renamed the dependency pytorch-triton, registered a placeholder package on PyPI so the problem could not repeat, and pulled the affected nightlies; stable releases were never affected.6
Defences
On npm, give every internal package a scope, a prefix such as @acme/billing. Only the owner of a scope can publish into it, and each scope points to exactly one registry, so a line in .npmrc sends every @acme package to the private registry and never to the public one.8 npm itself cannot detect dependency confusion and advises scoped names for this reason.4
# .npmrc
@acme:registry=https://npm.internal.example.com/Registering an organisation of the same name on npmjs.com keeps the scope out of other hands there, because only an organisation's members can add packages to its scope.8 An unscoped internal name can also be reserved on the public registry with an empty placeholder package.2
In Python, replace --extra-index-url with --index-url pointing at the private index, which then has to serve the public packages as well.1 uv avoids the problem by default: it stops at the first index that has a package, and an index configured under [[tool.uv.index]] comes before PyPI, so a package found there is never taken from PyPI. An index marked explicit = true serves only the packages pinned to it.9
# pyproject.toml (uv)
[[tool.uv.index]]
name = "internal"
url = "https://pypi.internal.example.com/simple"
explicit = true
[tool.uv.sources]
acme-billing = { index = "internal" }uv's unsafe-best-match strategy comes closest to pip's behaviour and brings the risk back.9 pip's --require-hashes checks every requirement against a hash recorded in the requirements file, so a download that differs from the recorded one fails the install.5
For typosquats and invented names, run npm view on a package before adding it and compare its download count with what a well-known package would have.2 Also read a project's .npmrc and .yarnrc.yml for registry lines before installing, since they decide where every package comes from.2
Check how this project resolves package names. List every private or internal package it depends on and say whether each one is scoped (npm) or pinned to a private index (Python). Look in .npmrc, .yarnrc.yml, pip.conf, requirements files, CI scripts and pyproject.toml for --extra-index-url, PIP_EXTRA_INDEX_URL, unscoped private names and uv index-strategy settings. For each package added in the last month, confirm it exists on the public registry under that exact name and has a real download history. Report what you found and propose changes; do not install anything.
In SecFlash
When a dependency, typosquatted or not, matches a published malware advisory, SecFlash reports SF-SEC-713 on the Security page and SF-THR-201 on the Threats page.
See also
- Malicious packages
- Software supply chain compromise
- Responding to a malicious package installation
- Package install scripts
- Lockfile
- Package provenance
- Software composition analysis
- Category: Software supply chain
References
- a b c d e f g Birsan, Alex (9 February 2021). "Dependency Confusion: How I Hacked Into Apple, Microsoft and Dozens of Other Companies". Medium.
- a b c d e f g h i "NPM Security Cheat Sheet". OWASP Cheat Sheet Series.
- back Silverio, C J (26 December 2017). "New Package Moniker rules". npm Blog (archive).
- a b "Threats and Mitigations". npm Docs. GitHub.
- a b c "pip install". pip documentation, stable.
- a b PyTorch Foundation (31 December 2022). "Compromised PyTorch-nightly dependency chain between December 25th and December 30th, 2022". PyTorch Blog.
- back Sharma, Ax (1 January 2023). "PyTorch discloses malicious dependency chain compromise over holidays". BleepingComputer.
- a b "scope". npm Docs, CLI v11. GitHub.
- a b "Package indexes". uv documentation. Astral.
External links
- NPM Security Cheat Sheet, OWASP Cheat Sheet Series
- Compromise Software Dependencies and Development Tools, MITRE ATT&CK