{"id":394,"cve":"CVE-2023-31543","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2023-31543","cwe":"CWE-427 Uncontrolled Search Path Element","cwe_number":427,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/427","cvss_score":9.8,"published":"2023-06-30","owasp":"A06 Vulnerable and Outdated Components","odc":"Function","cdc":"Incorrect Functionality","category":"Cryptographic","subcategory":"Unencrypted communication","scope":"Remote","impact":"Arbitrary Code Execution","summary":"Dependency confusion in pipreqs v0.3.0 to v0.4.11 allows arbitrary code execution via crafted PyPI package upload","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2023-31543\/","description":"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.","solution":"Update pipreqs to version 0.4.12 or higher.","affected_component":"pipreqs","fixed_by_upgrading":true,"vulnerable_code":"import subprocess\n\ndef install_package(package_name):\n    # Arbitrarily install a package without verifying its source\n    print(f\"Installing {package_name}...\")\n    subprocess.run([\"pip\", \"install\", package_name], check=True)\n\ndef load_dependencies(dependencies):\n    for dep in dependencies:\n        install_package(dep)\n\n# Example usage with a potentially untrusted package\ndependencies = ['trusted_package', 'untrusted_package']\nload_dependencies(dependencies)","patched_code":"import requests\nimport pkg_resources\n\ndef safe_import(package_name):\n    # Ensure that the package is installed from a trusted source\n    try:\n        # Check if the package is in the local environment\n        pkg_resources.get_distribution(package_name)\n    except pkg_resources.DistributionNotFound:\n        # If not found, raise an error instead of allowing arbitrary installation\n        raise ImportError(f\"Package '{package_name}' is not installed or is untrusted.\")\n\ndef install_package(package_name):\n    # This function simulates the installation of a package\n    # In a real-world scenario, you would ensure that the package is from a trusted source\n    response = requests.get(f\"https:\/\/pypi.org\/pypi\/{package_name}\/json\")\n    if response.status_code == 200:\n        # Proceed with installation logic\n        print(f\"Installing {package_name}...\")\n    else:\n        raise Exception(f\"Package '{package_name}' not found on PyPI.\")\n\n# Example usage\ntry:\n    safe_import('trusted_package')\nexcept ImportError as e:\n    print(e)\n\ntry:\n    install_package('untrusted_package')\nexcept Exception as e:\n    print(e)","payload_needed":false,"payload":null,"payload_status":"none","published_fix":[{"repository":"bndr\/pipreqs","commit_url":"https:\/\/github.com\/bndr\/pipreqs\/commit\/3f5964fcb90ec6eb6df46d78e651a1b73538d0ba","commit_subject":"fix name resolution for local packages","units":[{"file":"pipreqs\/pipreqs.py","function":"get_import_local"},{"file":"pipreqs\/pipreqs.py","function":"init"}]}],"references":null}