mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-04-27 03:00:12 -04:00
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:
@@ -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
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user