Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 24ac66a

Browse files
ezequielp-activestateicanhasmath
authored andcommitted
Add better tests for CVE 2023-27043
1 parent cfe4763 commit 24ac66a

2 files changed

Lines changed: 61 additions & 33 deletions

File tree

Lib/email/test/test_email.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,22 @@ def test_parseaddr_multiple_domains(self):
23202320
('', '')
23212321
)
23222322

2323+
def test_parseaddr_unicode(self):
2324+
"""Test parseaddr with unicode strings"""
2325+
2326+
test_cases = [
2327+
2328+
u'Test User <[email protected]>',
2329+
u'"Test User" <[email protected]>',
2330+
]
2331+
2332+
for addr in test_cases:
2333+
result = Utils.parseaddr(addr, strict=True)
2334+
self.assertNotEqual(result, ('', ''))
2335+
2336+
result_non_strict = Utils.parseaddr(addr, strict=False)
2337+
self.assertEqual(result, result_non_strict)
2338+
23232339
def test_noquote_dump(self):
23242340
self.assertEqual(
23252341
Utils.formataddr(('A Silly Person', '[email protected]')),
@@ -2425,29 +2441,27 @@ def test_getaddresses_nasty(self):
24252441
eq(Utils.getaddresses(
24262442
['foo: ;', '"Jason R. Mastaler" <[email protected]>']),
24272443
[('', ''), ('Jason R. Mastaler', '[email protected]')])
2428-
2429-
def test_getaddresses_nasty_unicode(self):
2430-
"""Test parseaddr with unicode strings in Python 2"""
2431-
2432-
test_cases = [
2433-
2434-
u'Test User <[email protected]>',
2435-
u'"Test User" <[email protected]>',
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)
24442444

24452445
def test_getaddresses_embedded_comment(self):
24462446
"""Test proper handling of a nested comment"""
24472447
eq = self.assertEqual
24482448
addrs = Utils.getaddresses(['User ((nested comment)) <[email protected]>'])
24492449
eq(addrs[0][1], '[email protected]')
24502450

2451+
def test_getaddresses_unicode(self):
2452+
"""Test getaddresses with unicode strings in Python 2"""
2453+
2454+
test_cases = [
2455+
2456+
([u'Test User <[email protected]>'], [('Test User', '[email protected]')]),
2457+
([u'"Test User" <[email protected]>'], [('Test User', '[email protected]')]),
2458+
2459+
]
2460+
2461+
for addrs, expected in test_cases:
2462+
result = Utils.getaddresses(addrs)
2463+
self.assertEqual(result, expected)
2464+
24512465
def test_make_msgid_collisions(self):
24522466
# Test make_msgid uniqueness, even with multiple threads
24532467
class MsgidsThread(Thread):

Lib/email/test/test_email_renamed.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,22 @@ def test_parseaddr_empty(self):
21992199
self.assertEqual(utils.parseaddr('<>'), ('', ''))
22002200
self.assertEqual(utils.formataddr(utils.parseaddr('<>')), '')
22012201

2202+
def test_parseaddr_unicode(self):
2203+
"""Test parseaddr with unicode strings"""
2204+
2205+
test_cases = [
2206+
2207+
u'Test User <[email protected]>',
2208+
u'"Test User" <[email protected]>',
2209+
]
2210+
2211+
for addr in test_cases:
2212+
result = utils.parseaddr(addr, strict=True)
2213+
self.assertNotEqual(result, ('', ''))
2214+
2215+
result_non_strict = utils.parseaddr(addr, strict=False)
2216+
self.assertEqual(result, result_non_strict)
2217+
22022218
def test_noquote_dump(self):
22032219
self.assertEqual(
22042220
utils.formataddr(('A Silly Person', '[email protected]')),
@@ -2286,30 +2302,28 @@ def test_getaddresses_nasty(self):
22862302
eq(utils.getaddresses(
22872303
['foo: ;', '"Jason R. Mastaler" <[email protected]>']),
22882304
[('', ''), ('Jason R. Mastaler', '[email protected]')])
2289-
2290-
def test_getaddresses_nasty_unicode(self):
2291-
"""Test parseaddr with unicode strings in Python 2"""
2292-
2293-
test_cases = [
2294-
2295-
u'Test User <[email protected]>',
2296-
u'"Test User" <[email protected]>',
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)
23052305

23062306
def test_getaddresses_embedded_comment(self):
23072307
"""Test proper handling of a nested comment"""
23082308
eq = self.assertEqual
23092309
addrs = utils.getaddresses(['User ((nested comment)) <[email protected]>'])
23102310
eq(addrs[0][1], '[email protected]')
23112311

2312-
def test_utils_quote_unquote(self):
2312+
def test_getaddresses_unicode(self):
2313+
"""Test getaddresses with unicode strings in Python 2"""
2314+
2315+
test_cases = [
2316+
2317+
([u'Test User <[email protected]>'], [('Test User', '[email protected]')]),
2318+
([u'"Test User" <[email protected]>'], [('Test User', '[email protected]')]),
2319+
2320+
]
2321+
2322+
for addrs, expected in test_cases:
2323+
result = utils.getaddresses(addrs)
2324+
self.assertEqual(result, expected)
2325+
2326+
def test__quote_unquote(self):
23132327
eq = self.assertEqual
23142328
msg = Message()
23152329
msg.add_header('content-disposition', 'attachment',

0 commit comments

Comments
 (0)