NLTK: Missing Post-Download Integrity Verification Allows Malicious Package Injection
What fixes it
A fixed version on one release line is not the upgrade target for every line. Check your exact version and any vendor backports.
Description
NLTK's package downloader in nltk/downloader.py does not verify file integrity after download and before extraction.
The download flow at lines 789-825:
- File is downloaded to a temp path via HTTP
- os.replace(tmp_filepath, filepath) moves it to the final location (line 799)
- Extraction begins via _unzip_iter() (line 825)
Between steps 2 and 3, there is no SHA-256 verification. The checksum logic exists in _pkg_status() (lines 982-1015) but it is only used BEFORE download as a status check ("is this package already installed and up-to-date?"). It is never called after download to verify the file that was actually received.
Attack vectors:
- MITM during HTTP download (NLTK downloads from http:// by default on some mirrors)
- Race condition on shared filesystems (attacker replaces file between os.replace and _unzip_iter)
- DNS poisoning redirecting to attacker-controlled server
PoC:
import nltk
import unittest.mock
import zipfile
import io
import os
# Create a malicious zip that will be "downloaded"
malicious_zip = io.BytesIO()
with zipfile.ZipFile(malicious_zip, 'w') as zf:
zf.writestr('punkt_tab/tokenizers/punkt_tab/english.pickle',
b'MALICIOUS PAYLOAD - attacker controlled content')
# Patch urllib to return our malicious zip
with unittest.mock.patch('urllib.request.urlopen') as mock_urlopen:
mock_response = unittest.mock.MagicMock()
mock_response.read.return_value = malicious_zip.getvalue()
mock_response.headers = {'Content-Length': str(len(malicious_zip.getvalue()))}
mock_urlopen.return_value = mock_response
# Download proceeds, no integrity check catches the swap
# nltk.download('punkt_tab') # Would install attacker payloadThis is distinct from CVE-2024-39705 (pickle deserialization via download) and CVE-2025-14009 (zip-slip path traversal). Those address what happens AFTER extraction. This finding addresses the gap BEFORE extraction where integrity is never verified.
Suggested fix: After os.replace() and before _unzip_iter(), compute SHA-256 of the final file and compare against the expected checksum from the package index. Reject and delete the file if the hash does not match.
Text from GitHub Advisory Database
Affected versions 53
- nltk, pip<= 3.9.2GitHub
- nltk/nltkaffected: unspecified through latestCVE List
- nltk, PyPIfrom first release, fixed in 3.9.3OSV
- nltk, PyPI0.8OSV
- nltk, PyPI0.9OSV
- nltk, PyPI0.9.3OSV
- nltk, PyPI0.9.4OSV
- nltk, PyPI0.9.5OSV
- nltk, PyPI0.9.6OSV
- nltk, PyPI0.9.7OSV
- nltk, PyPI0.9.8OSV
- nltk, PyPI0.9.9OSV
- nltk, PyPI2.0.1OSV
- nltk, PyPI2.0.1rc1OSV
- nltk, PyPI2.0.1rc2-gitOSV
- nltk, PyPI2.0.1rc3OSV
- nltk, PyPI2.0.1rc4OSV
- nltk, PyPI2.0.2OSV
- nltk, PyPI2.0.3OSV
- nltk, PyPI2.0.4OSV
- nltk, PyPI2.0.5OSV
- nltk, PyPI2.0b4OSV
- nltk, PyPI2.0b5OSV
- nltk, PyPI2.0b6OSV
- nltk, PyPI2.0b7OSV
- nltk, PyPI2.0b8OSV
- nltk, PyPI2.0b9OSV
- nltk, PyPI3.0.0OSV
- nltk, PyPI3.0.0b1OSV
- nltk, PyPI3.0.0b2OSV
- nltk, PyPI3.0.1OSV
- nltk, PyPI3.0.2OSV
- nltk, PyPI3.0.3OSV
- nltk, PyPI3.0.4OSV
- nltk, PyPI3.0.5OSV
- nltk, PyPI3.1OSV
- nltk, PyPI3.2OSV
- nltk, PyPI3.2.1OSV
- nltk, PyPI3.2.2OSV
- nltk, PyPI3.2.3OSV
- nltk, PyPI3.2.4OSV
- nltk, PyPI3.2.5OSV
- nltk, PyPI3.3OSV
- nltk, PyPI3.4OSV
- nltk, PyPI3.4.1OSV
- nltk, PyPI3.4.2OSV
- nltk, PyPI3.4.3OSV
- nltk, PyPI3.4.4OSV
- nltk, PyPI3.4.5OSV
- nltk, PyPI3.5OSV
- nltk, PyPI3.5b1OSV
- nltk, PyPI3.6OSV
- nltk, PyPI3.6.1OSV
References 9
- github.com/nltk/nltk/commit/0e26734a61094b628d93e26dc18dd7302567ac46Fixvia GitHub
- github.com/nltk/nltk/releases/tag/3.9.3Fixvia GitHub
- github.com/nltk/nltk/security/advisories/GHSA-5wp5-5229-5g6qAdvisoryvia GitHub
- github.com/pypa/advisory-database/tree/main/vulns/nltk/PYSEC-2026-3729.yamlAdvisoryvia GitHub
- vulncheck.com/advisories/nltk-before-missing-post-download-integrity-verificationAdvisoryvia GitHub
- github.com/advisories/GHSA-5wp5-5229-5g6qAdvisoryvia GitHub
- nvd.nist.gov/vuln/detail/CVE-2026-12259Referencevia GitHub
- github.com/nltk/nltk/pull/3449Referencevia GitHub
- huntr.com/bounties/659ccf6d-12d4-4d4a-84c0-078633c35a5dReferencevia CVE List