[3.12] gh-75171: Fix parsing invalid email address headers starting or ending with a dot (GH-15600) (GH-117964)

(cherry picked from commit 8cc9adbfdd)

Co-authored-by: tsufeki <tsufeki@ymail.com>
Co-authored-by: Tim Bell <timothybell@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-04-17 09:55:11 +02:00 committed by GitHub
parent 5f4c7cf3f4
commit 03108045d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 61 additions and 5 deletions

View file

@ -566,12 +566,14 @@ class DisplayName(Phrase):
if res[0].token_type == 'cfws':
res.pop(0)
else:
if res[0][0].token_type == 'cfws':
if (isinstance(res[0], TokenList) and
res[0][0].token_type == 'cfws'):
res[0] = TokenList(res[0][1:])
if res[-1].token_type == 'cfws':
res.pop()
else:
if res[-1][-1].token_type == 'cfws':
if (isinstance(res[-1], TokenList) and
res[-1][-1].token_type == 'cfws'):
res[-1] = TokenList(res[-1][:-1])
return res.value
@ -586,9 +588,13 @@ class DisplayName(Phrase):
quote = True
if len(self) != 0 and quote:
pre = post = ''
if self[0].token_type=='cfws' or self[0][0].token_type=='cfws':
if (self[0].token_type == 'cfws' or
isinstance(self[0], TokenList) and
self[0][0].token_type == 'cfws'):
pre = ' '
if self[-1].token_type=='cfws' or self[-1][-1].token_type=='cfws':
if (self[-1].token_type == 'cfws' or
isinstance(self[-1], TokenList) and
self[-1][-1].token_type == 'cfws'):
post = ' '
return pre+quote_string(self.display_name)+post
else:
@ -1772,7 +1778,10 @@ def get_name_addr(value):
raise errors.HeaderParseError(
"expected name-addr but found '{}'".format(token))
if leader is not None:
if isinstance(token[0], TokenList):
token[0][:0] = [leader]
else:
token[:0] = [leader]
leader = None
name_addr.append(token)
token, value = get_angle_addr(value)

View file

@ -1805,6 +1805,32 @@ class TestParser(TestParserMixin, TestEmailBase):
self.assertIsNone(name_addr.route)
self.assertEqual(name_addr.addr_spec, 'dinsdale@example.com')
def test_get_name_addr_ending_with_dot_without_space(self):
name_addr = self._test_get_x(parser.get_name_addr,
'John X.<jxd@example.com>',
'John X.<jxd@example.com>',
'"John X."<jxd@example.com>',
[errors.ObsoleteHeaderDefect],
'')
self.assertEqual(name_addr.display_name, 'John X.')
self.assertEqual(name_addr.local_part, 'jxd')
self.assertEqual(name_addr.domain, 'example.com')
self.assertIsNone(name_addr.route)
self.assertEqual(name_addr.addr_spec, 'jxd@example.com')
def test_get_name_addr_starting_with_dot(self):
name_addr = self._test_get_x(parser.get_name_addr,
'. Doe <jxd@example.com>',
'. Doe <jxd@example.com>',
'". Doe" <jxd@example.com>',
[errors.InvalidHeaderDefect, errors.ObsoleteHeaderDefect],
'')
self.assertEqual(name_addr.display_name, '. Doe')
self.assertEqual(name_addr.local_part, 'jxd')
self.assertEqual(name_addr.domain, 'example.com')
self.assertIsNone(name_addr.route)
self.assertEqual(name_addr.addr_spec, 'jxd@example.com')
def test_get_name_addr_with_route(self):
name_addr = self._test_get_x(parser.get_name_addr,
'"Roy.A.Bear" <@two.example.com: dinsdale@example.com>',

View file

@ -1237,6 +1237,26 @@ class TestAddressHeader(TestHeaderBase):
'example.com',
None),
'name_ending_with_dot_without_space':
('John X.<jxd@example.com>',
[errors.ObsoleteHeaderDefect],
'"John X." <jxd@example.com>',
'John X.',
'jxd@example.com',
'jxd',
'example.com',
None),
'name_starting_with_dot':
('. Doe <jxd@example.com>',
[errors.InvalidHeaderDefect, errors.ObsoleteHeaderDefect],
'". Doe" <jxd@example.com>',
'. Doe',
'jxd@example.com',
'jxd',
'example.com',
None),
}
# XXX: Need many more examples, and in particular some with names in

View file

@ -0,0 +1 @@
Fix parsing of emails with invalid address headers having a leading or trailing dot. Patch by tsufeki.