Summary
A path traversal flaw in NLTK's nltk.data.load() allows attackers to bypass regex-based path safety checks by using URL-encoded traversal sequences in the custom nltk: URL scheme.
When an application trusts attacker-controlled nltk: URLs, this bug can lead to arbitrary file reads from the local filesystem, exposing configuration data, secrets, or other sensitive content.
Affected context
This vulnerability appears in Python applications that use NLTK's nltk.data.load() with the nltk: URL scheme and rely on its documented path safety protections.
It is relevant when the URL passed to nltk.data.load() can be influenced by external input (such as user-supplied paths, configuration, or network-provided resource identifiers) and the environment exposes sensitive files readable by the NLTK process.
Exploit preconditions
- Attack surface: Any code path that calls nltk.data.load() with an nltk: URL derived from untrusted or semi-trusted input.
- Required access: Ability to supply or modify the nltk: URL string (e.g., via HTTP parameters, API fields, plug-in configuration, or job definitions in a system embedding NLTK).
- Trigger point: Invocation of nltk.data.load() where the unsafe-path regex runs before url2pathname() decoding, allowing encoded traversal segments to pass.
- Why exploitation is possible: The validation logic checks for traversal patterns on the still-encoded URL value; after validation, url2pathname() decodes %xx sequences, turning previously hidden separators and ".." segments into a real filesystem path that is then opened.
Why this happens
The bug stems from performing security checks on data in a different representation than the one ultimately used for filesystem access.
NLTK's unsafe-path regex is applied to the raw nltk: URL string, which still contains percent-encoded characters (%2f for '/', %2e for '.', etc.).
Once validation passes, url2pathname() decodes these sequences, transforming apparently safe input into paths containing directory separators and traversal segments.
This encode-then-check-then-decode pattern maps to CWE-22, where insufficient path sanitization enables directory traversal, and more broadly to a classic "decode-after-check" / time-of-check to time-of-use discrepancy in input validation logic.
Potential impact
- An attacker may read arbitrary files accessible to the NLTK-running process.
- Sensitive configuration files (keys, tokens, database credentials) could be exposed.
- Application-level secrets and private data stores can be disclosed indirectly via NLTK-powered features.
- Local file disclosure may be chained into further compromise, such as credential reuse against other services.
General mitigation guidance
Effective mitigation patterns center on treating paths and URLs as untrusted input and normalizing them before enforcing security checks.
Common strategies include:
- Performing decoding, normalization, and canonicalization before any path validation or allow/deny decisions.
- Ensuring path checks operate on resolved filesystem paths with traversal sequences removed or strictly confined to an expected base directory.
- Restricting file access to whitelisted directories or resource identifiers, rather than relying solely on blacklist-style regex checks.
- Avoiding direct use of user-controlled path fragments and instead mapping external identifiers to internal resource names via lookup tables.
- Running NLTK-consuming components with least-privilege filesystem access to constrain the blast radius of any path traversal.
How teams detect issues like this
Organizations can identify similar traversal issues by:
- Applying static analysis rules that flag decode-after-check patterns (e.g., validation on encoded URLs followed by decoding and filesystem access).
- Reviewing calls to nltk.data.load() and other resource loaders that accept URL-like strings, focusing on paths influenced by external input.
- Enforcing secure coding policies that require path normalization and base-directory confinement before opening files.
- Using security-focused code review checklists to ask whether any path validation operates on a different representation than the one actually used.
- Employing automated application security testing tools that attempt encoded traversal payloads against endpoints using NLTK-backed resource loading.

