Skip to content

Commit 8ba4d66

Browse files
miss-islingtonkenballus
authored andcommitted
pythongh-99418: Make urllib.parse.urlparse enforce that a scheme must begin with an alphabetical ASCII character. (pythonGH-99421)
Prevent urllib.parse.urlparse from accepting schemes that don't begin with an alphabetical ASCII character. RFC 3986 defines a scheme like this: `scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )` RFC 2234 defines an ALPHA like this: `ALPHA = %x41-5A / %x61-7A` The WHATWG URL spec defines a scheme like this: `"A URL-scheme string must be one ASCII alpha, followed by zero or more of ASCII alphanumeric, U+002B (+), U+002D (-), and U+002E (.)."` (cherry picked from commit 439b9cf) Co-authored-by: Ben Kallus <[email protected]>
1 parent db405f8 commit 8ba4d66

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Lib/test/test_urlparse.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,24 @@ def test_attributes_bad_port(self):
788788
with self.assertRaises(ValueError):
789789
p.port
790790

791+
def test_attributes_bad_scheme(self):
792+
"""Check handling of invalid schemes."""
793+
for bytes in (False, True):
794+
for parse in (urllib.parse.urlsplit, urllib.parse.urlparse):
795+
for scheme in (".", "+", "-", "0", "http&", "६http"):
796+
with self.subTest(bytes=bytes, parse=parse, scheme=scheme):
797+
url = scheme + "://www.example.net"
798+
if bytes:
799+
if url.isascii():
800+
url = url.encode("ascii")
801+
else:
802+
continue
803+
p = parse(url)
804+
if bytes:
805+
self.assertEqual(p.scheme, b"")
806+
else:
807+
self.assertEqual(p.scheme, "")
808+
791809
def test_attributes_without_netloc(self):
792810
# This example is straight from RFC 3261. It looks like it
793811
# should allow the username, hostname, and port to be filled

Lib/urllib/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
482482
clear_cache()
483483
netloc = query = fragment = ''
484484
i = url.find(':')
485-
if i > 0:
485+
if i > 0 and url[0].isascii() and url[0].isalpha():
486486
for c in url[:i]:
487487
if c not in scheme_chars:
488488
break
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug in :func:`urllib.parse.urlparse` that causes URL schemes that begin
2+
with a digit, a plus sign, or a minus sign to be parsed incorrectly.

0 commit comments

Comments
 (0)