GitHub advisory · PyPI
NLTK: XMLCorpusView comment and CDATA parsing permits quadratic CPU denial of service
Description
affected: PyPI
nltk>= 3.6—nltk.corpus.reader.xmldocs.XMLCorpusViewstreaming read (and thenps_chat/framenetreaders built on it) dynamically reproduced:3.6(6/6 checks),3.9.2(6/6),3.10.3(6/6) anddevelop6818767b9(6/6)
Source references (nltk/corpus/reader/xmldocs.py:NNN) are to commit 6818767b99c24659107eb2be9bccd32a3d4bf045 (develop, v3.10.3-15-g6818767b9). The released v3.10.3 wheel's xmldocs.py is byte-identical to that commit, so the same line numbers apply to the released wheel.
An attacker who can make a victim read a well-formed XML corpus file whose body contains one large comment or CDATA section can pin a CPU core for a time quadratic in the file size, because XMLCorpusView._read_xml_fragment (nltk/corpus/reader/xmldocs.py) re-runs a linear whole-buffer validation after every 1 KiB block while the comment/CDATA terminator is still ahead. XMLCorpusView is NLTK's documented streaming API for XML corpora and is instantiated directly by the NPSChatCorpusReader and FramenetCorpusReader readers; the module header (xmldocs.py:16-20) explicitly handles untrusted corpus XML. The in_tag guard added to this function for GHSA-vp2x covers only a single oversized <...> tag and leaves the CDATA/comment path quadratic in the current release.
Root cause: _read_xml_fragment (xmldocs.py:273-338) accumulates the file into one fragment string in _BLOCK_SIZE (1024) character chunks (xmldocs.py:123, 298-299). After every block that is not yet a well-formed prefix, the fragment is re-validated against _VALID_XML_RE. Two whole-buffer operations dominate:
# xmldocs.py:308
if not in_tag and self._VALID_XML_RE.match(fragment): # whole-buffer match (O(len))
return fragment
# xmldocs.py:312
if re.search("[<>]", fragment).group(0) == ">": # O(1) for this input class:
... # the…
Manual fix · PyPI
pip install --upgrade "nltk>=3.10.4"Sources