CVE-2023-31543
Dependency confusion in pipreqs v0.3.0 to v0.4.11 allows arbitrary code execution via crafted PyPI package upload
- CVSS 9.8
- CWE-427 Uncontrolled Search Path Element
- Cryptographic
- Remote
A dependency confusion in pipreqs v0.3.0 to v0.4.11 allows attackers to execute arbitrary code via uploading a crafted PyPI package to the chosen repository server.
- CVSS base score
- 9.8
- Published
- 2023-06-30
- OWASP
- A06 Vulnerable and Outdated Components
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Cryptographic
- Subcategory
- Unencrypted communication
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- pipreqs
- Fixed by upgrading
- Yes
Solution
Update pipreqs to version 0.4.12 or higher.
Vulnerable code sample
import subprocess
def install_package(package_name):
# Arbitrarily install a package without verifying its source
print(f"Installing {package_name}...")
subprocess.run(["pip", "install", package_name], check=True)
def load_dependencies(dependencies):
for dep in dependencies:
install_package(dep)
# Example usage with a potentially untrusted package
dependencies = ['trusted_package', 'untrusted_package']
load_dependencies(dependencies)Patched code sample
import requests
import pkg_resources
def safe_import(package_name):
# Ensure that the package is installed from a trusted source
try:
# Check if the package is in the local environment
pkg_resources.get_distribution(package_name)
except pkg_resources.DistributionNotFound:
# If not found, raise an error instead of allowing arbitrary installation
raise ImportError(f"Package '{package_name}' is not installed or is untrusted.")
def install_package(package_name):
# This function simulates the installation of a package
# In a real-world scenario, you would ensure that the package is from a trusted source
response = requests.get(f"https://pypi.org/pypi/{package_name}/json")
if response.status_code == 200:
# Proceed with installation logic
print(f"Installing {package_name}...")
else:
raise Exception(f"Package '{package_name}' not found on PyPI.")
# Example usage
try:
safe_import('trusted_package')
except ImportError as e:
print(e)
try:
install_package('untrusted_package')
except Exception as e:
print(e)Published fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- fix name resolution for local packages (bndr/pipreqs)
pipreqs/pipreqs.py · get_import_local,pipreqs/pipreqs.py · init
Cite this entry
@misc{vaitp:cve202331543,
title = {{Dependency confusion in pipreqs v0.3.0 to v0.4.11 allows arbitrary code execution via crafted PyPI package upload}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2023},
note = {VAITP Python Vulnerability Dataset, entry CVE-2023-31543},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-31543/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
