Skip to content

Commit 08c733e

Browse files
author
Adam Robbins-Pianka
committed
ENH: Bring email validation closer to specs
Fix #1179 This doesn't fully account for all valid email addresses (doing so is too much of a headache to implement, at least until it becomes necessary...), but it's closer, and will pass for the majority of email addresses.
1 parent da407f6 commit 08c733e

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

qiita_db/user.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# The full license is in the file LICENSE, distributed with this software.
2929
# -----------------------------------------------------------------------------
3030
from __future__ import division
31-
from re import match
31+
from re import sub
3232

3333
from qiita_core.exceptions import (IncorrectEmailError, IncorrectPasswordError,
3434
IncompetentQiitaDeveloperError)
@@ -476,15 +476,40 @@ def validate_email(email):
476476
bool
477477
Whether or not the email is valid
478478
"""
479-
valid_chars = "a-zA-Z0-9\.\+\-"
480-
pattern = r"[%s]+@[%s]+\.[%s]+" % (valid_chars, valid_chars, valid_chars)
481-
479+
# Do not accept email addresses that have unicode characters
482480
try:
483481
email.encode('ascii')
484482
except UnicodeError:
485483
return False
486484

487-
return True if match(pattern, email) is not None else False
485+
# we are not allowing quoted strings in the email address
486+
if '"' in email:
487+
return False
488+
489+
# Must have exactly 1 @ symbol
490+
if email.count('@') != 1:
491+
return False
492+
493+
local_part, domain_part = email.split('@')
494+
495+
# Neither part can be blank
496+
if not (local_part and domain_part):
497+
return False
498+
499+
# The local part cannot begin or end with a dot
500+
if local_part.startswith('.') or local_part.endswith('.'):
501+
return False
502+
503+
# This is the full set of allowable characters for the local part.
504+
local_valid_chars = "[a-zA-Z0-9#_~!$&'()*+,;=:.-]"
505+
if len(sub(local_valid_chars, '', local_part)):
506+
return False
507+
508+
domain_valid_chars = "[a-zA-Z0-9.]"
509+
if len(sub(domain_valid_chars, '', domain_part)):
510+
return False
511+
512+
return True
488513

489514

490515
def validate_password(password):

0 commit comments

Comments
 (0)