From 0b0e77d662f52a6ce8c484509254ae01a5a38924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20K=C3=A1rolyi?= Date: Wed, 26 Jun 2019 14:31:52 +0200 Subject: [PATCH] Adding IDNA failure handling --- CHANGELOG.txt | 3 +++ setup.py | 2 +- tests/test_mx_check.py | 6 ++++++ validate_email/mx_check.py | 7 +++++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index b6f6593..eff1110 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,6 @@ +0.1.11: +- Handling IDNA errors + 0.1.10: - Handling the NoNameservers exception diff --git a/setup.py b/setup.py index f5c98fe..20f4ba7 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/tests/test_mx_check.py b/tests/test_mx_check.py index a5696c3..7a29a01 100644 --- a/tests/test_mx_check.py +++ b/tests/test_mx_check.py @@ -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')) diff --git a/validate_email/mx_check.py b/validate_email/mx_check.py index 7f9df48..7b4aaca 100644 --- a/validate_email/mx_check.py +++ b/validate_email/mx_check.py @@ -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)