]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36742: Fixes handling of pre-normalization characters in urlsplit() (GH-13017)
authorSteve Dower <steve.dower@python.org>
Wed, 1 May 2019 15:00:27 +0000 (15:00 +0000)
committerGitHub <noreply@github.com>
Wed, 1 May 2019 15:00:27 +0000 (15:00 +0000)
Lib/test/test_urlparse.py
Lib/urlparse.py
Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst [new file with mode: 0644]

index 1830d0b28688d55bccecb90fa7f78dd6aa79c81b..6fd1071bf7cdec11f3e374cd01bb2d6e7b4a867f 100644 (file)
@@ -641,6 +641,12 @@ class UrlParseTestCase(unittest.TestCase):
         self.assertIn(u'\u2100', denorm_chars)
         self.assertIn(u'\uFF03', denorm_chars)
 
+        # bpo-36742: Verify port separators are ignored when they
+        # existed prior to decomposition
+        urlparse.urlsplit(u'http://\u30d5\u309a:80')
+        with self.assertRaises(ValueError):
+            urlparse.urlsplit(u'http://\u30d5\u309a\ufe1380')
+
         for scheme in [u"http", u"https", u"ftp"]:
             for c in denorm_chars:
                 url = u"{}://netloc{}false.netloc/path".format(scheme, c)
index 54eda08651ab95373addbf7b42bfab86fe473afa..f08e0fe58432cef3abfd0309ceffa5f55b40292c 100644 (file)
@@ -171,13 +171,16 @@ def _checknetloc(netloc):
     # looking for characters like \u2100 that expand to 'a/c'
     # IDNA uses NFKC equivalence, so normalize for this check
     import unicodedata
-    netloc2 = unicodedata.normalize('NFKC', netloc)
-    if netloc == netloc2:
+    n = netloc.rpartition('@')[2] # ignore anything to the left of '@'
+    n = n.replace(':', '')        # ignore characters already included
+    n = n.replace('#', '')        # but not the surrounding text
+    n = n.replace('?', '')
+    netloc2 = unicodedata.normalize('NFKC', n)
+    if n == netloc2:
         return
-    _, _, netloc = netloc.rpartition('@') # anything to the left of '@' is okay
     for c in '/?#@:':
         if c in netloc2:
-            raise ValueError("netloc '" + netloc2 + "' contains invalid " +
+            raise ValueError("netloc '" + netloc + "' contains invalid " +
                              "characters under NFKC normalization")
 
 def urlsplit(url, scheme='', allow_fragments=True):
diff --git a/Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst b/Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst
new file mode 100644 (file)
index 0000000..d729ed2
--- /dev/null
@@ -0,0 +1 @@
+Fixes mishandling of pre-normalization characters in urlsplit().