2021-06-01 12:22:35 +08:00
|
|
|
From 8d65ea1ecf6e2cdc27d42124e587c1b83a3118b0 Mon Sep 17 00:00:00 2001
|
|
|
|
|
From: Jorge <JALopezSilva@gmail.com>
|
|
|
|
|
Date: Mon, 15 Mar 2021 06:49:49 -0700
|
|
|
|
|
Subject: [PATCH] Merge pull request from GHSA-5phf-pp7p-vc2r
|
|
|
|
|
|
|
|
|
|
* Enable hostname verification for HTTPS proxies with default cert.
|
|
|
|
|
|
|
|
|
|
Signed-off-by: Jorge Lopez Silva <jalopezsilva@gmail.com>
|
|
|
|
|
|
|
|
|
|
* Adjust exception check for Python 3.9+
|
|
|
|
|
|
|
|
|
|
Signed-off-by: Jorge Lopez Silva <jalopezsilva@gmail.com>
|
|
|
|
|
|
|
|
|
|
* Use a SAN instead of a common name.
|
|
|
|
|
|
|
|
|
|
Signed-off-by: Jorge Lopez Silva <jalopezsilva@gmail.com>
|
|
|
|
|
---
|
|
|
|
|
src/urllib3/connection.py | 4 ++++
|
|
|
|
|
test/conftest.py | 11 ++++++++++
|
2022-01-19 14:20:17 +08:00
|
|
|
.../test_proxy_poolmanager.py | 20 +++++++++++++++++++
|
|
|
|
|
3 files changed, 35 insertions(+)
|
2021-06-01 12:22:35 +08:00
|
|
|
|
|
|
|
|
diff --git a/src/urllib3/connection.py b/src/urllib3/connection.py
|
2022-01-19 14:20:17 +08:00
|
|
|
index 60f70f7..f59f29b 100644
|
2021-06-01 12:22:35 +08:00
|
|
|
--- a/src/urllib3/connection.py
|
|
|
|
|
+++ b/src/urllib3/connection.py
|
2022-01-19 14:20:17 +08:00
|
|
|
@@ -495,6 +495,10 @@ class HTTPSConnection(HTTPConnection):
|
2021-06-01 12:22:35 +08:00
|
|
|
self.ca_cert_dir,
|
|
|
|
|
self.ca_cert_data,
|
|
|
|
|
)
|
|
|
|
|
+ # By default urllib3's SSLContext disables `check_hostname` and uses
|
|
|
|
|
+ # a custom check. For proxies we're good with relying on the default
|
|
|
|
|
+ # verification.
|
|
|
|
|
+ ssl_context.check_hostname = True
|
|
|
|
|
|
|
|
|
|
# If no cert was provided, use only the default options for server
|
|
|
|
|
# certificate validation
|
|
|
|
|
diff --git a/test/conftest.py b/test/conftest.py
|
2022-01-19 14:20:17 +08:00
|
|
|
index 10c3a54..d4bbd97 100644
|
2021-06-01 12:22:35 +08:00
|
|
|
--- a/test/conftest.py
|
|
|
|
|
+++ b/test/conftest.py
|
2022-01-19 14:20:17 +08:00
|
|
|
@@ -103,6 +103,17 @@ def no_san_server(tmp_path_factory):
|
2021-06-01 12:22:35 +08:00
|
|
|
yield cfg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+@pytest.fixture
|
|
|
|
|
+def no_localhost_san_server(tmp_path_factory):
|
|
|
|
|
+ tmpdir = tmp_path_factory.mktemp("certs")
|
|
|
|
|
+ ca = trustme.CA()
|
|
|
|
|
+ # non localhost common name
|
|
|
|
|
+ server_cert = ca.issue_cert(u"example.com")
|
|
|
|
|
+
|
|
|
|
|
+ with run_server_in_thread("https", "localhost", tmpdir, ca, server_cert) as cfg:
|
|
|
|
|
+ yield cfg
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
@pytest.fixture
|
2022-01-19 14:20:17 +08:00
|
|
|
def no_san_proxy(tmp_path_factory):
|
2021-06-01 12:22:35 +08:00
|
|
|
tmpdir = tmp_path_factory.mktemp("certs")
|
|
|
|
|
diff --git a/test/with_dummyserver/test_proxy_poolmanager.py b/test/with_dummyserver/test_proxy_poolmanager.py
|
2022-01-19 14:20:17 +08:00
|
|
|
index d5e91a0..0f8df60 100644
|
2021-06-01 12:22:35 +08:00
|
|
|
--- a/test/with_dummyserver/test_proxy_poolmanager.py
|
|
|
|
|
+++ b/test/with_dummyserver/test_proxy_poolmanager.py
|
2022-01-19 14:20:17 +08:00
|
|
|
@@ -565,6 +565,26 @@ class TestIPv6HTTPProxyManager(IPv6HTTPDummyProxyTestCase):
|
2021-06-01 12:22:35 +08:00
|
|
|
r = http.request("GET", "%s/" % self.https_url)
|
|
|
|
|
assert r.status == 200
|
2022-01-19 14:20:17 +08:00
|
|
|
|
2021-06-01 12:22:35 +08:00
|
|
|
+class TestHTTPSProxyVerification:
|
|
|
|
|
+ @onlyPy3
|
|
|
|
|
+ def test_https_proxy_hostname_verification(self, no_localhost_san_server):
|
|
|
|
|
+ bad_server = no_localhost_san_server
|
|
|
|
|
+ bad_proxy_url = "https://%s:%s" % (bad_server.host, bad_server.port)
|
|
|
|
|
+
|
|
|
|
|
+ # An exception will be raised before we contact the destination domain.
|
|
|
|
|
+ test_url = "testing.com"
|
|
|
|
|
+ with proxy_from_url(bad_proxy_url, ca_certs=bad_server.ca_certs) as https:
|
|
|
|
|
+ with pytest.raises(MaxRetryError) as e:
|
|
|
|
|
+ https.request("GET", "http://%s/" % test_url)
|
|
|
|
|
+ assert isinstance(e.value.reason, SSLError)
|
|
|
|
|
+ assert "hostname 'localhost' doesn't match" in str(e.value.reason)
|
|
|
|
|
+
|
|
|
|
|
+ with pytest.raises(MaxRetryError) as e:
|
|
|
|
|
+ https.request("GET", "https://%s/" % test_url)
|
|
|
|
|
+ assert isinstance(e.value.reason, SSLError)
|
|
|
|
|
+ assert "hostname 'localhost' doesn't match" in str(
|
|
|
|
|
+ e.value.reason
|
|
|
|
|
+ ) or "Hostname mismatch" in str(e.value.reason)
|
2022-01-19 14:20:17 +08:00
|
|
|
|
|
|
|
|
class TestHTTPSProxyVerification:
|
|
|
|
|
@onlyPy3
|
|
|
|
|
--
|
|
|
|
|
2.27.0
|
|
|
|
|
|