🛡️ NPM Security Landscape: Threats and Attack Vectors

Context: This document forms the theoretical foundation of the NPM Supply Chain Network Analysis project. It explains why the topological risk model used in the project is necessary, through the lens of active threats in the ecosystem.

1. Introduction: Why is NPM a Primary Attack Surface?

Node Package Manager (NPM) is the world's largest software registry, hosting over 3 million packages and processing billions of downloads weekly. Modern software development processes heavily rely on third-party code libraries (dependencies) to increase speed. However, when a single NPM package is added to a project, an average of 79 transitive (indirect) packages are also implicitly included in the trust chain.

This "implicit trust" model makes NPM an attractive target for attackers. By compromising a single popular package or a dependency deep within it, attackers gain access to a "one-to-many" distribution mechanism that can affect millions of developers and end users. This document defines the active threats in the NPM ecosystem and the technical defense mechanisms that should be taken against them.


2. Attack Types and Technical Taxonomy

The following attack vectors represent the most common and critical techniques targeting the modern software supply chain.

2.1. Shrinkwrapped Clones (Packaged Clones)

Risk Level: High
Definition: When an attacker or careless developer copies a legitimate package and publishes it under a new name without referencing the original package.

2.2. Typosquatting (Typo Hunting)

Risk Level: Medium-High
Definition: Publishing malicious packages with names similar to popular packages (e.g., raect instead of react or clolors instead of colors).

2.3. Dependency Confusion (Dependency Confusion)

Risk Level: Critical
Definition: Publishing the same name as a private package used by companies in their internal networks on the public NPM registry with a higher version number.

2.4. Account Takeover (ATO) & Worms (Account Hijacking)

Risk Level: Critical
Definition: Compromise of trusted package developer accounts (due to weak passwords, phishing, or leaked tokens) and injection of malicious code into their packages.

2.5. Protestware / Malicious Updates (Malicious Updates)

Risk Level: High
Definition: A legitimate package owner intentionally breaking (sabotaging) the package or adding malicious functionality to it.


3. Technical Defense Strategies and Measures

The defense-in-depth principle should be applied to narrow the attack surface.

3.1. Installation and CI/CD Security

Method Description and Implementation
npm ci Usage Never use npm install in CI/CD environments. npm ci (clean install) adheres to versions in package-lock.json, doesn't update package.json, and guarantees reproducibility of installation.
Lockfile Analysis Lockfiles contain integrity hashes (SHA-512) and resolved URLs of packages. Tools like lockfile-lint should be used to check whether packages are being downloaded from untrusted sources (e.g., attacker's own server).
Script Disabling If possible, use the npm install --ignore-scripts parameter during installation to prevent the execution of preinstall and postinstall scripts, which are most commonly used by attackers.

3.2. Developer and Organizational Measures

Scoped Packages

For internal company packages, Scoped Packages in the format @company/package-name must be used. This informs the NPM registry that the package belongs to a user or organization and allows the .npmrc file to route this scope only to the private registry (e.g., Artifactory, Verdaccio) to prevent Dependency Confusion attacks.

2FA and Token Security

Two-Factor Authentication (2FA) should be made mandatory for NPM accounts (especially for accounts with publish privileges). When using Automation Tokens, granular authorizations should be set to allow only necessary packages and IP addresses (CIDR whitelist).

Software Composition Analysis (SCA) and Security Tools

3.3. Manifest File Inspection

Developers should pay particular attention to the following areas in the package.json file:


4. Summary Table: Attack and Mitigation Matrix

Attack Type Primary Vector Key Mitigation
Shrinkwrapped Clones Cloned/Modified Code Source code analysis, OSSF Scorecard (Maintenance metrics)
Typosquatting Name Similarity Careful package name verification, Scoped packages usage
Dependency Confusion Version Priority @scope usage, Private registry configuration (.npmrc)
Account Takeover Identity Theft Mandatory 2FA, Granular Tokens
Malicious Updates Automatic Updates (^, ~) Dependency Pinning (Version Pinning), npm ci
Install Scripts postinstall Scripts --ignore-scripts, Socket.dev behavior analysis