{"id":415,"cve":"CVE-2022-23531","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2022-23531","cwe":"CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')","cwe_number":22,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/22","cvss_score":7.8,"published":"2022-12-17","owasp":"A06 Vulnerable and Outdated Components","odc":"Function","cdc":"Incorrect Functionality","category":"Input Validation and Sanitization","subcategory":"Path Traversal","scope":"Local","impact":"Arbitrary Code Execution","summary":"GuardDog CLI tool (<0.1.5) Vulnerable to Local PyPI Package Relative Path Traversal for Arbitrary File Writes","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2022-23531\/","description":"GuardDog is a CLI tool to identify malicious PyPI packages. Versions prior to 0.1.5 are vulnerable to Relative Path Traversal when scanning a specially-crafted local PyPI package. Running GuardDog against a specially-crafted package can allow an attacker to write an arbitrary file on the machine where GuardDog is executed due to a path traversal vulnerability when extracting the .tar.gz file of the package being scanned, which exists by design in the tarfile.TarFile.extractall function. This issue is patched in version 0.1.5.","solution":"Update GuardDog to version 0.1.5 or higher.","affected_component":"GuardDog","fixed_by_upgrading":true,"vulnerable_code":"import tarfile\n\ndef extract_package(file_path):\n    with tarfile.open(file_path, \"r:gz\") as tar:\n        tar.extractall()  # Vulnerable to path traversal","patched_code":"import tarfile\nimport os\n\ndef safe_extract(tar, path=\".\", members=None, *, numeric_owner=False):\n    # Prevent path traversal by ensuring the target path is within the allowed directory\n    if members is None:\n        members = tar.getmembers()\n    for member in members:\n        member_path = os.path.join(path, member.name)\n        # Check if the member path is safe\n        if not os.path.commonprefix([path, member_path]) == path:\n            raise Exception(\"Attempted Path Traversal detected: {}\".format(member_path))\n    tar.extractall(path, members, numeric_owner=numeric_owner)\n\n# Usage example\ndef scan_package(file_path):\n    with tarfile.open(file_path, \"r:gz\") as tar:\n        safe_extract(tar, path=\"safe_directory\")","payload_needed":false,"payload":null,"payload_status":"none","published_fix":[{"repository":"DataDog\/guarddog","commit_url":"https:\/\/github.com\/DataDog\/guarddog\/commit\/a56aff58264cb6b7855d71b00dc10c39a5dbd306","commit_subject":"Use tarsafe instead of built-in tarfile to extract archives","units":[{"file":"guarddog\/scanners\/package_scanner.py","function":null},{"file":"guarddog\/scanners\/package_scanner.py","function":"PackageScanner.scan_local"}]}],"references":null}