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