Skip to content

[3.8] bpo-29412: Fix indexError when parsing a header value ending unexpectedly (GH-14387) #14411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2019
Merged
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
3 changes: 3 additions & 0 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,9 @@ def get_word(value):
leader, value = get_cfws(value)
else:
leader = None
if not value:
raise errors.HeaderParseError(
"Expected 'atom' or 'quoted-string' but found nothing.")
if value[0]=='"':
token, value = get_quoted_string(value)
elif value[0] in SPECIALS:
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,12 @@ def test_get_word_atom_yields_atom(self):
self.assertEqual(word.token_type, 'atom')
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):
word = self._test_get_x(parser.get_word,
'"bar " (bang) ah', '"bar " (bang) ', 'bar ', [], 'ah')
Expand Down Expand Up @@ -2323,6 +2329,17 @@ def test_get_address_quoted_strings_in_atom_list(self):

# 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):
address_list = self._test_get_x(parser.get_address_list,
'dinsdale@example.com',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix IndexError in parsing a header value ending unexpectedly. Patch by Abhilash
Raj.