Skip to content

Commit f91a0b6

Browse files
authored
bpo-39073: validate Address parts to disallow CRLF (python#19007) (python#20450)
Disallow CR or LF in email.headerregistry.Address arguments to guard against header injection attacks. (cherry picked from commit 614f172)
1 parent f88b578 commit f91a0b6

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Lib/email/headerregistry.py

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def __init__(self, display_name='', username='', domain='', addr_spec=None):
3131
without any Content Transfer Encoding.
3232
3333
"""
34+
35+
inputs = ''.join(filter(None, (display_name, username, domain, addr_spec)))
36+
if '\r' in inputs or '\n' in inputs:
37+
raise ValueError("invalid arguments; address parts cannot contain CR or LF")
38+
3439
# This clause with its potential 'raise' may only happen when an
3540
# application program creates an Address object using an addr_spec
3641
# keyword. The email library code itself must always supply username

Lib/test/test_email/test_headerregistry.py

+19
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,25 @@ def test_il8n(self):
14151415
# with self.assertRaises(ValueError):
14161416
# Address('foo', 'wők', 'example.com')
14171417

1418+
def test_crlf_in_constructor_args_raises(self):
1419+
cases = (
1420+
dict(display_name='foo\r'),
1421+
dict(display_name='foo\n'),
1422+
dict(display_name='foo\r\n'),
1423+
dict(domain='example.com\r'),
1424+
dict(domain='example.com\n'),
1425+
dict(domain='example.com\r\n'),
1426+
dict(username='wok\r'),
1427+
dict(username='wok\n'),
1428+
dict(username='wok\r\n'),
1429+
dict(addr_spec='wok@example.com\r'),
1430+
dict(addr_spec='wok@example.com\n'),
1431+
dict(addr_spec='wok@example.com\r\n')
1432+
)
1433+
for kwargs in cases:
1434+
with self.subTest(kwargs=kwargs), self.assertRaisesRegex(ValueError, "invalid arguments"):
1435+
Address(**kwargs)
1436+
14181437
def test_non_ascii_username_in_addr_spec_raises(self):
14191438
with self.assertRaises(ValueError):
14201439
Address('foo', addr_spec='wők@example.com')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disallow CR or LF in email.headerregistry.Address arguments to guard against header injection attacks.

0 commit comments

Comments
 (0)