Skip to content

Commit

Permalink
Improve email address validation
Browse files Browse the repository at this point in the history
Fixes #88
  • Loading branch information
fluffy-critter committed Aug 27, 2020
1 parent 4349412 commit 17a5598
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
13 changes: 8 additions & 5 deletions authl/handlers/email_addr.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ def handles_url(self, url):
"""

parsed = urllib.parse.urlparse(url)
if parsed.scheme == 'mailto' and validate_email.validate_email(parsed.path):
return url
if parsed.scheme:
if parsed.scheme not in ('', 'mailto'):
return None

if validate_email.validate_email(url):
return 'mailto:' + url
address = parsed.path.strip()

if ' ' in address or '!' in address:
return None

if validate_email.validate_email(address):
return 'mailto:' + address

return None

Expand Down
14 changes: 14 additions & 0 deletions tests/handlers/test_emailaddr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def test_basics():
assert not handler.handles_url('@foo@bar.baz')
assert not handler.handles_url('https://example.com/')

assert handler.handles_url(' foo@bar.baz') == 'mailto:foo@bar.baz'
assert handler.handles_url('mailto: foo@bar.baz') == 'mailto:foo@bar.baz'
assert handler.handles_url('mailto:foo@bar.baz ') == 'mailto:foo@bar.baz'

assert not handler.handles_url(' foo @bar.baz')

assert not handler.handles_url(' asdf[]@poiu_foo.baz!')

assert not handler.handles_url('bang!path!is!fun!bob')
assert not handler.handles_url('bang.com!path!is!fun!bob')
assert not handler.handles_url('bang!path!is!fun!bob@example.com')

assert handler.handles_url('mailto:foo@example.com?subject=pwned') == 'mailto:foo@example.com'


def test_success():
store = {}
Expand Down

0 comments on commit 17a5598

Please sign in to comment.