Skip to content

Commit 9500bbe

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 <maxking@users.noreply.github.com>
1 parent c61f9b5 commit 9500bbe

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
@@ -549,6 +549,8 @@ class DisplayName(Phrase):
549549
@property
550550
def display_name(self):
551551
res = TokenList(self)
552+
if len(res) == 0:
553+
return res.value
552554
if res[0].token_type == 'cfws':
553555
res.pop(0)
554556
else:
@@ -570,7 +572,7 @@ def value(self):
570572
for x in self:
571573
if x.token_type == 'quoted-string':
572574
quote = True
573-
if quote:
575+
if len(self) != 0 and quote:
574576
pre = post = ''
575577
if self[0].token_type=='cfws' or self[0][0].token_type=='cfws':
576578
pre = ' '

Lib/test/test_email/test__header_value_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,14 @@ def test_get_display_name_ending_with_obsolete(self):
16801680
self.assertEqual(display_name[3].comments, ['with trailing comment'])
16811681
self.assertEqual(display_name.display_name, 'simple phrase.')
16821682

1683+
def test_get_display_name_for_invalid_address_field(self):
1684+
# bpo-32178: Test that address fields starting with `:` don't cause
1685+
# IndexError when parsing the display name.
1686+
display_name = self._test_get_x(
1687+
parser.get_display_name,
1688+
':Foo ', '', '', [errors.InvalidHeaderDefect], ':Foo ')
1689+
self.assertEqual(display_name.value, '')
1690+
16831691
# get_name_addr
16841692

16851693
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)