|
| 1 | +From f2eebf3c38eae77765247791576b437ec25ccfe2 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Serhiy Storchaka < [email protected]> |
| 3 | +Date: Sun, 11 Feb 2024 12:08:39 +0200 |
| 4 | +Subject: [PATCH] gh-115133: Fix tests for XMLPullParser with Expat 2.6.0 |
| 5 | + (GH-115164) |
| 6 | + |
| 7 | +Feeding the parser by too small chunks defers parsing to prevent |
| 8 | +CVE-2023-52425. Future versions of Expat may be more reactive. |
| 9 | +(cherry picked from commit 4a08e7b3431cd32a0daf22a33421cd3035343dc4) |
| 10 | + |
| 11 | +Co-authored-by: Serhiy Storchaka < [email protected]> |
| 12 | +--- |
| 13 | + Lib/test/test_xml_etree.py | 58 ++++++++++++------- |
| 14 | + ...-02-08-14-21-28.gh-issue-115133.ycl4ko.rst | 2 + |
| 15 | + 2 files changed, 38 insertions(+), 22 deletions(-) |
| 16 | + create mode 100644 Misc/NEWS.d/next/Library/2024-02-08-14-21-28.gh-issue-115133.ycl4ko.rst |
| 17 | + |
| 18 | +diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py |
| 19 | +index 267982a8233c92..fa03f381fac92a 100644 |
| 20 | +--- a/Lib/test/test_xml_etree.py |
| 21 | ++++ b/Lib/test/test_xml_etree.py |
| 22 | +@@ -13,6 +13,7 @@ |
| 23 | + import operator |
| 24 | + import os |
| 25 | + import pickle |
| 26 | ++import pyexpat |
| 27 | + import sys |
| 28 | + import textwrap |
| 29 | + import types |
| 30 | +@@ -120,6 +121,10 @@ |
| 31 | + </foo> |
| 32 | + """ |
| 33 | + |
| 34 | ++fails_with_expat_2_6_0 = (unittest.expectedFailure |
| 35 | ++ if pyexpat.version_info >= (2, 6, 0) else |
| 36 | ++ lambda test: test) |
| 37 | ++ |
| 38 | + def checkwarnings(*filters, quiet=False): |
| 39 | + def decorator(test): |
| 40 | + def newtest(*args, **kwargs): |
| 41 | +@@ -1400,28 +1405,37 @@ def assert_event_tags(self, parser, expected, max_events=None): |
| 42 | + self.assertEqual([(action, elem.tag) for action, elem in events], |
| 43 | + expected) |
| 44 | + |
| 45 | +- def test_simple_xml(self): |
| 46 | +- for chunk_size in (None, 1, 5): |
| 47 | +- with self.subTest(chunk_size=chunk_size): |
| 48 | +- parser = ET.XMLPullParser() |
| 49 | +- self.assert_event_tags(parser, []) |
| 50 | +- self._feed(parser, "<!-- comment -->\n", chunk_size) |
| 51 | +- self.assert_event_tags(parser, []) |
| 52 | +- self._feed(parser, |
| 53 | +- "<root>\n <element key='value'>text</element", |
| 54 | +- chunk_size) |
| 55 | +- self.assert_event_tags(parser, []) |
| 56 | +- self._feed(parser, ">\n", chunk_size) |
| 57 | +- self.assert_event_tags(parser, [('end', 'element')]) |
| 58 | +- self._feed(parser, "<element>text</element>tail\n", chunk_size) |
| 59 | +- self._feed(parser, "<empty-element/>\n", chunk_size) |
| 60 | +- self.assert_event_tags(parser, [ |
| 61 | +- ('end', 'element'), |
| 62 | +- ('end', 'empty-element'), |
| 63 | +- ]) |
| 64 | +- self._feed(parser, "</root>\n", chunk_size) |
| 65 | +- self.assert_event_tags(parser, [('end', 'root')]) |
| 66 | +- self.assertIsNone(parser.close()) |
| 67 | ++ def test_simple_xml(self, chunk_size=None): |
| 68 | ++ parser = ET.XMLPullParser() |
| 69 | ++ self.assert_event_tags(parser, []) |
| 70 | ++ self._feed(parser, "<!-- comment -->\n", chunk_size) |
| 71 | ++ self.assert_event_tags(parser, []) |
| 72 | ++ self._feed(parser, |
| 73 | ++ "<root>\n <element key='value'>text</element", |
| 74 | ++ chunk_size) |
| 75 | ++ self.assert_event_tags(parser, []) |
| 76 | ++ self._feed(parser, ">\n", chunk_size) |
| 77 | ++ self.assert_event_tags(parser, [('end', 'element')]) |
| 78 | ++ self._feed(parser, "<element>text</element>tail\n", chunk_size) |
| 79 | ++ self._feed(parser, "<empty-element/>\n", chunk_size) |
| 80 | ++ self.assert_event_tags(parser, [ |
| 81 | ++ ('end', 'element'), |
| 82 | ++ ('end', 'empty-element'), |
| 83 | ++ ]) |
| 84 | ++ self._feed(parser, "</root>\n", chunk_size) |
| 85 | ++ self.assert_event_tags(parser, [('end', 'root')]) |
| 86 | ++ self.assertIsNone(parser.close()) |
| 87 | ++ |
| 88 | ++ @fails_with_expat_2_6_0 |
| 89 | ++ def test_simple_xml_chunk_1(self): |
| 90 | ++ self.test_simple_xml(chunk_size=1) |
| 91 | ++ |
| 92 | ++ @fails_with_expat_2_6_0 |
| 93 | ++ def test_simple_xml_chunk_5(self): |
| 94 | ++ self.test_simple_xml(chunk_size=5) |
| 95 | ++ |
| 96 | ++ def test_simple_xml_chunk_22(self): |
| 97 | ++ self.test_simple_xml(chunk_size=22) |
| 98 | + |
| 99 | + def test_feed_while_iterating(self): |
| 100 | + parser = ET.XMLPullParser() |
| 101 | +diff --git a/Misc/NEWS.d/next/Library/2024-02-08-14-21-28.gh-issue-115133.ycl4ko.rst b/Misc/NEWS.d/next/Library/2024-02-08-14-21-28.gh-issue-115133.ycl4ko.rst |
| 102 | +new file mode 100644 |
| 103 | +index 00000000000000..6f1015235cc25d |
| 104 | +--- /dev/null |
| 105 | ++++ b/Misc/NEWS.d/next/Library/2024-02-08-14-21-28.gh-issue-115133.ycl4ko.rst |
| 106 | +@@ -0,0 +1,2 @@ |
| 107 | ++Fix tests for :class:`~xml.etree.ElementTree.XMLPullParser` with Expat |
| 108 | ++2.6.0. |
0 commit comments