CWE-506: Malicious & Typosquatted Packages
Malicious and typosquatted packages carry hidden harmful code that runs when you install or import a dependency you did not mean to trust.
Also known as: Malicious Packages, Typosquatting, Supply Chain Malware, Package Impersonation
Risk
A single malicious package can steal credentials, install backdoors, mine cryptocurrency, or wipe data the moment it is installed, often through an install script that runs before any code review. Because dependencies run with the developer's or build system's privileges, one bad package can compromise an entire organization.
Definition
What it is
Attackers publish packages that either impersonate popular libraries through near-identical names (typosquatting) or hide malicious behavior inside otherwise useful code. The harmful logic commonly runs in a postinstall hook or on first import, so it executes before anyone reads the source. A fast-moving developer who mistypes a name, trusts an autocompleted suggestion, or copies an install command can pull one in without noticing. The defense is to verify the identity and provenance of every package before it is allowed to install.
Use-cases
How it shows up in real code
One-character typo
A developer means to install the popular library but transposes two letters. The squatted package installs, runs a postinstall script that reads environment variables, and ships them to an attacker endpoint.
Hallucinated name is claimed
An AI assistant suggests a package name that does not exist. An attacker who monitors these hallucinations publishes it with embedded malware, so the next developer who trusts the suggestion installs working-looking but malicious code.
Compromised maintainer account
A legitimate, widely used package gets a new release after its maintainer account is phished. The update embeds a credential stealer, and thousands of projects pull it on their next build.
Lookalike scoped package
An attacker publishes a package under a scope that resembles a trusted vendor. A hurried team installs it believing it is official, and it quietly injects a backdoor into the bundled output.
AI risk
Why AI-generated code has increased the exposure
- Assistants frequently suggest package names that do not exist, and attackers claim these hallucinated names and fill them with malicious code.
- Models can recommend a near-identical name to a popular library, steering developers straight into a typosquat.
- Generated install commands are run verbatim, so a wrong or impersonating name executes its install scripts before any human reads it.
- Assistants reproduce package names from training data without checking current ownership, so a name that changed hands to an attacker is still suggested.
- The sheer volume of generated code pulls in many dependencies quickly, widening the window for a single malicious one to slip through unexamined.
Vibe coding
How vibe coding amplifies the risk
- Vibe coders install suggested packages unread, trusting the name and running install scripts without inspecting them.
- Mistyped or autocompleted names go uncorrected because the build still appears to work.
- Prototypes reach production carrying dependencies no one ever verified the provenance of.
- Without lockfile discipline, a later reinstall can swap in a freshly published malicious version of a name.
- Rapid scaffolding adds packages faster than anyone can review who published them or what their install hooks do.
Detection
How to find it
- Cross-check every new dependency name against the canonical spelling, publisher, and download history of the intended library.
- Run a malicious-package scanner that flags known squats, suspicious install scripts, and recently published lookalikes.
- Review postinstall and preinstall hooks for network calls, obfuscated code, or filesystem access.
- Verify that AI-suggested package names actually exist and are owned by the expected maintainer before installing.
Remediation
How to fix it
- Install dependencies with install scripts disabled by default and enable them only for vetted packages.
- Pin exact versions and commit lockfiles with integrity hashes so a swapped package fails verification.
- Require packages to be fetched through an internal proxy or allowlist that vets publisher and provenance.
- Verify package signatures or provenance attestations where the registry supports them.
- Add a CI gate that runs a malware and typosquat scanner and blocks any unrecognized or unsigned dependency.
Frequently asked questions
- What is CWE-506 (malicious & typosquatted packages)?
- CWE-506 is embedded malicious code: harmful logic hidden inside a dependency. Typosquatted packages impersonate popular libraries with near-identical names, while other malicious packages hide their payload in otherwise working code. The code often runs at install time, before anyone reviews it.
- How have AI assistants and vibe coding increased the risk?
- Assistants invent package names that attackers then publish as malware, and they suggest lookalike names that lead to typosquats. Vibe coders run generated install commands verbatim and add many dependencies quickly without checking who published them. That combination makes it easy to install a malicious package without noticing.
- How do I fix malicious & typosquatted packages?
- Disable install scripts by default, pin exact versions, and commit lockfiles with integrity hashes. Fetch packages through a vetted proxy or allowlist, verify publisher and signatures, and add a CI gate that runs a malware and typosquat scanner to block unrecognized dependencies.
Related weaknesses
Catch CWE-506 before you ship
Tethlyn Audit scans your codebase and its git history for CWE-506 and the rest of the dependency & supply chain risk class, then ranks each finding by real exploitability and data sensitivity.