Summary
A path traversal flaw in the NLTK downloader allows remote XML index servers to inject directory traversal sequences into the subdir and id attributes without validation.
When a client uses a malicious or compromised XML index, the downloader may create or overwrite arbitrary files and directories on the system, enabling data loss, configuration tampering, or limited code execution depending on what paths are targeted.
Affected context
This vulnerability affects applications and users that invoke the NLTK downloader to fetch corpora, models, or other resources from XML-based indexes.
It is relevant when:
- The downloader is configured to use remote or custom XML index servers, or
- An attacker can influence which XML index URL is loaded.
The flaw occurs in the client-side processing of subdir and id attributes from the XML index, which are used to determine local filesystem paths without sufficient normalization or validation.
Exploit preconditions
- Attack surface: NLTK downloader client running on a user or application machine.
- Required access: Ability to control, spoof, or redirect the downloader to a malicious XML index server (e.g., via configuration, environment, DNS, or MITM in some scenarios).
- Trigger point: Execution of
nltk.download()or any workflow that consumes the remote XML index and processessubdir/idattributes. - Why exploitation is possible: The downloader trusts path fragments from the XML index and concatenates them directly into local filesystem paths, allowing
../sequences or absolute path-like values to break out of the intended download directory.
Why this happens
The core issue is unvalidated use of attacker-controlled path components from an XML index to construct local file system paths.
The downloader logic interprets subdir and id values as relative directory/file names and uses them directly, failing to normalize, canonicalize, or constrain them to an expected base directory.
This aligns with CWE-22: Improper Limitation of a Pathname to a Restricted Directory, where path traversal sequences enable access to or modification of files outside the intended download root.
Potential impact
- Arbitrary directory creation anywhere the downloader process has write permission.
- Arbitrary file creation with attacker-controlled contents.
- Overwrite of existing files, potentially including configuration files, logs, or application resources.
- Disruption or corruption of NLTK data packages and NLP models, affecting application behavior or research results.
- Under certain configurations, potential facilitation of limited code execution by overwriting scripts, configuration, or data that is later executed or interpreted.
General mitigation guidance
Mitigation focuses on strictly constraining how remote-controlled paths are interpreted and reducing trust in external indexes.
Pattern-oriented defensive measures include:
- Normalizing and canonicalizing paths before use, rejecting any that escape the intended download root or contain traversal sequences (
.., drive changes, absolute paths). - Enforcing a fixed, application-controlled base directory for downloads and ensuring all resolved paths remain inside that directory.
- Treating all index-derived path fragments as untrusted input and applying allowlists for allowed characters and directory structures.
- Running downloader processes with least-privilege accounts and limited filesystem permissions, so even if a path issue exists, the blast radius is reduced.
- Favoring official, authenticated index sources and avoiding dynamically configured or user-supplied index URLs in production workflows.
How teams detect issues like this
Detection can combine code-focused and operational techniques:
- Static analysis: Rules that flag concatenation of untrusted data (from XML attributes or network sources) into filesystem paths without validation or canonicalization.
- Code review: Look for path-building logic that uses remote index attributes (
subdir,id, or similar) directly in directory or filename construction; verify that checks enforce containment within a designated base directory. - Runtime monitoring: File integrity monitoring or audit logging to detect unexpected writes outside the normal NLTK data directories during downloader runs.
- Configuration review: Inventory and review custom index URLs or mirrors used by NLTK in CI, training pipelines, or production systems to ensure they are trusted and well-controlled.

