8000 Fix test_elementtree with Expat 2.6.0 (GH-407) · lxml/lxml@373ac41 · GitHub
[go: up one dir, main page]

Skip to content

Commit 373ac41

Browse files
hroncokserhiy-storchaka
authored andcommitted
Fix test_elementtree with Expat 2.6.0 (GH-407)
Feeding the parser by too small chunks defers parsing to prevent CVE-2023-52425. Future versions of Expat may be more reactive. Heavily inspired by python/cpython@4a08e7b We cannot use a @fails_with_expat_2_6_0 decorator because the test passes in ETreePullTestCase. See python/cpython#115133 See GHSA-gh68-jm46-84rf Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 8dbad00 commit 373ac41

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

src/lxml/tests/test_elementtree.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io
1515
import operator
1616
import os
17+
import pyexpat
1718
import re
1819
import sys
1920
import textwrap
@@ -4396,29 +4397,44 @@ def assert_event_tags(self, parser, expected, max_events=None):
43964397
self.assertEqual([(action, elem.tag) for action, elem in events],
43974398
expected)
43984399

4399-
def test_simple_xml(self):
4400-
for chunk_size in (None, 1, 5):
4401-
#with self.subTest(chunk_size=chunk_size):
4402-
parser = self.etree.XMLPullParser()
4403-
self.assert_event_tags(parser, [])
4404-
self._feed(parser, "<!-- comment -->\n", chunk_size)
4405-
self.assert_event_tags(parser, [])
4406-
self._feed(parser,
4407-
"<root>\n <element key='value'>text</element",
4408-
chunk_size)
4409-
self.assert_event_tags(parser, [])
4410-
self._feed(parser, ">\n", chunk_size)
4411-
self.assert_event_tags(parser, [('end', 'element')])
4412-
self._feed(parser, "<element>text</element>tail\n", chunk_size)
4413-
self._feed(parser, "<empty-element/>\n", chunk_size)
4414-
self.assert_event_tags(parser, [
4415-
('end', 'element'),
4416-
('end', 'empty-element'),
4417-
])
4418-
self._feed(parser, "</root>\n", chunk_size)
4419-
self.assert_event_tags(parser, [('end', 'root')])
4420-
root = self._close_and_return_root(parser)
4421-
self.assertEqual(root.tag, 'root')
4400+
def test_simple_xml(self, chunk_size=None):
4401+
parser = self.etree.XMLPullParser()
4402+
self.assert_event_tags(parser, [])
4403+
self._feed(parser, "<!-- comment -->\n", chunk_size)
4404+
self.assert_event_tags(parser, [])
4405+
self._feed(parser,
4406+
"<root>\n <element key='value'>text</element",
4407+
chunk_size)
4408+
self.assert_event_tags(parser, [])
4409+
self._feed(parser, ">\n", chunk_size)
4410+
self.assert_event_tags(parser, [('end', 'element')])
4411+
self._feed(parser, "<element>text</element>tail\n", chunk_size)
4412+
self._feed(parser, "<empty-element/>\n", chunk_size)
4413+
self.assert_event_tags(parser, [
4414+
('end', 'element'),
4415+
('end', 'empty-element'),
4416+
])
4417+
self._feed(parser, "</root>\n", chunk_size)
4418+
self.assert_event_tags(parser, [('end', 'root')])
4419+
root = self._close_and_return_root(parser)
4420+
self.assertEqual(root.tag, 'root')
4421+
4422+
def test_simple_xml_chunk_1(self):
4423+
if self.etree is not etree and pyexpat.version_info >= (2, 6, 0):
4424+
raise unittest.SkipTest(
4425+
"Feeding the parser by too small chunks defers parsing"
4426+
)
4427+
self.test_simple_xml(chunk_size=1)
4428+
4429+
def test_simple_xml_chunk_5(self):
4430+
if self.etree is not etree and pyexpat.version_info >= (2, 6, 0):
4431+
raise unittest.SkipTest(
4432+
"Feeding the parser by too small chunks defers parsing"
4433+
)
4434+
self.test_simple_xml(chunk_size=5)
4435+
4436+
def test_simple_xml_chunk_22(self):
4437+
self.test_simple_xml(chunk_size=22)
44224438

44234439
def test_feed_while_iterating(self):
44244440
parser = self.etree.XMLPullParser()

0 commit comments

Comments
 (0)
0