bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. (GH-15044)

This should fix the IndexError trying to retrieve `DisplayName.display_name` and `DisplayName.value` when the `value` is basically an empty string.

https://bugs.python.org/issue32178
(cherry picked from commit 09a1872a80)

Co-authored-by: Abhilash Raj <maxking@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2019-08-11 14:04:31 -07:00 committed by GitHub
parent c61f9b57cf
commit 9500bbe937
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 1 deletions

View File

@ -549,6 +549,8 @@ class DisplayName(Phrase):
@property @property
def display_name(self): def display_name(self):
res = TokenList(self) res = TokenList(self)
if len(res) == 0:
return res.value
if res[0].token_type == 'cfws': if res[0].token_type == 'cfws':
res.pop(0) res.pop(0)
else: else:
@ -570,7 +572,7 @@ class DisplayName(Phrase):
for x in self: for x in self:
if x.token_type == 'quoted-string': if x.token_type == 'quoted-string':
quote = True quote = True
if quote: if len(self) != 0 and quote:
pre = post = '' pre = post = ''
if self[0].token_type=='cfws' or self[0][0].token_type=='cfws': if self[0].token_type=='cfws' or self[0][0].token_type=='cfws':
pre = ' ' pre = ' '

View File

@ -1680,6 +1680,14 @@ class TestParser(TestParserMixin, TestEmailBase):
self.assertEqual(display_name[3].comments, ['with trailing comment']) self.assertEqual(display_name[3].comments, ['with trailing comment'])
self.assertEqual(display_name.display_name, 'simple phrase.') self.assertEqual(display_name.display_name, 'simple phrase.')
def test_get_display_name_for_invalid_address_field(self):
# bpo-32178: Test that address fields starting with `:` don't cause
# IndexError when parsing the display name.
display_name = self._test_get_x(
parser.get_display_name,
':Foo ', '', '', [errors.InvalidHeaderDefect], ':Foo ')
self.assertEqual(display_name.value, '')
# get_name_addr # get_name_addr
def test_get_name_addr_angle_addr_only(self): def test_get_name_addr_angle_addr_only(self):

View File

@ -0,0 +1 @@
Fix IndexError in :mod:`email` package when trying to parse invalid address fields starting with ``:``.