Code

Ticket #19070: 19070-2.diff

File 19070-2.diff, 1.9 KB (added by tom@…, 19 months ago)
Line 
1diff --git a/django/utils/html.py b/django/utils/html.py
2index d914234..25605be 100644
3--- a/django/utils/html.py
4+++ b/django/utils/html.py
5@@ -150,13 +150,17 @@ fix_ampersands = allow_lazy(fix_ampersands, six.text_type)
6 def smart_urlquote(url):
7     "Quotes a URL if it isn't already quoted."
8     # Handle IDN before quoting.
9-    scheme, netloc, path, query, fragment = urlsplit(url)
10     try:
11-        netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE
12-    except UnicodeError: # invalid domain part
13+        scheme, netloc, path, query, fragment = urlsplit(url)
14+        try:
15+            netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE
16+        except UnicodeError: # invalid domain part
17+            pass
18+        else:
19+            url = urlunsplit((scheme, netloc, path, query, fragment))
20+    except ValueError:
21+        # invalid IPv6 URL (normally square brackets in hostname part).
22         pass
23-    else:
24-        url = urlunsplit((scheme, netloc, path, query, fragment))
25 
26     # An URL is considered unquoted if it contains no % characters or
27     # contains a % not followed by two hexadecimal digits. See #9655.
28diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
29index 52268da..8596f8c 100644
30--- a/tests/regressiontests/defaultfilters/tests.py
31+++ b/tests/regressiontests/defaultfilters/tests.py
32@@ -310,6 +310,10 @@ class DefaultFiltersTests(TestCase):
33         self.assertEqual(urlize('[see www.example.com]'),
34             '[see <a href="http://www.example.com" rel="nofollow">www.example.com</a>]' )
35 
36+        # Check urlize doesn't crash when square bracket is prepended to url (#19070)
37+        self.assertEqual(urlize('see test[at[example.com'),
38+            'see <a href="http://test[at[example.com" rel="nofollow">test[at[example.com</a>' )
39+
40 
41     def test_wordcount(self):
42         self.assertEqual(wordcount(''), 0)