GitHub advisory · PyPI
NLTK: Uncontrolled recursion in CCG lexicon parser causes denial of service via deeply nested category input
Description
The CCG lexicon parser recurses on nested parenthesised categories without any depth
bound. matchBrackets (nltk/ccg/lexicon.py:154) calls itself once per nested (, and
augParseCategory (nltk/ccg/lexicon.py:228,242) recurses once per bracketed
sub-category. Neither function tracks recursion depth nor checks
sys.getrecursionlimit(), and the public entry point fromstring
(nltk/ccg/lexicon.py:252, also re-exported via nltk.ccg.chart.fromstring) applies no
length or nesting cap before handing the text to the parser.
An application that parses an untrusted lexicon string aborts with an unhandled
RecursionError, terminating the affected parse operation or request worker
(denial of service, CWE-674). The recursion is pure Python, so CPython raises a catchable
RecursionError rather than crashing the process; there is no memory-corruption or
code-execution primitive. Confidentiality and integrity are unaffected. Plausibly, a
service that does not wrap the parse in a handler will surface the error to the caller and
drop the in-flight request.
nltk.ccg.lexicon.fromstring(lex_str) with attacker-supplied
text such as ":- S\nw => " + "(" * 2000 + "S" + ")" * 2000 + "\n".fromstring splits the input into lines; LEX_RE/RHS_RE
(nltk/ccg/lexicon.py:275-276) match and route the parenthesised category into
augParseCategory (nltk/ccg/lexicon.py:277) with no depth or length guard.(, nextCategory dispatches to matchBrackets,
which recurses once per nested (; augParseCategory recurses once per bracketed
sub-category. Each nesting level consumes call-stack frames.RecursionError
propagates out of the public API and aborts the parse.Running the dependency-free matchBrackets body verbatim from the shipped source at the
default…
Manual fix · PyPI
pip install --upgrade "nltk>=3.10.4"Sources