Typosquatting asks almost nothing of an attacker. Register a name one character away from a popular package, attach a hostile post-install script, and wait. npm carries over two million published names, so proximity to a popular one is inevitable at that scale. Rare edge case is the wrong frame.
No maintainer account needs to fall. No reviewer needs convincing. A free registry account and some patience do the whole job.
Cheap to scale, expensive to absorb
The economics lean heavily one way. Register ten names one or two characters off popular utilities, and nine may never be installed by anyone. The tenth gets pulled by a developer who fat-fingered the name in a package.json add command, or clicked the wrong registry result while searching. An hour of attacker effort, and a CI environment now runs hostile code.
Extending the campaign costs next to nothing. Republish the same hostile package under twenty more variants. Registries such as npm remove confirmed typosquats when someone reports them, but days pass between discovery and removal, and re-registering under fresh variants comes easier and faster than reporting them does.
Recent years raised the stakes because the packages improved. Early typosquats stood out: no functionality, no README, a post-install script suspiciously brief. Today's versions often copy enough of the genuine package's behavior to deflect a glance, while exfiltration or persistence code runs down a separate path.
Why npm draws the attacks
npm's naming conventions open typosquatting surface on their own. Names treat case loosely and handle hyphens and underscores inconsistently across ecosystems. Type lodash and you get the intended package. Type Lodash, 1odash, or lodash_ and you might get something else entirely.
Scoped packages (@org/package-name) arrived partly to dampen this. Squatting a scope is harder, because the attacker would need control of the namespace rather than a similar name. Unscoped packages still dominate by volume, though, and most of the third-party utilities developers install casually are unscoped.
Discovery habits add to the exposure. Run npm install somepackage against a package that does not exist and npm answers with a 404. Search the registry UI for a utility, though, and results rank by download count and keyword match. A typosquat with an aggressive README can sit near or above the legitimate package until download counts pull the two apart.
Detection methods and what they miss
Name similarity screening
The plainest check compares each installed name against a list of high-download packages using edit distance, Levenshtein or similar. A name landing within distance 1 or 2 of one with millions of weekly downloads deserves a look.
Precision is where it gets hard. With more than two million names on npm, plenty of legitimate packages sit close to popular ones on purpose, extending or complementing them. Any threshold that catches every typosquat also drags in a pile of false positives. We tune the signal carefully, because a collision with a package that has no install history, no repository link, and a post-install script is nothing like a collision with a well-established fork of a popular utility.
Behavioral analysis
Proximity by itself settles little. Signals inside the package carry more weight:
- Post-install scripts that make outbound network calls on first install
- Package contents that do not match the README or stated purpose
- Presence of encoded or obfuscated JavaScript strings that resolve at runtime
- Package metadata that was created recently and has never been downloaded before your install
We read behavior next to name proximity. A package scoring high on both, close name and suspicious behavior, gets a critical flag before the install step finishes. A package scoring on one dimension only gets a warning in the scan report that does not block the install by default.
The gap between publish and takedown
Here is a shape security teams meet regularly. A developer building a data pipeline needs CSV parsing and types the utility name slightly wrong, pulling a recently published package that shares most of the name. The package parses CSV fine. Its post-install script reads the CI environment variables and carries off whatever secrets were injected.
Nobody notices, because everything works. The CI run goes green. Three days have passed since the package went live and no report has been filed. No CVE names it, so the CVE scan at the end of the pipeline reports clean.
Name similarity plus behavioral scanning targets exactly this case. Package name, metadata age, and post-install behavior together form a signal readable before or during installation, long before any database could hold an entry.
Where scanning runs out of room
A determined typosquat can still get through. Build one with full legitimate functionality and defer the hostile behavior to a later execution context, and install-time signals thin out. Such packages exist, and their number grows.
Pre-install scanning bends the attacker's cost-benefit math regardless. Running behavioral analysis at every install makes install-time evasion harder. Evasion stays possible, but a package must now dodge name-proximity and behavioral checks at the same time, which takes considerably more construction work.
Lockfile discipline supplies the complement. With npm ci against a committed lockfile, the install resolves to the exact packages recorded, not whatever currently tops the registry for a given name. A typosquat can only enter through a manual npm install that rewrites the lockfile, and a lockfile rewrite is visible to review in a way silent registry resolution never is.
Teams wiring scanning through a pipeline can read the earlier article on securing software pipelines from commit to deploy for where to place these checks around the install step to get the widest coverage.