Skip to content

bpo-39073: validate Address parts to disallow CRLF #19007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/email/headerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def __init__(self, display_name='', username='', domain='', addr_spec=None):
without any Content Transfer Encoding.

"""

inputs = ''.join(filter(None, (display_name, username, domain, addr_spec)))
if '\r' in inputs or '\n' in inputs:
raise ValueError("invalid arguments; address parts cannot contain CR or LF")

# This clause with its potential 'raise' may only happen when an
# application program creates an Address object using an addr_spec
# keyword. The email library code itself must always supply username
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_email/test_headerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,25 @@ def test_il8n(self):
# with self.assertRaises(ValueError):
# Address('foo', 'wők', 'example.com')

def test_crlf_in_constructor_args_raises(self):
cases = (
dict(display_name='foo\r'),
dict(display_name='foo\n'),
dict(display_name='foo\r\n'),
dict(domain='example.com\r'),
dict(domain='example.com\n'),
dict(domain='example.com\r\n'),
dict(username='wok\r'),
dict(username='wok\n'),
dict(username='wok\r\n'),
dict(addr_spec='wok@example.com\r'),
dict(addr_spec='wok@example.com\n'),
dict(addr_spec='wok@example.com\r\n')
)
for kwargs in cases:
with self.subTest(kwargs=kwargs), self.assertRaisesRegex(ValueError, "invalid arguments"):
Address(**kwargs)

def test_non_ascii_username_in_addr_spec_raises(self):
with self.assertRaises(ValueError):
Address('foo', addr_spec='wők@example.com')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Disallow CR or LF in email.headerregistry.Address arguments to guard against header injection attacks.