Strip leading zeros from ports
ADD
changelog/2804.bugfix.rst
+Fixed issue in parse_url that rejected ASCII digits with more than 5 characters
+(i.e. lots of leading zeros but still <= 65535).

MODIFY
src/urllib3/util/url.py
src/urllib3/util/url.py
@@ -63,7 +63,7 @@ _IPV6_ADDRZ_RE = re.compile("^" + _IPV6_ADDRZ_PAT + "$")
_BRACELESS_IPV6_ADDRZ_RE = re.compile("^" + _IPV6_ADDRZ_PAT[2:-2] + "$")
_ZONE_ID_RE = re.compile("(" + _ZONE_ID_PAT + r")\]$")
-_HOST_PORT_PAT = ("^(%s|%s|%s)(?::([0-9]{0,5}))?$") % (
+_HOST_PORT_PAT = ("^(%s|%s|%s)(?::0*([0-9]{0,5}))?$") % (
_REG_NAME_PAT,
_IPV4_PAT,
_IPV6_ADDRZ_PAT,

MODIFY
test/test_util.py
test/test_util.py
@@ -348,6 +348,10 @@ class TestUtil:
with pytest.raises(LocationParseError):
parse_url("https://www.google.com:-80/")
+ def test_parse_url_remove_leading_zeros(self) -> None:
+ url = parse_url("https://example.com:0000000000080")
+ assert url.port == 80
+
def test_Url_str(self) -> None:
U = Url("http", host="google.com")
assert str(U) == U.url

Strip leading zeros from ports