diff --git a/django/utils/html.py b/django/utils/html.py index de6b35dfd4..78359f97f6 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -168,7 +168,10 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False): url = smart_urlquote('http://%s' % middle) elif not ':' in middle and simple_email_re.match(middle): local, domain = middle.rsplit('@', 1) - domain = domain.encode('idna') + try: + domain = domain.encode('idna') + except UnicodeError: + continue url = 'mailto:%s@%s' % (local, domain) nofollow_attr = '' diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py index 94e8c43430..36f8f5851c 100644 --- a/tests/regressiontests/defaultfilters/tests.py +++ b/tests/regressiontests/defaultfilters/tests.py @@ -288,6 +288,10 @@ class DefaultFiltersTests(TestCase): self.assertEqual(urlize('usa.gov'), u'usa.gov') + # Check urlize don't crash on invalid email with dot-starting domain - see #17592 + self.assertEqual(urlize('email@.stream.ru'), + u'email@.stream.ru') + def test_wordcount(self): self.assertEqual(wordcount(''), 0) self.assertEqual(wordcount(u'oneword'), 1)