|
|
763158 |
diff --git a/Doc/library/urlparse.rst b/Doc/library/urlparse.rst
|
|
|
763158 |
index efd112d..61022f7 100644
|
|
|
763158 |
--- a/Doc/library/urlparse.rst
|
|
|
763158 |
+++ b/Doc/library/urlparse.rst
|
|
|
763158 |
@@ -118,6 +118,12 @@ The :mod:`urlparse` module defines the following functions:
|
|
|
763158 |
See section :ref:`urlparse-result-object` for more information on the result
|
|
|
763158 |
object.
|
|
|
763158 |
|
|
|
763158 |
+ Characters in the :attr:`netloc` attribute that decompose under NFKC
|
|
|
763158 |
+ normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
|
|
|
763158 |
+ ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
|
|
|
763158 |
+ decomposed before parsing, or is not a Unicode string, no error will be
|
|
|
763158 |
+ raised.
|
|
|
763158 |
+
|
|
|
763158 |
.. versionchanged:: 2.5
|
|
|
763158 |
Added attributes to return value.
|
|
|
763158 |
|
|
|
763158 |
@@ -125,6 +131,11 @@ The :mod:`urlparse` module defines the following functions:
|
|
|
763158 |
Added IPv6 URL parsing capabilities.
|
|
|
763158 |
|
|
|
763158 |
|
|
|
763158 |
+ .. versionchanged:: 2.7.17
|
|
|
763158 |
+ Characters that affect netloc parsing under NFKC normalization will
|
|
|
763158 |
+ now raise :exc:`ValueError`.
|
|
|
763158 |
+
|
|
|
763158 |
+
|
|
|
763158 |
.. function:: parse_qs(qs[, keep_blank_values[, strict_parsing]])
|
|
|
763158 |
|
|
|
763158 |
Parse a query string given as a string argument (data of type
|
|
|
763158 |
@@ -219,11 +230,21 @@ The :mod:`urlparse` module defines the following functions:
|
|
|
763158 |
See section :ref:`urlparse-result-object` for more information on the result
|
|
|
763158 |
object.
|
|
|
763158 |
|
|
|
763158 |
+ Characters in the :attr:`netloc` attribute that decompose under NFKC
|
|
|
763158 |
+ normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
|
|
|
763158 |
+ ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
|
|
|
763158 |
+ decomposed before parsing, or is not a Unicode string, no error will be
|
|
|
763158 |
+ raised.
|
|
|
763158 |
+
|
|
|
763158 |
.. versionadded:: 2.2
|
|
|
763158 |
|
|
|
763158 |
.. versionchanged:: 2.5
|
|
|
763158 |
Added attributes to return value.
|
|
|
763158 |
|
|
|
763158 |
+ .. versionchanged:: 2.7.17
|
|
|
763158 |
+ Characters that affect netloc parsing under NFKC normalization will
|
|
|
763158 |
+ now raise :exc:`ValueError`.
|
|
|
763158 |
+
|
|
|
763158 |
|
|
|
763158 |
.. function:: urlunsplit(parts)
|
|
|
763158 |
|
|
|
763158 |
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
|
|
|
b6b16a |
index 72ebfaa..2717163 100644
|
|
|
763158 |
--- a/Lib/test/test_urlparse.py
|
|
|
763158 |
+++ b/Lib/test/test_urlparse.py
|
|
|
763158 |
@@ -1,6 +1,8 @@
|
|
|
763158 |
#! /usr/bin/env python
|
|
|
763158 |
|
|
|
763158 |
from test import test_support
|
|
|
763158 |
+import sys
|
|
|
763158 |
+import unicodedata
|
|
|
763158 |
import unittest
|
|
|
763158 |
import urlparse
|
|
|
763158 |
|
|
|
b6b16a |
@@ -564,6 +566,45 @@ class UrlParseTestCase(unittest.TestCase):
|
|
|
763158 |
self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
|
|
|
763158 |
('http','www.python.org:80','','','',''))
|
|
|
763158 |
|
|
|
763158 |
+ def test_urlsplit_normalization(self):
|
|
|
763158 |
+ # Certain characters should never occur in the netloc,
|
|
|
763158 |
+ # including under normalization.
|
|
|
763158 |
+ # Ensure that ALL of them are detected and cause an error
|
|
|
763158 |
+ illegal_chars = u'/:#?@'
|
|
|
763158 |
+ hex_chars = {'{:04X}'.format(ord(c)) for c in illegal_chars}
|
|
|
763158 |
+ denorm_chars = [
|
|
|
763158 |
+ c for c in map(unichr, range(128, sys.maxunicode))
|
|
|
763158 |
+ if (hex_chars & set(unicodedata.decomposition(c).split()))
|
|
|
763158 |
+ and c not in illegal_chars
|
|
|
763158 |
+ ]
|
|
|
763158 |
+ # Sanity check that we found at least one such character
|
|
|
763158 |
+ self.assertIn(u'\u2100', denorm_chars)
|
|
|
763158 |
+ self.assertIn(u'\uFF03', denorm_chars)
|
|
|
763158 |
+
|
|
|
763158 |
+ # bpo-36742: Verify port separators are ignored when they
|
|
|
763158 |
+ # existed prior to decomposition
|
|
|
763158 |
+ urlparse.urlsplit(u'http://\u30d5\u309a:80')
|
|
|
763158 |
+ with self.assertRaises(ValueError):
|
|
|
763158 |
+ urlparse.urlsplit(u'http://\u30d5\u309a\ufe1380')
|
|
|
763158 |
+
|
|
|
763158 |
+ for scheme in [u"http", u"https", u"ftp"]:
|
|
|
b6b16a |
+ for netloc in [u"netloc{}false.netloc", u"n{}user@netloc"]:
|
|
|
b6b16a |
+ for c in denorm_chars:
|
|
|
b6b16a |
+ url = u"{}://{}/path".format(scheme, netloc.format(c))
|
|
|
b6b16a |
+ if test_support.verbose:
|
|
|
b6b16a |
+ print "Checking %r" % url
|
|
|
b6b16a |
+ with self.assertRaises(ValueError):
|
|
|
b6b16a |
+ urlparse.urlsplit(url)
|
|
|
b6b16a |
+
|
|
|
b6b16a |
+ # check error message: invalid netloc must be formated with repr()
|
|
|
b6b16a |
+ # to get an ASCII error message
|
|
|
b6b16a |
+ with self.assertRaises(ValueError) as cm:
|
|
|
b6b16a |
+ urlparse.urlsplit(u'http://example.com\uFF03@bing.com')
|
|
|
b6b16a |
+ self.assertEqual(str(cm.exception),
|
|
|
b6b16a |
+ "netloc u'example.com\\uff03@bing.com' contains invalid characters "
|
|
|
b6b16a |
+ "under NFKC normalization")
|
|
|
b6b16a |
+ self.assertIsInstance(cm.exception.args[0], str)
|
|
|
763158 |
+
|
|
|
763158 |
def test_main():
|
|
|
763158 |
test_support.run_unittest(UrlParseTestCase)
|
|
|
763158 |
|
|
|
763158 |
diff --git a/Lib/urlparse.py b/Lib/urlparse.py
|
|
|
b6b16a |
index 4ce982e..9a1df74 100644
|
|
|
763158 |
--- a/Lib/urlparse.py
|
|
|
763158 |
+++ b/Lib/urlparse.py
|
|
|
b6b16a |
@@ -164,6 +164,25 @@ def _splitnetloc(url, start=0):
|
|
|
763158 |
delim = min(delim, wdelim) # use earliest delim position
|
|
|
763158 |
return url[start:delim], url[delim:] # return (domain, rest)
|
|
|
763158 |
|
|
|
763158 |
+def _checknetloc(netloc):
|
|
|
763158 |
+ if not netloc or not isinstance(netloc, unicode):
|
|
|
763158 |
+ return
|
|
|
763158 |
+ # looking for characters like \u2100 that expand to 'a/c'
|
|
|
763158 |
+ # IDNA uses NFKC equivalence, so normalize for this check
|
|
|
763158 |
+ import unicodedata
|
|
|
b6b16a |
+ n = netloc.replace(u'@', u'') # ignore characters already included
|
|
|
b6b16a |
+ n = n.replace(u':', u'') # but not the surrounding text
|
|
|
b6b16a |
+ n = n.replace(u'#', u'')
|
|
|
b6b16a |
+ n = n.replace(u'?', u'')
|
|
|
763158 |
+ netloc2 = unicodedata.normalize('NFKC', n)
|
|
|
763158 |
+ if n == netloc2:
|
|
|
763158 |
+ return
|
|
|
763158 |
+ for c in '/?#@:':
|
|
|
763158 |
+ if c in netloc2:
|
|
|
b6b16a |
+ raise ValueError("netloc %r contains invalid characters "
|
|
|
b6b16a |
+ "under NFKC normalization"
|
|
|
b6b16a |
+ % netloc)
|
|
|
763158 |
+
|
|
|
763158 |
def urlsplit(url, scheme='', allow_fragments=True):
|
|
|
763158 |
"""Parse a URL into 5 components:
|
|
|
763158 |
<scheme>://<netloc>/<path>?<query>#<fragment>
|
|
|
b6b16a |
@@ -192,6 +211,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
|
|
|
763158 |
url, fragment = url.split('#', 1)
|
|
|
763158 |
if '?' in url:
|
|
|
763158 |
url, query = url.split('?', 1)
|
|
|
763158 |
+ _checknetloc(netloc)
|
|
|
763158 |
v = SplitResult(scheme, netloc, url, query, fragment)
|
|
|
763158 |
_parse_cache[key] = v
|
|
|
763158 |
return v
|
|
|
b6b16a |
@@ -215,6 +235,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
|
|
|
763158 |
url, fragment = url.split('#', 1)
|
|
|
763158 |
if '?' in url:
|
|
|
763158 |
url, query = url.split('?', 1)
|
|
|
763158 |
+ _checknetloc(netloc)
|
|
|
763158 |
v = SplitResult(scheme, netloc, url, query, fragment)
|
|
|
763158 |
_parse_cache[key] = v
|
|
|
763158 |
return v
|