Skip to content

Commit

Permalink
Merge pull request #4720 from sivang/fix-email-validation-regexp
Browse files Browse the repository at this point in the history
enable email validator to exclude malformed dots usage in email addre…
  • Loading branch information
amercader committed Apr 12, 2019
2 parents 8b0a6be + ca3d707 commit 6eec1c4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ckan/logic/validators.py
Expand Up @@ -842,9 +842,13 @@ def empty_if_not_sysadmin(key, data, errors, context):
empty(key, data, errors, context)

#pattern from https://html.spec.whatwg.org/#e-mail-state-(type=email)
email_pattern = re.compile(r"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]"\
"(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9]"\
"(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
email_pattern = re.compile(
# additional pattern to reject malformed dots usage
r"^(?!\.)(?!.*\.$)(?!.*?\.\.)"\
"[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]"\
"(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9]"\
"(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
)


def email_validator(value, context):
Expand Down
29 changes: 29 additions & 0 deletions ckan/tests/logic/test_validators.py
Expand Up @@ -193,6 +193,35 @@ def call_validator(*args, **kwargs):
return validators.name_validator(*args, **kwargs)
call_validator(invalid_value, context={})

def test_email_validator_with_invalid_value(selfs):
invalid_values = [
'..test...test..@example.com',
'test @example.com',
'test@ example.com',
'test..test@example.com',
'test.test...@example.com',
'...test@example.com',
]

for invalid_value in invalid_values:
@raises_Invalid
def call_validator(*args, **kwargs):
return validators.email_validator(*args, **kwargs)
call_validator(invalid_value, context={})

def test_email_validator_with_valid_value(self):
valid_values = [
'text@example.com',
'test.this@example.com',
'test.this@server.example.com',
]

for valid_value in valid_values:
@returns_arg
def call_validator(*args, **kwargs):
return validators.email_validator(*args, **kwargs)
call_validator(valid_value)

def test_name_validator_with_valid_value(self):
'''If given a valid string name_validator() should do nothing and
return the string.
Expand Down

0 comments on commit 6eec1c4

Please sign in to comment.