Adding IDNA failure handling

This commit is contained in:
László Károlyi 2019-06-26 14:31:52 +02:00
parent 9ab570c9af
commit 0b0e77d662
Signed by: karolyi
GPG Key ID: 2DCAF25E55735BFE
4 changed files with 15 additions and 3 deletions

View File

@ -1,3 +1,6 @@
0.1.11:
- Handling IDNA errors
0.1.10:
- Handling the NoNameservers exception

View File

@ -26,7 +26,7 @@ class PostBuildPyCommand(build_py):
setup(
name='py3-validate-email',
version='0.1.10',
version='0.1.11',
packages=find_packages(exclude=['tests']),
install_requires=['dnspython>=1.16.0', 'idna>=2.8'],
author='László Károlyi',

View File

@ -55,6 +55,7 @@ class IdnaTestCase(TestCase):
second='email@address.com')
class GetMxRecordsTestCase(TestCase):
'Testing `_get_mx_records`.'
@ -100,3 +101,8 @@ class GetMxRecordsTestCase(TestCase):
_get_mx_records(domain='testdomain3', timeout=10)
self.assertEqual(
exc.exception.args[0], 'testdomain3 DNS resolve timed out')
def test_returns_false_on_idna_failure(self):
'Returns `False` on IDNA failure.'
self.assertFalse(expr=mx_module.mx_check(
email_address='test@♥web.de', from_address='mail@example.com'))

View File

@ -9,7 +9,7 @@ from dns.rdatatype import MX as rdtype_mx
from dns.rdtypes.ANY.MX import MX
from dns.resolver import (
NXDOMAIN, YXDOMAIN, Answer, NoAnswer, NoNameservers, query)
from idna.core import encode
from idna.core import encode, IDNAError
from .constants import EMAIL_EXTRACT_HOST_REGEX, HOST_REGEX
@ -106,7 +106,10 @@ def mx_check(
"""
host = helo_host or gethostname()
idna_from = _get_idna_address(email_address=from_address or email_address)
idna_to = _get_idna_address(email_address=email_address)
try:
idna_to = _get_idna_address(email_address=email_address)
except IDNAError:
return False
_user, domain = _dissect_email(email_address=email_address)
try:
mx_records = _get_mx_records(domain=domain, timeout=dns_timeout)