From e5f9f4adb95233c66578e6f7ea176687af2f78ca Mon Sep 17 00:00:00 2001
From: "Miss Islington (bot)"
 <31488909+miss-islington@users.noreply.github.com>
Date: Thu, 2 May 2019 09:02:35 -0700
Subject: [PATCH] bpo-36742: Fixes handling of pre-normalization characters in
 urlsplit() (GH-13017) (GH-13024)

(cherry picked from commit d537ab0ff9767ef024f26246899728f0116b1ec3)

Co-authored-by: Steve Dower <steve.dower@python.org>
---
 Lib/test/test_urlparse.py                             |  6 ++++++
 Lib/urllib/parse.py                                   | 11 +++++++----
 .../Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst |  1 +
 3 files changed, 14 insertions(+), 4 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst

From fd1771dbdd28709716bd531580c40ae5ed814468 Mon Sep 17 00:00:00 2001
From: "Miss Islington (bot)"
 <31488909+miss-islington@users.noreply.github.com>
Date: Tue, 4 Jun 2019 11:43:52 -0700
Subject: [PATCH] bpo-36742: Corrects fix to handle decomposition in usernames
 (GH-13812) (GH-13814)

(cherry picked from commit 8d0ef0b5edeae52960c7ed05ae8a12388324f87e)

Co-authored-by: Steve Dower <steve.dower@python.org>
---
 Lib/test/test_urlparse.py | 11 ++++++-----
 Lib/urllib/parse.py       |  6 +++---
 2 files changed, 9 insertions(+), 8 deletions(-)


--- a/Lib/test/test_urlparse.py	2019-06-17 18:25:59.209410304 +0530
+++ b/Lib/test/test_urlparse.py	2019-06-17 18:32:10.557417721 +0530
@@ -994,12 +994,19 @@ class UrlParseTestCase(unittest.TestCase
         self.assertIn('\u2100', denorm_chars)
         self.assertIn('\uFF03', denorm_chars)
 
+        # bpo-36742: Verify port separators are ignored when they
+        # existed prior to decomposition
+        urllib.parse.urlsplit('http://\u30d5\u309a:80')
+        with self.assertRaises(ValueError):
+            urllib.parse.urlsplit('http://\u30d5\u309a\ufe1380')
+
         for scheme in ["http", "https", "ftp"]:
-            for c in denorm_chars:
-                url = "{}://netloc{}false.netloc/path".format(scheme, c)
-                with self.subTest(url=url, char='{:04X}'.format(ord(c))):
-                    with self.assertRaises(ValueError):
-                        urllib.parse.urlsplit(url)
+            for netloc in ["netloc{}false.netloc", "n{}user@netloc"]:
+                for c in denorm_chars:
+                    url = "{}://{}/path".format(scheme, netloc.format(c))
+                    with self.subTest(url=url, char='{:04X}'.format(ord(c))):
+                        with self.assertRaises(ValueError):
+                            urllib.parse.urlsplit(url)
 
 class Utility_Tests(unittest.TestCase):
     """Testcase to test the various utility functions in the urllib."""
--- a/Lib/urllib/parse.py	2019-06-17 18:27:08.097411680 +0530
+++ b/Lib/urllib/parse.py	2019-06-17 18:33:46.965419647 +0530
@@ -397,13 +397,17 @@ 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.replace('@', '')   # ignore characters already included
+    n = n.replace(':', '')        # but not the surrounding text
+    n = n.replace('#', '')
+    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):
--- a/Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst_org	2019-06-17 18:29:26.413414442 +0530
+++ b/Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst	2019-06-17 18:29:39.205414698 +0530
@@ -0,0 +1 @@
+Fixes mishandling of pre-normalization characters in urlsplit().