Commit 7213df7b authored by Abhilash Raj's avatar Abhilash Raj Committed by Barry Warsaw

bpo-29412: Fix indexError when parsing a header value ending unexpectedly (GH-14387)

* patched string index out of range error in get_word function of _header_value_parser.py and created tests in test__header_value_parser.py for CFWS.
* Raise HeaderParseError instead of continuing when parsing a word.
parent 2a7d596f
...@@ -1360,6 +1360,9 @@ def get_word(value): ...@@ -1360,6 +1360,9 @@ def get_word(value):
leader, value = get_cfws(value) leader, value = get_cfws(value)
else: else:
leader = None leader = None
if not value:
raise errors.HeaderParseError(
"Expected 'atom' or 'quoted-string' but found nothing.")
if value[0]=='"': if value[0]=='"':
token, value = get_quoted_string(value) token, value = get_quoted_string(value)
elif value[0] in SPECIALS: elif value[0] in SPECIALS:
......
...@@ -930,6 +930,12 @@ class TestParser(TestParserMixin, TestEmailBase): ...@@ -930,6 +930,12 @@ class TestParser(TestParserMixin, TestEmailBase):
self.assertEqual(word.token_type, 'atom') self.assertEqual(word.token_type, 'atom')
self.assertEqual(word[0].token_type, 'cfws') self.assertEqual(word[0].token_type, 'cfws')
def test_get_word_all_CFWS(self):
# bpo-29412: Test that we don't raise IndexError when parsing CFWS only
# token.
with self.assertRaises(errors.HeaderParseError):
parser.get_word('(Recipients list suppressed')
def test_get_word_qs_yields_qs(self): def test_get_word_qs_yields_qs(self):
word = self._test_get_x(parser.get_word, word = self._test_get_x(parser.get_word,
'"bar " (bang) ah', '"bar " (bang) ', 'bar ', [], 'ah') '"bar " (bang) ah', '"bar " (bang) ', 'bar ', [], 'ah')
...@@ -2343,6 +2349,17 @@ class TestParser(TestParserMixin, TestEmailBase): ...@@ -2343,6 +2349,17 @@ class TestParser(TestParserMixin, TestEmailBase):
# get_address_list # get_address_list
def test_get_address_list_CFWS(self):
address_list = self._test_get_x(parser.get_address_list,
'(Recipient list suppressed)',
'(Recipient list suppressed)',
' ',
[errors.ObsoleteHeaderDefect], # no content in address list
'')
self.assertEqual(address_list.token_type, 'address-list')
self.assertEqual(len(address_list.mailboxes), 0)
self.assertEqual(address_list.mailboxes, address_list.all_mailboxes)
def test_get_address_list_mailboxes_simple(self): def test_get_address_list_mailboxes_simple(self):
address_list = self._test_get_x(parser.get_address_list, address_list = self._test_get_x(parser.get_address_list,
'dinsdale@example.com', 'dinsdale@example.com',
......
Fix IndexError in parsing a header value ending unexpectedly. Patch by Abhilash
Raj.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment