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