CVE-2023-45805
Malicious pdm.lock file allows project substitution
- CVSS 7.8
- CWE-20 Improper Input Validation
- Input Validation and Sanitization
- Remote
pdm is a Python package and dependency manager supporting the latest PEP standards. It's possible to craft a malicious `pdm.lock` file that could allow e.g. an insider or a malicious open source project to appear to depend on a trusted PyPI project, but actually install another project. A project `foo` can be targeted by creating the project `foo-2` and uploading the file `foo-2-2.tar.gz` to pypi.org. PyPI will see this as project `foo-2` version `2`, while PDM will see this as project `foo` version `2-2`. The version must only be `parseable as a version` and the filename must be a prefix of the project name, but it's not verified to match the version being installed. Version `2-2` is also not a valid normalized version per PEP 440. Matching the project name exactly (not just prefix) would fix the issue. When installing dependencies with PDM, what's actually installed could differ from what's listed in `pyproject.toml` (including arbitrary code execution on install). It could also be used for downgrade attacks by only changing the version. This issue has been addressed in commit `6853e2642df` which is included in release version `2.9.4`. Users are advised to upgrade. There are no known workarounds for this vulnerability.
- CVSS base score
- 7.8
- Published
- 2023-10-20
- OWASP
- A06 Vulnerable and Outdated Components
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- pdm
- Fixed by upgrading
- Yes
Solution
Update PDM to version 2.9.4 or higher.
Vulnerable code sample
import re
def install_package(package_name, version):
# Simulated package installation logic
# Vulnerable to incorrect package name and version matching
package_version = f"{package_name}-{version}"
# This allows for an incorrect version to be parsed
# e.g., "foo-2" could be interpreted as "foo" version "2-2"
if re.match(r'^[a-zA-Z0-9-_]+-[0-9]+$', package_version):
print(f"Installing {package_name} version {version}...")
# Simulate the installation process
# In reality, this could lead to installing a different package
# due to the lack of strict validation.
else:
print("Invalid package version format.")
# Example usage that demonstrates the vulnerability
install_package("foo", "2-2") # This would incorrectly interpret the versionPatched code sample
import re
from packaging.version import parse as parse_version
def validate_package_name_and_version(package_name, version):
# Check if the package name matches exactly
if package_name != version.split('-')[0]:
raise ValueError("Package name does not match the version prefix.")
# Validate the version against PEP 440
if parse_version(version) is None:
raise ValueError("Invalid version format.")
# Example usage
try:
validate_package_name_and_version("foo", "foo-2.0")
except ValueError 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.
- Merge pull request from GHSA-j44v-mmf2-xvm9 (pdm-project/pdm)
src/pdm/models/repositories.py,src/pdm/models/repositories.py · LockedRepository._read_lockfile
Cite this entry
@misc{vaitp:cve202345805,
title = {{Malicious pdm.lock file allows project substitution}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2023},
note = {VAITP Python Vulnerability Dataset, entry CVE-2023-45805},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-45805/}}
}
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 ::
