Skip to content

Lesson 2 of 9 · 23m

Typosquatting, Name Squatting, and Dependency Confusion

Understand how attackers exploit naming, human typing, namespace gaps, and registry resolution order to make you request hostile code voluntarily.

Most supply-chain attacks do not break into your network. They arrange for your tooling to invite them in. Naming attacks are devastating because the initial execution path is legitimate: a developer or CI job asks for a package, the resolver returns attacker-controlled code, and the installation pipeline does exactly what it was designed to do.

Typosquatting is a human-interface exploit

Attackers register names that are one edit away from popular packages:

requests     -> reqests, requestz, reuests
lodash       -> lodas, lodashs, 1odash
cross-env    -> crossenv
python-dateutil -> python3-dateutil

The mistake can be a typo, autocomplete acceptance, copied documentation, or a dependency name embedded in an internal wiki. Once installed, the malicious package often implements the real API plus a hostile install hook, making functional testing useless as a security control.

Dependency confusion exploits resolution order

In 2021 Alex Birsan demonstrated that private package names leak into public ecosystems. If internal code depends on @acme/billing-utils and that name is absent from the public npm registry, an attacker can publish a high-version public package with the same name. Depending on registry configuration, the resolver may choose the public package:

{
  "dependencies": {
    "@acme/billing-utils": "^3.0.0"
  }
}

If the attacker publishes version 99.0.0, version comparison can become an attack primitive. This is not theoretical: npm, PyPI, RubyGems, Maven mirrors, NuGet, and artifact proxies have all had resolution-order footguns.

Namespaces are a security boundary only when enforced

npm scopes, GitHub organization names, Docker namespaces, Maven group IDs, and private registry namespaces look like ownership proof, but enforcement varies. Configure the resolver so a private namespace cannot escape:

# .npmrc
@acme:registry=https://artifacts.acme.corp/api/npm/private/
registry=https://registry.npmjs.org/
strict-ssl=true

That alone is not enough if the proxy merges namespaces or favors upstream versions. Reserve internal names publicly where feasible, and prefer names that are not attractive collision targets.

Install-time behavior is the payload window

npm lifecycle scripts and Python source distributions historically provide convenient execution points:

{
  "scripts": {
    "preinstall": "node index.js",
    "postinstall": "node setup.js"
  }
}

The package does not need to be imported. Installation is execution. Review preinstall, install, postinstall, prepare, native compilation, binary downloads, and build plugins. "It only ran in CI" is not a comforting sentence when CI holds production deploy credentials.

Field notes

  • Package age, maintainer history, repository linkage, and release cadence are stronger signals than download count alone.
  • Do not treat lockfiles as proof the first installation was safe; they preserve whatever was resolved, including malicious code.
  • Scoped npm packages reduce accidental collision only if every client and proxy consistently routes the scope privately.
  • Typo reports are security incidents, not embarrassing trivia. Capture who installed it, when, and on which machine class.

Was this lesson helpful?

Sign in to rate