From 0765ae4d3728db1e7d0870185d42e0025db6d2c5 Mon Sep 17 00:00:00 2001
From: Serhiy Storchaka <storchaka@gmail.com>
Date: Sat, 4 Jul 2026 20:40:22 +0300
Subject: [PATCH] gh-153030: Fix quadratic complexity in incremental parsing in
 HTMLParser (GH-153031)

When an unterminated construct (e.g. a tag or comment) spanned many
feed() calls, rescanning the growing buffer and concatenating new data
onto it were both quadratic.  New data is now accumulated in a list and
only joined and parsed once enough has piled up.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
(cherry picked from commit bcf98ddbc40ec9b3ee87da0124a5660b19b7e606)
---
 Lib/HTMLParser.py                                                        |   32 +++++++++-
 Lib/test/test_htmlparser.py                                              |   20 ++++++
 Misc/NEWS.d/next/Security/2026-07-04-17-00-00.gh-issue-153030.RovkP6.rst |    3 
 3 files changed, 53 insertions(+), 2 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Security/2026-07-04-17-00-00.gh-issue-153030.RovkP6.rst

Index: Python-2.7.18/Lib/HTMLParser.py
===================================================================
--- Python-2.7.18.orig/Lib/HTMLParser.py	2026-08-15 10:53:54.174458999 +0200
+++ Python-2.7.18/Lib/HTMLParser.py	2026-08-15 10:56:44.883292037 +0200
@@ -105,6 +105,9 @@
         self.lasttag = '???'
         self.interesting = interesting_normal
         self.cdata_elem = None
+        self._pending = []
+        self._pending_len = 0
+        self._parse_threshold = 1
         markupbase.ParserBase.reset(self)
 
     def feed(self, data):
@@ -113,11 +116,36 @@
         Call this as often as you want, with as little or as much text
         as you want (may include '\n').
         """
-        self.rawdata = self.rawdata + data
-        self.goahead(0)
+        # Accumulate new data in a list and only join and parse it once
+        # enough has piled up.  Rescanning an unparsed buffer (e.g. an
+        # unterminated tag) and concatenating onto it on every call would
+        # both be quadratic in the input size.
+        self._pending_len += len(data)
+        if self._pending_len < self._parse_threshold:
+            self._pending.append(data)
+        else:
+            if not self._pending:
+                self.rawdata += data
+            else:
+                self._pending.append(data)
+                self.rawdata += ''.join(self._pending)
+                del self._pending[:]
+            self._pending_len = 0
+            n = len(self.rawdata)
+            self.goahead(0)
+            if len(self.rawdata) < n:
+                # Some data was parsed; resume on the next call.
+                self._parse_threshold = 1
+            else:
+                # Nothing was parsed; wait until the buffer doubles.
+                self._parse_threshold = len(self.rawdata)
 
     def close(self):
         """Handle any buffered data."""
+        if self._pending:
+            self.rawdata += ''.join(self._pending)
+            del self._pending[:]
+            self._pending_len = 0
         self.goahead(1)
 
     def error(self, message):
Index: Python-2.7.18/Lib/test/test_htmlparser.py
===================================================================
--- Python-2.7.18.orig/Lib/test/test_htmlparser.py	2026-08-15 10:53:54.174834737 +0200
+++ Python-2.7.18/Lib/test/test_htmlparser.py	2026-08-15 10:57:45.612113267 +0200
@@ -664,6 +664,26 @@
         ]
         self._run_check(html, expected)
 
+    @support.requires_resource('cpu')
+    def test_incremental_no_quadratic_complexity(self):
+        # An unterminated construct fed in many small chunks used to take
+        # quadratic time, both to rescan and to concatenate the buffer.
+        # Now it takes a fraction of a second.
+        def check(prefix, chunk, suffix):
+            parser = HTMLParser.HTMLParser()
+            parser.feed(prefix)
+            for _ in xrange(200000):
+                parser.feed(chunk)
+            parser.feed(suffix)
+            parser.close()
+        chunk = "a" * 64
+        check("<!--", chunk, "-->")       # comment
+        check("<?", chunk, ">")           # processing instruction
+        check("<!doctype ", chunk, ">")   # doctype
+        check("<![CDATA[", chunk, "]]>")  # CDATA section
+        check("<a href='", chunk, "'>")   # start tag
+        check("<script>", chunk, "</script>")  # RAWTEXT element
+
 
 def test_main():
     test_support.run_unittest(HTMLParserTestCase, AttributesTestCase)
Index: Python-2.7.18/Misc/NEWS.d/next/Security/2026-07-04-17-00-00.gh-issue-153030.RovkP6.rst
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ Python-2.7.18/Misc/NEWS.d/next/Security/2026-07-04-17-00-00.gh-issue-153030.RovkP6.rst	2026-08-15 10:56:21.605206211 +0200
@@ -0,0 +1,3 @@
+Fixed quadratic complexity in incremental parsing of long unterminated
+constructs (such as tags or comments) in :class:`html.parser.HTMLParser`,
+which could be exploited for a denial of service.
