Executive brief
NLTK's tgrep module is a natural language processing tool that searches parse trees using pattern matching syntax. The module accepts user-supplied regular expressions without validation or timeouts, allowing an attacker to craft a malicious regex pattern that causes exponential backtracking (ReDoS). A single crafted request can hang the entire Python process indefinitely, denying service to all other users sharing the process.
Technical details
The vulnerability exists in nltk/tgrep.py in the _tgrep_node_action() function (around line 320), which compiles embedded regex literals directly from user input without validation. When a tgrep pattern contains a /regex/ node, the function calls re.compile(node_lit) directly and applies the compiled regex via r.search() against tree node labels. The attacker controls node_lit entirely through tgrep_positions() or tgrep_compile() entry points. Vulnerable to ReDoS through patterns like /((a+)+)b/ matching a string of repeated 'a' characters with no 'b' present, causing exponential backtracking. No authentication is required; the attack vector is network-accessible in web API deployments (Flask, FastAPI). Patch available in version 3.10.3; affected versions are <= 3.10.2.
Affected products
- NLTK NLTK <= 3.10.2
Timeline
- 2026-09-08: disclosed: GitHub Advisory published
- 2026: patched: Patched in version 3.10.3