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 2 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
10 changes: 10 additions & 0 deletions Lib/email/headerregistry.py
Expand Up @@ -48,6 +48,12 @@ 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
else:
self._validate_part(username)
self._validate_part(domain)

self._validate_part(display_name)

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

def _validate_part(self, value):
"""Parts cannot contain CRLF for security reasons."""
if '\r\n' in value:
raise ValueError("invalid address; address parts cannot contain CRLF")
epicfaace marked this conversation as resolved.
Show resolved Hide resolved

class Group:

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_email/test_headerregistry.py
Expand Up @@ -1437,6 +1437,18 @@ def test_il8n(self):
# with self.assertRaises(ValueError):
# Address('foo', 'wők', 'example.com')

def test_crlf_in_display_name_raises(self):
with self.assertRaisesRegex(ValueError, "invalid address"):
Address(display_name='example.com\r\n')

def test_crlf_in_username_raises(self):
with self.assertRaisesRegex(ValueError, "invalid address"):
Address(username='hello\r\n')

def test_crlf_in_domain_raises(self):
with self.assertRaisesRegex(ValueError, "invalid address"):
Address(domain='example.com\r\n')
epicfaace marked this conversation as resolved.
Show resolved Hide resolved

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