nltk: Arbitrary File Read via Path Traversal in nltk.data.load() through Percent-Encoded Sequences
The NLTK library's data loading functions decode percent-encoded sequences after performing path safety checks, allowing an attacker to bypass validation and read arbitrary files outside the intended data directory. An attacker who controls the resource name can read any file the process can access, such as credentials or configuration files.
nltk/nltk
Originally disclosed on huntr
Summary
nltk.data.load() and nltk.data.find() resolve user-supplied resource names to filesystem paths using url2pathname(), which decodes percent-encoded sequences (e.g. %2e%2e to ..). Path safety checks are performed on the raw, still-encoded string before decoding occurs. An attacker supplying %2e%2e instead of .. bypasses all path validation and reads arbitrary files outside the NLTK data directory.
Vulnerable Code
nltk/data.py - find() function: url2pathname() decodes %2e%2e -> .. AFTER any safety check p = os.path.join(path_, url2pathname(resource_name)) if os.path.exists(p): return FileSystemPathPointer(p)
Proof of Concept
import nltk.data nltk.data.path = ["/home/user/nltk_data"] %2e%2e decodes to .. via url2pathname(), escaping the data dir data = nltk.data.load("%2e%2e/SECRET_credentials.txt", format="raw") print(data) b'AWS_SECRET_KEY=AKIAIOSFODNN7EXAMPLE\nDATABASE_PASS=hunter2\n' All of these bypass path checks and decode identically:
Payload After url2pathname()
%2e%2e/secret ../secret .%2e/secret ../secret %2e./secret ../secret %2E%2E/secret ../secret Root Cause url2pathname() is called after path safety checks, not before. Encoding .. as %2e%2e passes every check, then decodes to a traversal sequence at filesystem access time.
Fix
Decode before checking:
from urllib.parse import unquote resource_name = unquote(resource_name) # decode first, then validate
Impact
An attacker who controls the resource name passed to nltk.data.load() can read any file the process has permission to access - credentials, environment files, SSH private keys, /etc/passwd, /proc/self/environ, application config files, etc. This affects any application that passes user-controlled input to nltk.data.load() or nltk.data.find().
From the GitHub Advisory Database (GHSA-m42h-3232-vpv3, CC BY 4.0).
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the path safety check is performed on the raw, still-encoded string before url2pathname() decodes percent-encoded sequences, so encoded traversal sequences bypass validation.
What the attacker needed
Attacker controls resource name passed to nltk.data.load()
The fix
The fix is to decode the resource name before performing path safety checks.
How it was found
- Set nltk.data.path to a directory.
- Call nltk.data.load() with a resource name containing percent-encoded traversal sequences.
- The function decodes the sequences after checks, allowing access to files outside the data directory.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
The library checks a file path for '..' (which means 'go up a folder') before it decodes special characters like %2e. An attacker can write %2e%2e instead of '..', so the check passes, but then the library decodes it to '..' and allows escaping the folder. This lets the attacker read files they should not see.
What is Path traversal?
Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.