GitHub advisory · PyPI
NLTK: Uncontrolled recursion in semantic-logic type parser causes denial of service via nested type strings
Description
The semantic-logic type parser read_type (nltk/sem/logic.py:866) recurses on both
halves of every <a,b> complex type (nltk/sem/logic.py:882-884) with no depth or length
bound — only an isinstance assertion and a parenthesis-balance assertion. The
LogicParser.MAX_PARSE_DEPTH guard (nltk/sem/logic.py:107,294) protects the expression
parser but never runs on the read_type path, which is reached directly by the public
Type.fromstring classmethod and by signature-string type checking.
An application that parses an untrusted type string, or type-checks an expression against
an attacker-influenced signature, aborts with an unhandled RecursionError, terminating
the affected operation or request worker (denial of service, CWE-674). The recursion is
pure Python, yielding a catchable RecursionError rather than memory corruption or code
execution. Confidentiality and integrity are unaffected. Exposure is narrower than a
general-purpose parser because the input must reach a type-string or signature parameter.
nltk.sem.logic.Type.fromstring(s) (nltk/sem/logic.py:736) — or
Expression.typecheck(signature) / Expression.fromstring(expr, type_check=True, signature=...), which reach read_type for each type-string signature value
(nltk/sem/logic.py:1080) — with a deeply nested type string such as
"<" * n + "e,e" + ">" * n.Type.fromstring passes the string straight to read_type with no length or depth
check (only assertions on type and parenthesis balance).read_type recurses on both halves of each <a,b> level, consuming one stack frame per
nesting level; the MAX_PARSE_DEPTH guard on the separate LogicParser class does not
execute here.RecursionError
propagates out of the public API.Running the shipped read_type/ComplexType/Type source…
Manual fix · PyPI
pip install --upgrade "nltk>=3.10.4"Sources