NLTK NKJPCorpusReader Path Traversal Vulnerability
A path-traversal vulnerability in NLTK's NKJPCorpusReader allows attackers to read arbitrary files by bypassing the nltk.pathsec security sandbox.
The Natural Language Toolkit (NLTK) version 3.9.4 and earlier contains a critical path-traversal vulnerability (CVE-2026-12072) within the NKJPCorpusReader class. The component fails to validate or sanitize the fileids parameter used in its corpus reading methods (e.g., header, raw, words). Instead of utilizing the nltk.pathsec security framework, which is designed to prevent directory traversal and enforce strict access control, NKJPCorpusReader relies on insecure string concatenation to build file paths and subsequently invokes Python's built-in open() function.
This architectural flaw persists even when nltk.pathsec.ENFORCE = True is set, a configuration specifically intended to protect multi-tenant and web-based applications from unauthorized file system access. An attacker capable of influencing the input to these methods can escape the intended corpus directory, leading to unauthorized disclosure of sensitive files on the host system. The vulnerability affects a wide range of NLTK corpus readers that share the same insecure pattern of using raw file operations over the provided security API.
Attack Chain
- Attacker identifies an application endpoint or script that accepts user-supplied input used as a
fileidsargument for NLTK'sNKJPCorpusReader. - Attacker crafts a malicious path string containing directory traversal sequences (e.g.,
../../../../) to escape the configured corpus root directory. - The application passes the malicious string into the
NKJPCorpusReader.header()or related corpus reading method. NKJPCorpusReaderperforms plain string concatenation of the corpus root and the attacker-controlledfileidswithout normalization or security validation.- The resulting path points to a sensitive file on the host filesystem (e.g.,
/etc/passwdor configuration files). - The
NKJPCorpusReadermethod calls the Python built-inopen()function directly with the resolved malicious path. - The
nltk.pathsecsandbox is never consulted, bypassing theENFORCE=Truesecurity configuration. - The application returns the content of the unintended file to the attacker, resulting in unauthorized data exfiltration.
Impact
Successful exploitation allows for the arbitrary reading of files on the host operating system with the privileges of the service account running the NLTK-based application. This poses a significant risk to web and multi-tenant environments where NLTK is utilized to process untrusted user input. Exposure of local configuration files, API keys, or system credentials could lead to full system compromise. The vulnerability affects any software utilizing NLTK <= 3.9.4 for corpus analysis.
Recommendation
- Update NLTK to the latest version once a patch is released that addresses the
NKJPCorpusReaderpath validation logic. - Implement strict input validation for any user-controlled data intended to act as a file path or identifier within NLTK readers.
- Audit application code for any custom use of
os.path.joinor string concatenation when constructing file paths passed to NLTK corpus objects, ensuring thatFileSystemPathPointer.open()is used instead of the built-inopen(). - Restrict the privileges of the system user running the NLTK-dependent application to the minimum necessary directory access using OS-level controls (e.g., AppArmor, SELinux, or container volume restrictions).