From b08bde5e3c46f8ebc54925c5707013344a5c4ecf Mon Sep 17 00:00:00 2001 From: Abhilash Raj Date: Tue, 30 Jul 2019 22:41:10 -0700 Subject: [PATCH 1/4] bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. --- Lib/email/_header_value_parser.py | 4 +++- Lib/test/test_email/test__header_value_parser.py | 6 ++++++ .../next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py index 641e0979f3d785..9984e66cad3c42 100644 --- a/Lib/email/_header_value_parser.py +++ b/Lib/email/_header_value_parser.py @@ -561,6 +561,8 @@ class DisplayName(Phrase): @property def display_name(self): res = TokenList(self) + if len(res) == 0: + return res.value if res[0].token_type == 'cfws': res.pop(0) else: @@ -582,7 +584,7 @@ def value(self): for x in self: if x.token_type == 'quoted-string': quote = True - if quote: + if self and quote: pre = post = '' if self[0].token_type=='cfws' or self[0][0].token_type=='cfws': pre = ' ' diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py index f6e5886f7571fc..55737e2267238a 100644 --- a/Lib/test/test_email/test__header_value_parser.py +++ b/Lib/test/test_email/test__header_value_parser.py @@ -1700,6 +1700,12 @@ def test_get_display_name_ending_with_obsolete(self): self.assertEqual(display_name[3].comments, ['with trailing comment']) self.assertEqual(display_name.display_name, 'simple phrase.') + def test_get_display_name_bpo_32178(self): + display_name = self._test_get_x( + parser.get_display_name, + ':Foo ', '', '', [errors.InvalidHeaderDefect], ':Foo ') + self.assertEqual(display_name.value, '') + # get_name_addr def test_get_name_addr_angle_addr_only(self): diff --git a/Misc/NEWS.d/next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst b/Misc/NEWS.d/next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst new file mode 100644 index 00000000000000..1b591151ec96b8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst @@ -0,0 +1 @@ +Fix IndexError trying to parse invalid address fields starting with ``:``. From 0849ffb78e3f37e093f8bae2b1d41806388ae1b2 Mon Sep 17 00:00:00 2001 From: Abhilash Raj Date: Sat, 10 Aug 2019 13:23:05 -0700 Subject: [PATCH 2/4] Make the check of length more explicit. --- Lib/email/_header_value_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py index 9984e66cad3c42..ea33bd8eda7d39 100644 --- a/Lib/email/_header_value_parser.py +++ b/Lib/email/_header_value_parser.py @@ -584,7 +584,7 @@ def value(self): for x in self: if x.token_type == 'quoted-string': quote = True - if self and quote: + if len(self) != 0 and quote: pre = post = '' if self[0].token_type=='cfws' or self[0][0].token_type=='cfws': pre = ' ' From 380103ca73b748e25348d4d4bec4bfef9596bc08 Mon Sep 17 00:00:00 2001 From: Abhilash Raj Date: Sat, 10 Aug 2019 13:38:26 -0700 Subject: [PATCH 3/4] Rename the test case. --- Lib/test/test_email/test__header_value_parser.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py index 55737e2267238a..b3e6b2661524e9 100644 --- a/Lib/test/test_email/test__header_value_parser.py +++ b/Lib/test/test_email/test__header_value_parser.py @@ -1700,7 +1700,9 @@ def test_get_display_name_ending_with_obsolete(self): self.assertEqual(display_name[3].comments, ['with trailing comment']) self.assertEqual(display_name.display_name, 'simple phrase.') - def test_get_display_name_bpo_32178(self): + 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 ') From e71f34488346fa078628bd0cd0f2fb2630e24e0b Mon Sep 17 00:00:00 2001 From: Abhilash Raj Date: Sun, 11 Aug 2019 12:27:52 -0700 Subject: [PATCH 4/4] Update NEWS.rst to point to email module. Co-Authored-By: Sanyam Khurana <8039608+CuriousLearner@users.noreply.github.com> --- .../next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst b/Misc/NEWS.d/next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst index 1b591151ec96b8..5e7a2e964d93db 100644 --- a/Misc/NEWS.d/next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst +++ b/Misc/NEWS.d/next/Library/2019-07-30-22-41-05.bpo-32178.X-IFLe.rst @@ -1 +1 @@ -Fix IndexError trying to parse invalid address fields starting with ``:``. +Fix IndexError in :mod:`email` package when trying to parse invalid address fields starting with ``:``.