Email validator: don't allow whitespace in the address

See sections 3.4.1 and 3.2.4 of RFC 2822.

From my reading, you can *technically* include whitespace if it's quoted, but
for our purposes it's almost certainly going to be a mistake we want to notify
the user about.
This commit is contained in:
xiongchiamiov
2015-02-26 10:31:22 -08:00
parent f7b549a2c9
commit c7bbc0e566
2 changed files with 17 additions and 1 deletions

View File

@@ -2050,6 +2050,11 @@ class ValidEmail(Validator):
"""Validates a single email. Returns the email on success."""
def run(self, email):
# Strip out leading/trailing whitespace, since the inclusion of that is
# a common and easily-fixable user error.
if email is not None:
email = email.strip()
if not email:
self.set_error(errors.NO_EMAIL)
elif not ValidEmails.email_re.match(email):
@@ -2065,7 +2070,7 @@ class ValidEmails(Validator):
success"""
separator = re.compile(r'[^\s,;]+')
email_re = re.compile(r'.+@.+\..+')
email_re = re.compile(r'[^\s]+@[^\s]+\.[^\s]+')
def __init__(self, param, num = 20, **kw):
self.num = num

View File

@@ -61,6 +61,17 @@ class TestValidEmail(unittest.TestCase):
def test_blank_email(self):
self._test_failure('', errors.NO_EMAIL)
self.setUp()
self._test_failure(' ', errors.NO_EMAIL)
def test_no_whitespace(self):
self._test_failure('test @example.com')
self.setUp()
self._test_failure('test@ example.com')
self.setUp()
self._test_failure('test@example. com')
self.setUp()
self._test_failure("test@\texample.com")
def test_no_hostname(self):
self._test_failure('example')