fec11f
From d5e3238b87fc557600618f18179e821a4a1c7577 Mon Sep 17 00:00:00 2001
fec11f
From: Lumir Balhar <lbalhar@redhat.com>
fec11f
Date: Tue, 29 Jun 2021 16:03:37 +0200
fec11f
Subject: [PATCH] CVE-2021-33503
fec11f
fec11f
---
fec11f
 src/urllib3/util/url.py |  8 +++++---
fec11f
 test/test_util.py       | 10 ++++++++++
fec11f
 2 files changed, 15 insertions(+), 3 deletions(-)
fec11f
fec11f
diff --git a/src/urllib3/util/url.py b/src/urllib3/util/url.py
fec11f
index 8ef5a23..7fb2650 100644
fec11f
--- a/src/urllib3/util/url.py
fec11f
+++ b/src/urllib3/util/url.py
fec11f
@@ -63,12 +63,12 @@ IPV6_ADDRZ_RE = re.compile("^" + IPV6_ADDRZ_PAT + "$")
fec11f
 BRACELESS_IPV6_ADDRZ_RE = re.compile("^" + IPV6_ADDRZ_PAT[2:-2] + "$")
fec11f
 ZONE_ID_RE = re.compile("(" + ZONE_ID_PAT + r")\]$")
fec11f
 
fec11f
-SUBAUTHORITY_PAT = (u"^(?:(.*)@)?(%s|%s|%s)(?::([0-9]{0,5}))?$") % (
fec11f
+_HOST_PORT_PAT = ("^(%s|%s|%s)(?::([0-9]{0,5}))?$") % (
fec11f
     REG_NAME_PAT,
fec11f
     IPV4_PAT,
fec11f
     IPV6_ADDRZ_PAT,
fec11f
 )
fec11f
-SUBAUTHORITY_RE = re.compile(SUBAUTHORITY_PAT, re.UNICODE | re.DOTALL)
fec11f
+_HOST_PORT_RE = re.compile(_HOST_PORT_PAT, re.UNICODE | re.DOTALL)
fec11f
 
fec11f
 UNRESERVED_CHARS = set(
fec11f
     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-~"
fec11f
@@ -365,7 +365,9 @@ def parse_url(url):
fec11f
             scheme = scheme.lower()
fec11f
 
fec11f
         if authority:
fec11f
-            auth, host, port = SUBAUTHORITY_RE.match(authority).groups()
fec11f
+            auth, _, host_port = authority.rpartition("@")
fec11f
+            auth = auth or None
fec11f
+            host, port = _HOST_PORT_RE.match(host_port).groups()
fec11f
             if auth and normalize_uri:
fec11f
                 auth = _encode_invalid_chars(auth, USERINFO_CHARS)
fec11f
             if port == "":
fec11f
diff --git a/test/test_util.py b/test/test_util.py
fec11f
index 42c3882..04c90b0 100644
fec11f
--- a/test/test_util.py
fec11f
+++ b/test/test_util.py
fec11f
@@ -425,6 +425,16 @@ class TestUtil(object):
fec11f
                 query="%0D%0ASET%20test%20failure12%0D%0A:8080/test/?test=a",
fec11f
             ),
fec11f
         ),
fec11f
+        # Tons of '@' causing backtracking
fec11f
+        ("https://" + ("@" * 10000) + "[", False),
fec11f
+        (
fec11f
+            "https://user:" + ("@" * 10000) + "example.com",
fec11f
+            Url(
fec11f
+                scheme="https",
fec11f
+                auth="user:" + ("%40" * 9999),
fec11f
+                host="example.com",
fec11f
+            ),
fec11f
+        ),
fec11f
     ]
fec11f
 
fec11f
     @pytest.mark.parametrize("url, expected_url", url_vulnerabilities)
fec11f
-- 
fec11f
2.31.1
fec11f