ccbe51
From 2d4a3fee6de2fa45eb82169361918f759269b4ec Mon Sep 17 00:00:00 2001
ccbe51
From: Seth Michael Larson <sethmichaellarson@gmail.com>
ccbe51
Date: Wed, 26 May 2021 10:43:12 -0500
ccbe51
Subject: [PATCH] Improve performance of sub-authority splitting in URL
ccbe51
ccbe51
---
ccbe51
 src/urllib3/util/url.py |  8 +++++---
ccbe51
 test/test_util.py       | 10 ++++++++++
ccbe51
 2 files changed, 15 insertions(+), 3 deletions(-)
ccbe51
ccbe51
diff --git a/src/urllib3/util/url.py b/src/urllib3/util/url.py
ccbe51
index 6ff238fe3c..81a03da9e3 100644
ccbe51
--- a/src/urllib3/util/url.py
ccbe51
+++ b/src/urllib3/util/url.py
ccbe51
@@ -63,12 +63,12 @@
ccbe51
 BRACELESS_IPV6_ADDRZ_RE = re.compile("^" + IPV6_ADDRZ_PAT[2:-2] + "$")
ccbe51
 ZONE_ID_RE = re.compile("(" + ZONE_ID_PAT + r")\]$")
ccbe51
 
ccbe51
-SUBAUTHORITY_PAT = (u"^(?:(.*)@)?(%s|%s|%s)(?::([0-9]{0,5}))?$") % (
ccbe51
+_HOST_PORT_PAT = ("^(%s|%s|%s)(?::([0-9]{0,5}))?$") % (
ccbe51
     REG_NAME_PAT,
ccbe51
     IPV4_PAT,
ccbe51
     IPV6_ADDRZ_PAT,
ccbe51
 )
ccbe51
-SUBAUTHORITY_RE = re.compile(SUBAUTHORITY_PAT, re.UNICODE | re.DOTALL)
ccbe51
+_HOST_PORT_RE = re.compile(_HOST_PORT_PAT, re.UNICODE | re.DOTALL)
ccbe51
 
ccbe51
 UNRESERVED_CHARS = set(
ccbe51
     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-~"
ccbe51
@@ -365,7 +365,9 @@ def parse_url(url):
ccbe51
             scheme = scheme.lower()
ccbe51
 
ccbe51
         if authority:
ccbe51
-            auth, host, port = SUBAUTHORITY_RE.match(authority).groups()
ccbe51
+            auth, _, host_port = authority.rpartition("@")
ccbe51
+            auth = auth or None
ccbe51
+            host, port = _HOST_PORT_RE.match(host_port).groups()
ccbe51
             if auth and normalize_uri:
ccbe51
                 auth = _encode_invalid_chars(auth, USERINFO_CHARS)
ccbe51
             if port == "":
ccbe51
diff --git a/test/test_util.py b/test/test_util.py
ccbe51
index a5b68a084b..88409e2d6c 100644
ccbe51
--- a/test/test_util.py
ccbe51
+++ b/test/test_util.py
ccbe51
@@ -438,6 +438,16 @@ def test_netloc(self, url, expected_netloc):
ccbe51
                 fragment="hash",
ccbe51
             ),
ccbe51
         ),
ccbe51
+        # Tons of '@' causing backtracking
ccbe51
+        ("https://" + ("@" * 10000) + "[", False),
ccbe51
+        (
ccbe51
+            "https://user:" + ("@" * 10000) + "example.com",
ccbe51
+            Url(
ccbe51
+                scheme="https",
ccbe51
+                auth="user:" + ("%40" * 9999),
ccbe51
+                host="example.com",
ccbe51
+            ),
ccbe51
+        ),
ccbe51
     ]
ccbe51
 
ccbe51
     @pytest.mark.parametrize("url, expected_url", url_vulnerabilities)