Skip to content

Commit e5f9f4a

Browse files
miss-islingtonzooba
authored andcommitted
bpo-36742: Fixes handling of pre-normalization characters in urlsplit() (GH-13017) (GH-13024)
(cherry picked from commit d537ab0) Co-authored-by: Steve Dower <steve.dower@python.org>
1 parent dadc347 commit e5f9f4a

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
@@ -1001,6 +1001,12 @@ def test_urlsplit_normalization(self):
10011001
self.assertIn('\u2100', denorm_chars)
10021002
self.assertIn('\uFF03', denorm_chars)
10031003

1004+
# bpo-36742: Verify port separators are ignored when they
1005+
# existed prior to decomposition
1006+
urllib.parse.urlsplit('http://\u30d5\u309a:80')
1007+
with self.assertRaises(ValueError):
1008+
urllib.parse.urlsplit('http://\u30d5\u309a\ufe1380')
1009+
10041010
for scheme in ["http", "https", "ftp"]:
10051011
for c in denorm_chars:
10061012
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
@@ -397,13 +397,16 @@ def _checknetloc(netloc):
397397
# looking for characters like \u2100 that expand to 'a/c'
398398
# IDNA uses NFKC equivalence, so normalize for this check
399399
import unicodedata
400-
netloc2 = unicodedata.normalize('NFKC', netloc)
401-
if netloc == netloc2:
400+
n = netloc.rpartition('@')[2] # ignore anything to the left of '@'
401+
n = n.replace(':', '') # ignore characters already included
402+
n = n.replace('#', '') # but not the surrounding text
403+
n = n.replace('?', '')
404+
netloc2 = unicodedata.normalize('NFKC', n)
405+
if n == netloc2:
402406
return
403-
_, _, netloc = netloc.rpartition('@') # anything to the left of '@' is okay
404407
for c in '/?#@:':
405408
if c in netloc2:
406-
raise ValueError("netloc '" + netloc2 + "' contains invalid " +
409+
raise ValueError("netloc '" + netloc + "' contains invalid " +
407410
"characters under NFKC normalization")
408411

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