Skip to content

Commit c98f6b9

Browse files
ezequielp-activestateicanhasmath
authored andcommitted
Refactor CVE-2023-27043 patch to support Unicode characters
1 parent ef22247 commit c98f6b9

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

Lib/email/test/test_email.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,22 @@ def test_getaddresses_nasty(self):
24252425
eq(Utils.getaddresses(
24262426
['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
24272427
[('', ''), ('Jason R. Mastaler', 'jason@dom.ain')])
2428+
2429+
def test_getaddresses_nasty_unicode(self):
2430+
"""Test parseaddr with unicode strings in Python 2"""
2431+
2432+
test_cases = [
2433+
u'user@example.com',
2434+
u'Test User <user@example.com>',
2435+
u'"Test User" <user@example.com>',
2436+
]
2437+
2438+
for addr in test_cases:
2439+
result = Utils.parseaddr(addr, strict=True)
2440+
self.assertNotEqual(result, ('', ''))
2441+
2442+
result_non_strict = Utils.parseaddr(addr, strict=False)
2443+
self.assertEqual(result, result_non_strict)
24282444

24292445
def test_getaddresses_embedded_comment(self):
24302446
"""Test proper handling of a nested comment"""

Lib/email/test/test_email_renamed.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,22 @@ def test_getaddresses_nasty(self):
22862286
eq(utils.getaddresses(
22872287
['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
22882288
[('', ''), ('Jason R. Mastaler', 'jason@dom.ain')])
2289+
2290+
def test_getaddresses_nasty_unicode(self):
2291+
"""Test parseaddr with unicode strings in Python 2"""
2292+
2293+
test_cases = [
2294+
u'user@example.com',
2295+
u'Test User <user@example.com>',
2296+
u'"Test User" <user@example.com>',
2297+
]
2298+
2299+
for addr in test_cases:
2300+
result = utils.parseaddr(addr, strict=True)
2301+
self.assertNotEqual(result, ('', ''))
2302+
2303+
result_non_strict = utils.parseaddr(addr, strict=False)
2304+
self.assertEqual(result, result_non_strict)
22892305

22902306
def test_getaddresses_embedded_comment(self):
22912307
"""Test proper handling of a nested comment"""

Lib/email/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,14 @@ def parseaddr(addr, strict=True):
339339
if isinstance(addr, list):
340340
addr = addr[0]
341341

342-
if not isinstance(addr, str):
342+
# FIX: Support both str and unicode in Python 2
343+
if not isinstance(addr, (str, unicode)): # Python 2 compatible
343344
return ('', '')
344345

346+
# Convert unicode to str for consistent processing
347+
if isinstance(addr, unicode):
348+
addr = addr.encode('utf-8')
349+
345350
addr = _pre_parse_validation([addr])[0]
346351
addrs = _post_parse_validation(_AddressList(addr).addresslist)
347352

0 commit comments

Comments
 (0)