Summary
NLTK before version 3.10.0 contains a path traversal issue in StreamBackedCorpusView that can allow arbitrary local file reads.
If an attacker controls the fileid argument, the code may read files outside the intended corpus location, including sensitive system files or application credentials, even when pathsec.ENFORCE is enabled.
Affected context
The vulnerability occurs in NLTK's StreamBackedCorpusView when a caller-controlled fileid is used to access corpus data. The risk is present where untrusted input can influence that identifier and the implementation uses builtins.open() directly rather than the security-aware pathsec.open() enforcement path. Exploitation depends on the attacker being able to reach the affected corpus-reading functionality and influence the file selection value.
Exploit preconditions
- Attack surface: An application or service that uses NLTK's StreamBackedCorpusView
- Required access: Ability to supply or influence the fileid argument
- Trigger point: Opening a corpus resource through the affected file-reading path
- Why exploitation is possible: Direct use of builtins.open() bypasses pathsec.ENFORCE and permits path values outside the intended corpus directory
Why this happens
The underlying bug is improper restriction of filesystem paths, mapping to CWE-22: Improper Limitation of a Pathname to a Restricted Directory.
The implementation relies on a security control in pathsec.open(), but a separate code path calls builtins.open() directly. As a result, the configured path enforcement is not consistently applied before a caller-controlled filename is opened. This creates a mismatch between the intended trust boundary and the actual filesystem access behavior.
Potential impact
- An attacker could read arbitrary files accessible to the NLTK process.
- Sensitive system configuration or application credentials may be exposed.
- Authentication tokens, keys, or environment-specific secrets could be disclosed.
- Information from readable files could support further compromise.
- Confidential training data or locally stored application data may be retrieved.
General mitigation guidance
Mitigation generally focuses on ensuring that every filesystem access uses a consistently enforced, security-aware path handling mechanism.
Common defensive patterns include:
- Route file access through approved path-validation and opening APIs.
- Resolve and validate paths against an explicit allowed base directory.
- Treat file identifiers and corpus names as untrusted input.
- Prefer allowlisted resource identifiers over user-provided filesystem paths.
- Apply least-privilege permissions to the process and its readable directories.
- Avoid storing credentials or other sensitive material in locations readable by the application.
- Add regression tests covering traversal sequences, absolute paths, symbolic links, and enforcement-disabled configurations.
How teams detect issues like this
Organizations can detect related issues through:
- Static analysis rules that identify direct filesystem opens using caller-controlled paths.
- Code review checks for inconsistent use of path-validation wrappers and raw file APIs.
- Tests that verify all corpus resource access remains within an approved directory.
- Runtime monitoring for unexpected reads of system files, credential stores, or paths outside application data directories.
- Policy enforcement requiring centralized filesystem access functions for untrusted filenames.
Automated code review and software composition analysis tools may help identify unsafe path handling, but findings should be validated against the application's actual input and permission boundaries.

