{"id":924,"cve":"CVE-2023-45805","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2023-45805","cwe":"CWE-20 Improper Input Validation","cwe_number":20,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/20","cvss_score":7.8,"published":"2023-10-20","owasp":"A06 Vulnerable and Outdated Components","odc":"Function","cdc":"Incorrect Functionality","category":"Input Validation and Sanitization","subcategory":"Insecure Parsing or Deserialization","scope":"Remote","impact":"Arbitrary Code Execution","summary":"Malicious pdm.lock file allows project substitution","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2023-45805\/","description":"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.","solution":"Update PDM to version 2.9.4 or higher.","affected_component":"pdm","fixed_by_upgrading":true,"vulnerable_code":"import re\n\ndef install_package(package_name, version):\n    # Simulated package installation logic\n    # Vulnerable to incorrect package name and version matching\n    package_version = f\"{package_name}-{version}\"\n    \n    # This allows for an incorrect version to be parsed\n    # e.g., \"foo-2\" could be interpreted as \"foo\" version \"2-2\"\n    if re.match(r'^[a-zA-Z0-9-_]+-[0-9]+$', package_version):\n        print(f\"Installing {package_name} version {version}...\")\n        # Simulate the installation process\n        # In reality, this could lead to installing a different package\n        # due to the lack of strict validation.\n    else:\n        print(\"Invalid package version format.\")\n\n# Example usage that demonstrates the vulnerability\ninstall_package(\"foo\", \"2-2\")  # This would incorrectly interpret the version","patched_code":"import re\nfrom packaging.version import parse as parse_version\n\ndef validate_package_name_and_version(package_name, version):\n    # Check if the package name matches exactly\n    if package_name != version.split('-')[0]:\n        raise ValueError(\"Package name does not match the version prefix.\")\n    \n    # Validate the version against PEP 440\n    if parse_version(version) is None:\n        raise ValueError(\"Invalid version format.\")\n\n# Example usage\ntry:\n    validate_package_name_and_version(\"foo\", \"foo-2.0\")\nexcept ValueError as e:\n    print(e)","payload_needed":false,"payload":null,"payload_status":"none","published_fix":[{"repository":"pdm-project\/pdm","commit_url":"https:\/\/github.com\/pdm-project\/pdm\/commit\/6853e2642dfa281d4a9958fbc6c95b7e32d84831","commit_subject":"Merge pull request from GHSA-j44v-mmf2-xvm9","units":[{"file":"src\/pdm\/models\/repositories.py","function":null},{"file":"src\/pdm\/models\/repositories.py","function":"LockedRepository._read_lockfile"}]}],"references":null}