Skip to content

Commit 4655d57

Browse files
zoobalarryhastings
authored andcommitted
bpo-36742: Fixes handling of pre-normalization characters in urlsplit() (GH-13017) (#13042)
1 parent 4fe82a8 commit 4655d57

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Lib/test/test_urlparse.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,12 @@ def test_urlsplit_normalization(self):
987987
self.assertIn('\u2100', denorm_chars)
988988
self.assertIn('\uFF03', denorm_chars)
989989

990+
# bpo-36742: Verify port separators are ignored when they
991+
# existed prior to decomposition
992+
urllib.parse.urlsplit('http://\u30d5\u309a:80')
993+
with self.assertRaises(ValueError):
994+
urllib.parse.urlsplit('http://\u30d5\u309a\ufe1380')
995+
990996
for scheme in ["http", "https", "ftp"]:
991997
for c in denorm_chars:
992998
url = "{}://netloc{}false.netloc/path".format(scheme, c)

Lib/urllib/parse.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,16 @@ def _checknetloc(netloc):
333333
# looking for characters like \u2100 that expand to 'a/c'
334334
# IDNA uses NFKC equivalence, so normalize for this check
335335
import unicodedata
336-
netloc2 = unicodedata.normalize('NFKC', netloc)
337-
if netloc == netloc2:
336+
n = netloc.rpartition('@')[2] # ignore anything to the left of '@'
337+
n = n.replace(':', '') # ignore characters already included
338+
n = n.replace('#', '') # but not the surrounding text
339+
n = n.replace('?', '')
340+
netloc2 = unicodedata.normalize('NFKC', n)
341+
if n == netloc2:
338342
return
339-
_, _, netloc = netloc.rpartition('@') # anything to the left of '@' is okay
340343
for c in '/?#@:':
341344
if c in netloc2:
342-
raise ValueError("netloc '" + netloc2 + "' contains invalid " +
345+
raise ValueError("netloc '" + netloc + "' contains invalid " +
343346
"characters under NFKC normalization")
344347

345348
def urlsplit(url, scheme='', allow_fragments=True):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes mishandling of pre-normalization characters in urlsplit().

0 commit comments

Comments
 (0)