Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Lib/test/test_pyexpat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from xml.parsers import expat
from xml.parsers.expat import errors

from test.support import import_helper
from test.support import import_helper, infinite_recursion
from test.support import sortdict


Expand Down Expand Up @@ -648,6 +648,24 @@ def test_change_size_2(self):
parser.Parse(xml2, True)
self.assertEqual(self.n, 4)

class ElementDeclHandlerTest(unittest.TestCase):
def test_deeply_nested_content_model(self):
# This should raise a RecursionError and not crash.
# See https://github.com/python/cpython/issues/145986.
N = 500_000
data = (
b'<!DOCTYPE root [\n<!ELEMENT root '
+ b'(a, ' * N + b'a' + b')' * N
+ b'>\n]>\n<root/>\n'
)

parser = expat.ParserCreate()
parser.ElementDeclHandler = lambda _1, _2: None
with infinite_recursion():
with self.assertRaises(RecursionError):
parser.Parse(data)


class MalformedInputTest(unittest.TestCase):
def test1(self):
xml = b"\0\r\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`xml.parsers.expat`: Fixed a crash caused by unbounded C recursion when
converting deeply nested XML content models with
:meth:`~xml.parsers.expat.xmlparser.ElementDeclHandler`.
This addresses `CVE-2026-4224 <https://www.cve.org/CVERecord?id=CVE-2026-4224>`_.
8 changes: 7 additions & 1 deletion Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ static PyObject *
conv_content_model(XML_Content * const model,
PyObject *(*conv_string)(const XML_Char *))
{
if (Py_EnterRecursiveCall(" in conv_content_model")) {
return NULL;
}

PyObject *result = NULL;
PyObject *children = PyTuple_New(model->numchildren);
int i;
Expand All @@ -585,14 +589,16 @@ conv_content_model(XML_Content * const model,
conv_string);
if (child == NULL) {
Py_XDECREF(children);
return NULL;
goto done;
}
PyTuple_SET_ITEM(children, i, child);
}
result = Py_BuildValue("(iiO&N)",
model->type, model->quant,
conv_string,model->name, children);
}
done:
Py_LeaveRecursiveCall();
return result;
}

Expand Down
Loading