diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2012-12-04 02:18:10 -0800 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2012-12-04 02:18:10 -0800 |
| commit | 501c7a221cfd022b6d0021fd9a54005c5ffc7a23 (patch) | |
| tree | 786d2a9cce6f1af4452b801fc8382e30a67922c2 | |
| parent | b64d30405a3d5468dc8c6232747d45bbeee4f7bb (diff) | |
| parent | 74809fdcc75c400e7d2eac4fa1b0f4113f9f395b (diff) | |
Merge pull request #573 from tominsam/master
Fixed #19070: urlize template filter raises exception in some cases
| -rw-r--r-- | django/utils/html.py | 14 | ||||
| -rw-r--r-- | tests/regressiontests/defaultfilters/tests.py | 4 |
2 files changed, 13 insertions, 5 deletions
diff --git a/django/utils/html.py b/django/utils/html.py index d914234d60..25605bea04 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -150,13 +150,17 @@ fix_ampersands = allow_lazy(fix_ampersands, six.text_type) def smart_urlquote(url): "Quotes a URL if it isn't already quoted." # Handle IDN before quoting. - scheme, netloc, path, query, fragment = urlsplit(url) try: - netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE - except UnicodeError: # invalid domain part + scheme, netloc, path, query, fragment = urlsplit(url) + try: + netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE + except UnicodeError: # invalid domain part + pass + else: + url = urlunsplit((scheme, netloc, path, query, fragment)) + except ValueError: + # invalid IPv6 URL (normally square brackets in hostname part). pass - else: - url = urlunsplit((scheme, netloc, path, query, fragment)) # An URL is considered unquoted if it contains no % characters or # contains a % not followed by two hexadecimal digits. See #9655. diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py index 52268da2ec..8596f8c801 100644 --- a/tests/regressiontests/defaultfilters/tests.py +++ b/tests/regressiontests/defaultfilters/tests.py @@ -310,6 +310,10 @@ class DefaultFiltersTests(TestCase): self.assertEqual(urlize('[see www.example.com]'), '[see <a href="http://www.example.com" rel="nofollow">www.example.com</a>]' ) + # Check urlize doesn't crash when square bracket is prepended to url (#19070) + self.assertEqual(urlize('see test[at[example.com'), + 'see <a href="http://test[at[example.com" rel="nofollow">test[at[example.com</a>' ) + def test_wordcount(self): self.assertEqual(wordcount(''), 0) |
