Skip to content

Commit d537ab0

Browse files
authored
bpo-36742: Fixes handling of pre-normalization characters in urlsplit() (GH-13017)
1 parent b84cb70 commit d537ab0

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
@@ -1011,6 +1011,12 @@ def test_urlsplit_normalization(self):
10111011
self.assertIn('\u2100', denorm_chars)
10121012
self.assertIn('\uFF03', denorm_chars)
10131013

1014+
# bpo-36742: Verify port separators are ignored when they
1015+
# existed prior to decomposition
1016+
urllib.parse.urlsplit('http://\u30d5\u309a:80')
1017+
with self.assertRaises(ValueError):
1018+
urllib.parse.urlsplit('http://\u30d5\u309a\ufe1380')
1019+
10141020
for scheme in ["http", "https", "ftp"]:
10151021
for c in denorm_chars:
10161022
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
@@ -402,13 +402,16 @@ def _checknetloc(netloc):
402402
# looking for characters like \u2100 that expand to 'a/c'
403403
# IDNA uses NFKC equivalence, so normalize for this check
404404
import unicodedata
405-
netloc2 = unicodedata.normalize('NFKC', netloc)
406-
if netloc == netloc2:
405+
n = netloc.rpartition('@')[2] # ignore anything to the left of '@'
406+
n = n.replace(':', '') # ignore characters already included
407+
n = n.replace('#', '') # but not the surrounding text
408+
n = n.replace('?', '')
409+
netloc2 = unicodedata.normalize('NFKC', n)
410+
if n == netloc2:
407411
return
408-
_, _, netloc = netloc.rpartition('@') # anything to the left of '@' is okay
409412
for c in '/?#@:':
410413
if c in netloc2:
411-
raise ValueError("netloc '" + netloc2 + "' contains invalid " +
414+
raise ValueError("netloc '" + netloc + "' contains invalid " +
412415
"characters under NFKC normalization")
413416

414417
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)