Skip to content

Commit dec231a

Browse files
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 09a1872) Co-authored-by: Abhilash Raj <[email protected]>
1 parent c48d606 commit dec231a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Lib/email/_header_value_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ class DisplayName(Phrase):
566566
@property
567567
def display_name(self):
568568
res = TokenList(self)
569+
if len(res) == 0:
570+
return res.value
569571
if res[0].token_type == 'cfws':
570572
res.pop(0)
571573
else:
@@ -587,7 +589,7 @@ def value(self):
587589
for x in self:
588590
if x.token_type == 'quoted-string':
589591
quote = True
590-
if quote:
592+
if len(self) != 0 and quote:
591593
pre = post = ''
592594
if self[0].token_type=='cfws' or self[0][0].token_type=='cfws':
593595
pre = ' '

Lib/test/test_email/test__header_value_parser.py

Lines changed: 8 additions & 0 deletions
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):
Lines changed: 1 addition & 0 deletions
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)