Overview
This vulnerability, often a core component of software supply chain attacks, occurs when an application includes and executes code or functionality from an untrusted or unverified source (an “untrusted control sphere”). This includes:- Dependency Confusion: An attacker publishes a malicious package to a public repository (like PyPI, npm) with the same name as an internal package. The build tool might accidentally download the malicious public version instead of the trusted internal one.
- Typosquatting: An attacker publishes a malicious package with a name very similar to a popular one (e.g.,
djangainstead ofdjango). - Compromised Dependencies: A legitimate package is hijacked by an attacker who publishes a new, malicious version.
Business Impact
Including untrusted functionality is one of the most severe risks, as it effectively hands control over to the attacker:- Remote Code Execution (RCE): The malicious package can run any code on the build server or the production server.
- Credential Theft: The package can steal environment variables, secrets, API keys, and developer credentials during the build or at runtime.
- Data Exfiltration: Malicious code can read and send sensitive application data to the attacker.
- Backdoors: The package can install persistent backdoors for future access.
- Ransomware/Malware: The server can be compromised with ransomware or used to spread malware.
Reference Details
CWE ID: CWE-829
Related CWEs: CWE-494 (Integrity Check), CWE-1104 (Outdated Components)
OWASP Top 10 (2021): A08:2021 - Software and Data Integrity Failures
Severity: Critical
Framework-Specific Analysis and Remediation
This vulnerability is about the process of acquiring and managing dependencies for any framework. The defense lies in securing the build and deployment pipeline and verifying the source of all components. Key Remediation Principles:- Use Private Repositories: Host internal packages on a private, authenticated repository (like a private PyPI server, npm registry, Artifactory, Nexus).
- Explicit Repository Configuration: Configure package managers (
pip,npm,maven) to only use your trusted private repository, or to prioritize it. Ensure they don’t fall back to public repositories for internal package names. - Use Lock Files: Always commit a fully resolved lock file (
package-lock.json,poetry.lock,composer.lock,Gemfile.lock,yarn.lock) to version control. This ensures builds are repeatable and use the exact, vetted versions of dependencies. - Use Integrity Hashes: Use
requirements.txtwith hashes (--hash) or npm’spackage-lock.json/ Yarn’syarn.lock, which include integrity hashes (Subresource Integrity - SRI). - SCA Scanning: Regularly scan dependencies for known vulnerabilities (see
CWE-1104) and for suspicious packages (e.g., typosquatted names). - Namespace/Scope Packages: Use private namespaces or scopes (e.g.,
@mycompany/internal-packagein npm) to prevent name collisions with public packages.
- Python
- Java
- .NET(C#)
- PHP
- Node.js
- Ruby
Framework Context
Usingpip with requirements.txt or pyproject.toml (Poetry/Flit) where internal package names might collide with public PyPI.Vulnerable Scenario 1: Dependency Confusion
An internal package is namedmy-corp-utils. An attacker publishes a malicious package named my-corp-utils to the public PyPI.Vulnerable Scenario 2: Typosquatting
A developer manually adds a dependency with a typo.Mitigation and Best Practices
- Configure
pip.confto only use your internal repository, or use the--index-urland--extra-index-urlflags carefully, ensuring your private index is the primary one. - Use
pip freeze > requirements.txtto capture exact versions. - Use hashes in your requirements file:
pip hash requirements.txt >> requirements.txt. This ensurespip install -r requirements.txt --require-hasheswill fail if the package content changes.
Secure Code Example
Testing Strategy
Auditrequirements.txt / pyproject.toml for package names. Check if any internal package names are also available for registration on public PyPI. Run SCA tools that check for typosquatting. Enforce --require-hashes or use tools like Poetry/Pipenv which use lock files with hashes by default.
