diff options
author | Steve Dower <steve.dower@python.org> | 2019-07-14 08:16:19 (GMT) |
---|---|---|
committer | larryhastings <larry@hastings.org> | 2019-07-14 08:16:19 (GMT) |
commit | 4655d576141ee56a69d2052431c636858fcb916a (patch) | |
tree | afaa4fb3311330dfe5d706e815f5cc328a91230e | |
parent | 4fe82a8eef7aed60de05bfca0f2c322730ea921e (diff) | |
download | cpython-4655d576141ee56a69d2052431c636858fcb916a.zip cpython-4655d576141ee56a69d2052431c636858fcb916a.tar.gz cpython-4655d576141ee56a69d2052431c636858fcb916a.tar.bz2 |
bpo-36742: Fixes handling of pre-normalization characters in urlsplit() (GH-13017) (#13042)
-rw-r--r-- | Lib/test/test_urlparse.py | 6 | ||||
-rw-r--r-- | Lib/urllib/parse.py | 11 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst | 1 |
3 files changed, 14 insertions, 4 deletions
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index d0420b0..1e90e18 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -987,6 +987,12 @@ 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) diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 7ba2b44..7405d66 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -333,13 +333,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 index 0000000..d729ed2 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst @@ -0,0 +1 @@ +Fixes mishandling of pre-normalization characters in urlsplit(). |