Skip to content
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

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/email/headerregistry.py
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 inputs; address parts cannot contain CR / LF")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Reading this I think I'd say "arguments" rather than inputs, that aligns better with our typical vocabulary. And how about "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 All @@ -48,6 +53,7 @@ def __init__(self, display_name='', username='', domain='', addr_spec=None):
raise a_s.all_defects[0]
username = a_s.local_part
domain = a_s.domain

self._display_name = display_name
self._username = username
self._domain = domain
Expand Down Expand Up @@ -99,7 +105,6 @@ def __eq__(self, other):
self.username == other.username and
self.domain == other.domain)


class Group:

def __init__(self, display_name=None, addresses=None):
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_email/test_headerregistry.py
Expand Up @@ -1437,6 +1437,22 @@ 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='example.com\r'),
dict(display_name='example.com\n'),
dict(display_name='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')
bitdancer marked this conversation as resolved.
Show resolved Hide resolved
)
for kwargs in cases:
with self.subTest(kwargs=kwargs), self.assertRaisesRegex(ValueError, "invalid inputs"):
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
@@ -0,0 +1 @@
Validate email.headerregistry.Address to disallow CRLF in address parts (username, domain, display_name).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd already made this comment but I can't find it:

"DIsallow CR or LF in email.headerregistry.Address arguments to guard against header injection attacks."

epicfaace marked this conversation as resolved.
Show resolved Hide resolved