Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/email/headerregistry.py
Original file line number Diff line number Diff line change
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")

class Group:

Expand Down
12 changes: 12 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,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')

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 @@
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."