[3.11] gh-146333: Fix quadratic regex backtracking in configparser option parsing (GH-146399) (GH-148559) (GH-154081) by miss-islington · Pull Request #155160 · python/cpython · GitHub
Skip to content
Closed
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
8 changes: 6 additions & 2 deletions Lib/configparser.py
20 changes: 20 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,26 @@ def test_instance_assignment(self):
self.assertEqual(cfg['two'].getlen('one'), 5)


class ReDoSTestCase(unittest.TestCase):
"""Regression tests for quadratic regex backtracking (gh-146333)."""

def test_option_regex_does_not_backtrack(self):
# A line with many spaces between non-delimiter characters
# should be parsed in linear time, not quadratic.
parser = configparser.RawConfigParser()
content = "[section]\n" + "x" + " " * 40000 + "y" + "\n"
# This should complete almost instantly. Before the fix,
# it would take over a minute due to catastrophic backtracking.
with self.assertRaises(configparser.ParsingError):
parser.read_string(content)

def test_option_regex_no_value_does_not_backtrack(self):
parser = configparser.RawConfigParser(allow_no_value=True)
content = "[section]\n" + "x" + " " * 40000 + "y" + "\n"
parser.read_string(content)
self.assertTrue(parser.has_option("section", "x" + " " * 40000 + "y"))


class MiscTestCase(unittest.TestCase):
def test__all__(self):
support.check__all__(self, configparser, not_exported={"Error"})
Expand Down
Loading