Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09a1872

Browse files
maxkingmiss-islington
authored andcommitted
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
1 parent 51aac15 commit 09a1872

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Lib/email/_header_value_parser.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ class DisplayName(Phrase):
561561
@property
562562
def display_name(self):
563563
res = TokenList(self)
564+
if len(res) == 0:
565+
return res.value
564566
if res[0].token_type == 'cfws':
565567
res.pop(0)
566568
else:
@@ -582,7 +584,7 @@ def value(self):
582584
for x in self:
583585
if x.token_type == 'quoted-string':
584586
quote = True
585-
if quote:
587+
if len(self) != 0 and quote:
586588
pre = post = ''
587589
if self[0].token_type=='cfws' or self[0][0].token_type=='cfws':
588590
pre = ' '

Lib/test/test_email/test__header_value_parser.py

+8
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,14 @@ def test_get_display_name_ending_with_obsolete(self):
17001700
self.assertEqual(display_name[3].comments, ['with trailing comment'])
17011701
self.assertEqual(display_name.display_name, 'simple phrase.')
17021702

1703+
def test_get_display_name_for_invalid_address_field(self):
1704+
# bpo-32178: Test that address fields starting with `:` don't cause
1705+
# IndexError when parsing the display name.
1706+
display_name = self._test_get_x(
1707+
parser.get_display_name,
1708+
':Foo ', '', '', [errors.InvalidHeaderDefect], ':Foo ')
1709+
self.assertEqual(display_name.value, '')
1710+
17031711
# get_name_addr
17041712

17051713
def test_get_name_addr_angle_addr_only(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix IndexError in :mod:`email` package when trying to parse invalid address fields starting with ``:``.

0 commit comments

Comments
 (0)