Résumé
NLTK corpus-reader view methods bypass the scoped root sandbox via intermediate-directory symlinks
Détails de l’avis
affected: PyPI
nltk>= 3.9.4—CorpusReader.abspaths()/abspath()and thePlaintextCorpusReader/SyntaxCorpusReaderview methods dynamically reproduced:nltk3.9.4(8/9, scoped-escape checks),3.10.3(wheel, 9/9 checks) andmaster6818767b9(9/9 checks)
Source references (file:line) are to commit 303f6e2b (release tag v3.10.3). Line numbers in the released nltk wheel match this tag exactly; master 6818767b9 contains the same code (pathsec.py renumbered by +44 lines from added hardening comments). In 3.9.4 the same logic exists at earlier line numbers (api.py:222-236 for open(), api.py:161-203 for abspath()/abspaths(), data.py:382-388 for the pointer open, pathsec.py:105-114 for the scoped branch); the affected code is identical.
Summary
CorpusReader.open() (nltk/corpus/reader/api.py:235-257) is NLTK's hardened file-access entry point: after a lexical guard it calls validate_path(..., required_root=self._root) (api.py:246-249), and the scoped branch of pathsec.validate_path (nltk/pathsec.py:168-179) resolves symlinks and raises ValueError when the resolved target is not inside the corpus root. The view methods do not use this entry point: PlaintextCorpusReader.words/sents/paras (nltk/corpus/reader/plaintext.py:67-118) and SyntaxCorpusReader.parsed_sents/tagged_sents/sents/tagged_words/words (api.py:473-519) build their streams from self.abspaths(fileids, ...) (api.py:174-217), which only does a lexical self._root.join(fileid) (nltk/data.py:452-469) and returns a FileSystemPathPointer. That pointer is opened via FileSystemPathPointer.open() → pathsec.open(raw_path, "rb") with required_root left at its default None (data.py:438-447, nltk/pathsec.py:752,783), so only the global allowed-roots check runs. The O_NOFOLLOW in _hardened_open (pathsec.py:676-731) protects only the final path component, so an intermediate-directory symlink inside the corpus root pointing at a sibling allowed root is followed by the kernel. A caller who places such a symlink (or loads an untrusted corpus containing one) can read any file that open() would refuse — files outside the corpus root yet inside the global NLTK data sandbox — via PlaintextCorpusReader.words([...]) / SyntaxCorpusReader.words([...]) (and the abspaths()-returned pointer), defeating the scoped escape protection that GHSA-p4rw-class fixes and test_required_root_blocks_symlink_escape were added for.
Details
Root cause: two sibling file-access channels on the same CorpusReader disagree about the scoped sandbox.
CorpusReader.open()(api.py:235-257) — the guarded channel:- Layer 1 lexical guard (
api.py:240-242) rejects absolute paths and... - Layer 2 scoped guard (
api.py:246-249) callspathsec.validate_path(path, context="CorpusReader", required_root=self._root). - The
required_rootbranch (pathsec.py:168-179) resolves the target withPath(raw).resolve()and raisesValueErrorwhentarget.is_relative_to(scoped_root)is false.
- Layer 1 lexical guard (
The view-method channel —
abspaths()→FileSystemPathPointer→pathsec.open(required_root=None):CorpusReader.abspath()(api.py:174-183) andabspaths()(api.py:185-217) returnself._root.join(f)with novalidate_pathcall.FileSystemPathPointer.join()(data.py:452-469) only normalizes lexically (normpath+ root-prefixstartswith); it never callsos.path.realpath/Path.resolve, so the intermediate symlink is preserved verbatim.FileSystemPathPointer.open()(data.py:438-447) calls_secure_open(self._path, "rb")(aliased frompathsec.open) with norequired_root, sopathsec.open(pathsec.py:752) uses its defaultrequired_root=Noneandvalidate_path(pathsec.py:783) runs only the global allowed-roots check (pathsec.py:181-207).- Under
ENFORCE=True+ POSIX,_hardened_open(pathsec.py:676-731) opens withO_NOFOLLOW(pathsec.py:705) — but that flag only refuses a symlink in the final component; an intermediate directory symlink (corpus/sub -> <other allowed root>) is followed by the kernel. The post-open fd realpath re-validation (pathsec.py:728-731) again usesrequired_root=None, and since the target is inside an allowed root it passes.
The view methods hand the unguarded pointers straight to the stream views:
PlaintextCorpusReader.words/sents/paras(plaintext.py:67-118, abspaths at76/96/116) andchapters(plaintext.py:230).SyntaxCorpusReader.parsed_sents/tagged_sents/sents/tagged_words/words(api.py:473-519, abspaths at478/489/498/509/517).StreamBackedCorpusView._open()(nltk/corpus/reader/util.py:203-216) callsself._fileid.open(self._encoding)on thePathPointer(util.py:210), landing in therequired_root=Nonepath above.
Net effect (observed): the identical fileid "sub/secret.txt" is rejected by reader.open() with ValueError: Security Violation [CorpusReader]: Path ... escapes root ... but successfully read by reader.words(["sub/secret.txt"]), returning the out-of-root file's content.
The abspaths()/view-method bypass contradicts the contract stated in open()'s docstring (api.py:238, "Security patched: prevents path traversal and scoped escapes") and the class-level promise that every fileid stays within the corpus root (api.py:26-30). Existing regression tests cover only the open()/validate_path(required_root=...) path (test_corpus_reader.py:31, test_corpus_reader_pathsec.py:39-49), not the view-method channel.
PoC
A self-contained, self-checking repro script is embedded at the end of this advisory. It builds a private temp sandbox as an allowed NLTK data root, creates corpus root R, sibling bar (inside the same allowed root), file bar/secret.txt = TOPSECRET, and an intermediate directory symlink R/sub -> bar. It then asserts the control (same fileid through open() is refused) and the attack (abspaths() pointer, PlaintextCorpusReader.words, SyntaxCorpusReader.words/sents read the secret), plus a boundary check that a symlink pointing outside every allowed root is still refused.
Scope note: dynamically run on (1) the released nltk 3.9.4 wheel, (2) the released nltk 3.10.3 wheel, and (3) master 6818767b9 (v3.10.3-15-g6818767b9). The scoped-escape checks pass on all three; the "global sandbox still blocks outside-allowed-root read" boundary check passes on 3.10.3 and master, and is expected to differ on 3.9.4: 3.9.4 ships pathsec.ENFORCE = False (enforcement became the default only in 3.10.0) and its pathsec.open is a thin builtins.open wrapper with no _hardened_open/O_NOFOLLOW, so on 3.9.4 the sandbox does not confine reads to the allowed roots at all — the escape is broader there, not narrower. The required_root scoped guard (1b0e519e2, "Implement scoped root validation in pathsec sentinel") is present in 3.9.4 and every later release (v3.10.0/3.10.1/3.10.2/3.10.3 confirmed by tag inspection), and static comparison shows the identical abspaths()/view-method code path in all of them; 3.9.4 and 3.10.3 were the versions dynamically run.
Environment / steps (Python 3.10.12, Linux, pathsec.ENFORCE=True, POSIX):
SANDBOX=$(mktemp -d);nltk.data.path.append(SANDBOX)— the private dir becomes an allowed NLTK data root.R=$SANDBOX/corpora/mysample,BAR=$SANDBOX/bar; writeBAR/secret.txt=TOPSECRET.ln -s "$BAR" "$R/sub"— intermediate directory symlink inside the corpus root.- Control:
reader.open("sub/secret.txt")→ValueError: Security Violation [CorpusReader]: Path <BAR>/secret.txt escapes root <R>(refused). - Attack:
reader.abspaths(["sub/secret.txt"])[0].open().read()→b'TOPSECRET\n';list(reader.words(["sub/secret.txt"]))→['TOPSECRET']; same forBracketParseCorpusReader.words/sents. - Boundary: `R/out ->
Références
- https://github.com/advisories/GHSA-g58v-j9g9-5gfg
- https://github.com/nltk/nltk/security/advisories/GHSA-g58v-j9g9-5gfg
- https://nvd.nist.gov/vuln/detail/CVE-2026-128137
- https://github.com/nltk/nltk/pull/3761
- https://github.com/nltk/nltk/commit/9f7cf06b4af4ca5c65381ed0abc17e989dc54400
- https://github.com/nltk/nltk/releases/tag/v3.10.4
Vulnérabilités liées
Tout Supply chain →- MEDIUMCVE-2026-35376
chcon: recursive relabel not anchored to traversal dirfd (TOCTOU)
- MEDIUMCVE-2026-35374
split: TOCTOU output-open allows truncating a swapped file
- HIGHCVE-2026-128135
NLTK: ZIP package extraction follows in-root hardlinks and overwrites files outside the NLTK data root
- MEDIUMCVE-2026-121717
fzf: Predictable FIFO path in zsh completion allows local symlink attack / arbitrary file overwrite as the victim user (_fzf_feed_fifo)
- HIGHCVE-2026-62336
qbee-agent: Local Privilege Escalation and Arbitrary File Tampering via Symlink Following in createFile helper
- MEDIUMCVE-2026-126302
NLTK: Symlink escape in chat80.sql_query bypasses the pathsec data-root sandbox to read out-of-root SQLite databases