VAITP Dataset

← Back to the dataset

CVE-2026-54293

Path traversal in NLTK's data.load() using URL-encoded characters.

  • CVSS 7.5
  • CWE-22
  • Input Validation and Sanitization
  • Remote

NLTK (Natural Language Toolkit) is a suite of open source Python modules, data sets, and tutorials supporting research and development in Natural Language Processing. Prior to 3.10.0-rc1, nltk.data.load() in NLTK is vulnerable to path traversal via URL-encoded path separators and traversal segments when using the nltk: URL scheme. The unsafe-path regex check is performed before url2pathname() decodes the %xx sequences (a classic decode-after-check / TOCTOU-style flaw), allowing an attacker to bypass the protection documented in NLTK's SECURITY.md and read arbitrary files from the filesystem. While literal traversal strings such as ../../../etc/passwd are correctly blocked, encoded variants such as %2fetc%2fpasswd, %2e%2e%2f…, and ..%2f..%2f slip past the regex and are subsequently decoded into a real filesystem path. This vulnerability is fixed in 3.10.0-rc1.

CVSS base score
7.5
Published
2026-06-22
OWASP
A01 Broken Access Control
Orthogonal defect classification
Checking
Code defect classification
Incorrect Check
Category
Input Validation and Sanitization
Subcategory
Path Traversal
Accessibility scope
Remote
Impact
Information Disclosure
Affected component
NLTK
Fixed by upgrading
Yes

Solution

Upgrade NLTK to version 3.10.0-rc1 or later.

Vulnerable code sample

import re
import os
from urllib.request import url2pathname

_UNSAFE_PATH_REGEX = re.compile(r"""
    (^(/|\\)) | # starts with a path separator
    (\.\.)      | # contains '..'
    (?!nltk:)     # does not start with 'nltk:'
""", re.VERBOSE)

def find(resource_name, paths=None):
    # This is a simplified stand-in for NLTK's actual find mechanism.
    # It demonstrates the path joining part of the vulnerability.
    if paths is None:
        # A default search path for the example
        paths = ["/tmp/nltk_data"]

    for path in paths:
        # The decoded, and now unsafe, path is joined here.
        full_path = os.path.join(path, resource_name)
        if os.path.exists(full_path):
            return full_path
    raise LookupError(f"Resource {resource_name} not found.")

def load(resource_url, format='auto', cache=True, verbose=False):
    # The vulnerability is in how the resource_url is handled.
    # 1. The check is performed on the raw, URL-encoded string.
    # A path like 'corpora/../../../../../etc/passwd' would be blocked.
    if _UNSAFE_PATH_REGEX.search(resource_url):
        raise ValueError(f"Malicious path component detected in '{resource_url}'")

    # 2. The URL is then decoded *after* the check.
    # An encoded path like 'corpora/%2e%2e/%2e%2e/etc/passwd'
    # passes the regex check above, but url2pathname decodes it
    # into a malicious path. This is the core of the vulnerability.
    resource_path = url2pathname(resource_url.split(':', 1)[1])

    # 3. The decoded, potentially malicious path is used to find and open a file.
    filepath = find(resource_path)
    with open(filepath, 'rb') as f:
        return f.read()

Patched code sample

import re
from urllib.parse import unquote
import os

# This regex is a simplified representation of a check for unsafe path components.
# It looks for directory traversal attempts or absolute paths.
UNSAFE_PATH_REGEX = re.compile(r"\.\.[/\\]|[/\\]\.\.|^[/\\]|^[a-zA-Z]:[/\\]")

def fixed_nltk_data_load(resource_url: str):
    """
    A conceptual fix demonstrating how to prevent path traversal by
    decoding the URL path before validation, as described in CVE-2026-54293.
    """
    if not resource_url.startswith("nltk:"):
        raise ValueError(f"Unsupported resource URL: {resource_url}")

    path_component = resource_url[len("nltk:"):]

    # THE FIX:
    # 1. First, decode any URL-encoded characters (e.g., %2e, %2f).
    # This ensures that the security check sees the actual path components
    # an attacker is trying to use, preventing a "decode-after-check" flaw.
    decoded_path = unquote(path_component)

    # 2. Second, perform the security check on the now-decoded path.
    # An attack like '..%2f..%2fetc%2fpasswd' becomes '../etc/passwd',
    # which is now correctly caught by the regex.
    if UNSAFE_PATH_REGEX.search(decoded_path):
        raise ValueError(
            f"Path traversal or absolute path detected in '{decoded_path}'"
        )

    # If the check passes, the path is considered safe. In a real implementation,
    # it would be joined with a trusted base directory to load a file.
    # For example:
    #
    # from nltk.downloader import Downloader
    # downloader = Downloader()
    # safe_full_path = downloader.find(decoded_path)
    # with open(safe_full_path) as f:
    #     return f.read()

    # Placeholder for safe file loading logic
    pass

Payload

nltk.data.load('nltk:%2e%2e%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fetc/passwd')

Cite this entry

@misc{vaitp:cve202654293,
  title        = {{Path traversal in NLTK's data.load() using URL-encoded characters.}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2026},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2026-54293},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-54293/}}
}
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 ::