| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import binascii |
| 4 |
import contextlib |
| 5 |
import hashlib |
| 6 |
import ipaddress |
| 7 |
import os.path |
| 8 |
import pathlib |
| 9 |
import shutil |
| 10 |
import socket |
| 11 |
import ssl |
| 12 |
import tempfile |
| 13 |
from test import LONG_TIMEOUT, SHORT_TIMEOUT, resolvesLocalhostFQDN, withPyOpenSSL |
| 14 |
from test.conftest import ServerConfig |
| 15 |
|
| 16 |
import pytest |
| 17 |
import trustme |
| 18 |
|
| 19 |
import urllib3.exceptions |
| 20 |
from dummyserver.socketserver import DEFAULT_CA, HAS_IPV6, get_unreachable_address |
| 21 |
from dummyserver.testcase import ( |
| 22 |
HypercornDummyProxyTestCase, |
| 23 |
IPv6HypercornDummyProxyTestCase, |
| 24 |
) |
| 25 |
from urllib3 import HTTPResponse |
| 26 |
from urllib3._collections import HTTPHeaderDict |
| 27 |
from urllib3.connection import VerifiedHTTPSConnection |
| 28 |
from urllib3.connectionpool import connection_from_url |
| 29 |
from urllib3.exceptions import ( |
| 30 |
ConnectTimeoutError, |
| 31 |
InsecureRequestWarning, |
| 32 |
MaxRetryError, |
| 33 |
ProxyError, |
| 34 |
ProxySchemeUnknown, |
| 35 |
ProxySchemeUnsupported, |
| 36 |
ReadTimeoutError, |
| 37 |
SSLError, |
| 38 |
) |
| 39 |
from urllib3.poolmanager import ProxyManager, proxy_from_url |
| 40 |
from urllib3.util.retry import RequestHistory |
| 41 |
from urllib3.util.ssl_ import create_urllib3_context |
| 42 |
from urllib3.util.timeout import Timeout |
| 43 |
|
| 44 |
from .. import TARPIT_HOST, requires_network |
| 45 |
|
| 46 |
|
| 47 |
def assert_is_verified(pm: ProxyManager, *, proxy: bool, target: bool) -> None: |
| 48 |
pool = list(pm.pools._container.values())[-1] # retrieve last pool entry |
| 49 |
connection = ( |
| 50 |
pool.pool.queue[-1] if pool.pool is not None else None |
| 51 |
) # retrieve last connection entry |
| 52 |
|
| 53 |
assert connection is not None |
| 54 |
assert connection.proxy_is_verified is proxy |
| 55 |
assert connection.is_verified is target |
| 56 |
|
| 57 |
|
| 58 |
class TestHTTPProxyManager(HypercornDummyProxyTestCase): |
| 59 |
@classmethod |
| 60 |
def setup_class(cls) -> None: |
| 61 |
super().setup_class() |
| 62 |
cls.http_url = f"http://{cls.http_host}:{int(cls.http_port)}" |
| 63 |
cls.http_url_alt = f"http://{cls.http_host_alt}:{int(cls.http_port)}" |
| 64 |
cls.https_url = f"https://{cls.https_host}:{int(cls.https_port)}" |
| 65 |
cls.https_url_alt = f"https://{cls.https_host_alt}:{int(cls.https_port)}" |
| 66 |
cls.https_url_fqdn = f"https://{cls.https_host}.:{int(cls.https_port)}" |
| 67 |
cls.proxy_url = f"http://{cls.proxy_host}:{int(cls.proxy_port)}" |
| 68 |
cls.https_proxy_url = f"https://{cls.proxy_host}:{int(cls.https_proxy_port)}" |
| 69 |
|
| 70 |
# Generate another CA to test verification failure |
| 71 |
cls.certs_dir = tempfile.mkdtemp() |
| 72 |
bad_ca = trustme.CA() |
| 73 |
|
| 74 |
cls.bad_ca_path = os.path.join(cls.certs_dir, "ca_bad.pem") |
| 75 |
bad_ca.cert_pem.write_to_path(cls.bad_ca_path) |
| 76 |
|
| 77 |
@classmethod |
| 78 |
def teardown_class(cls) -> None: |
| 79 |
super().teardown_class() |
| 80 |
shutil.rmtree(cls.certs_dir) |
| 81 |
|
| 82 |
def test_basic_proxy(self) -> None: |
| 83 |
with proxy_from_url(self.proxy_url, ca_certs=DEFAULT_CA) as http: |
| 84 |
r = http.request("GET", f"{self.http_url}/") |
| 85 |
assert r.status == 200 |
| 86 |
|
| 87 |
r = http.request("GET", f"{self.https_url}/") |
| 88 |
assert r.status == 200 |
| 89 |
|
| 90 |
def test_https_proxy(self) -> None: |
| 91 |
with proxy_from_url(self.https_proxy_url, ca_certs=DEFAULT_CA) as https: |
| 92 |
r = https.request("GET", f"{self.https_url}/") |
| 93 |
assert r.status == 200 |
| 94 |
|
| 95 |
r = https.request("GET", f"{self.http_url}/") |
| 96 |
assert r.status == 200 |
| 97 |
|
| 98 |
def test_is_verified_http_proxy_to_http_target(self) -> None: |
| 99 |
with proxy_from_url(self.proxy_url, ca_certs=DEFAULT_CA) as http: |
| 100 |
r = http.request("GET", f"{self.http_url}/") |
| 101 |
assert r.status == 200 |
| 102 |
assert_is_verified(http, proxy=False, target=False) |
| 103 |
|
| 104 |
def test_is_verified_http_proxy_to_https_target(self) -> None: |
| 105 |
with proxy_from_url(self.proxy_url, ca_certs=DEFAULT_CA) as http: |
| 106 |
r = http.request("GET", f"{self.https_url}/") |
| 107 |
assert r.status == 200 |
| 108 |
assert_is_verified(http, proxy=False, target=True) |
| 109 |
|
| 110 |
def test_is_verified_https_proxy_to_http_target(self) -> None: |
| 111 |
with proxy_from_url(self.https_proxy_url, ca_certs=DEFAULT_CA) as https: |
| 112 |
r = https.request("GET", f"{self.http_url}/") |
| 113 |
assert r.status == 200 |
| 114 |
assert_is_verified(https, proxy=True, target=False) |
| 115 |
|
| 116 |
def test_is_verified_https_proxy_to_https_target(self) -> None: |
| 117 |
with proxy_from_url(self.https_proxy_url, ca_certs=DEFAULT_CA) as https: |
| 118 |
r = https.request("GET", f"{self.https_url}/") |
| 119 |
assert r.status == 200 |
| 120 |
assert_is_verified(https, proxy=True, target=True) |
| 121 |
|
| 122 |
def test_http_and_https_kwarg_ca_cert_data_proxy(self) -> None: |
| 123 |
with open(DEFAULT_CA) as pem_file: |
| 124 |
pem_file_data = pem_file.read() |
| 125 |
with proxy_from_url(self.https_proxy_url, ca_cert_data=pem_file_data) as https: |
| 126 |
r = https.request("GET", f"{self.https_url}/") |
| 127 |
assert r.status == 200 |
| 128 |
|
| 129 |
r = https.request("GET", f"{self.http_url}/") |
| 130 |
assert r.status == 200 |
| 131 |
|
| 132 |
def test_https_proxy_with_proxy_ssl_context(self) -> None: |
| 133 |
proxy_ssl_context = create_urllib3_context() |
| 134 |
proxy_ssl_context.load_verify_locations(DEFAULT_CA) |
| 135 |
with proxy_from_url( |
| 136 |
self.https_proxy_url, |
| 137 |
proxy_ssl_context=proxy_ssl_context, |
| 138 |
ca_certs=DEFAULT_CA, |
| 139 |
) as https: |
| 140 |
r = https.request("GET", f"{self.https_url}/") |
| 141 |
assert r.status == 200 |
| 142 |
|
| 143 |
r = https.request("GET", f"{self.http_url}/") |
| 144 |
assert r.status == 200 |
| 145 |
|
| 146 |
@withPyOpenSSL |
| 147 |
def test_https_proxy_pyopenssl_not_supported(self) -> None: |
| 148 |
with proxy_from_url(self.https_proxy_url, ca_certs=DEFAULT_CA) as https: |
| 149 |
r = https.request("GET", f"{self.http_url}/") |
| 150 |
assert r.status == 200 |
| 151 |
|
| 152 |
with pytest.raises( |
| 153 |
ProxySchemeUnsupported, match="isn't available on non-native SSLContext" |
| 154 |
): |
| 155 |
https.request("GET", f"{self.https_url}/") |
| 156 |
|
| 157 |
def test_https_proxy_forwarding_for_https(self) -> None: |
| 158 |
with proxy_from_url( |
| 159 |
self.https_proxy_url, |
| 160 |
ca_certs=DEFAULT_CA, |
| 161 |
use_forwarding_for_https=True, |
| 162 |
) as https: |
| 163 |
r = https.request("GET", f"{self.http_url}/") |
| 164 |
assert r.status == 200 |
| 165 |
|
| 166 |
r = https.request("GET", f"{self.https_url}/") |
| 167 |
assert r.status == 200 |
| 168 |
|
| 169 |
def test_nagle_proxy(self) -> None: |
| 170 |
"""Test that proxy connections do not have TCP_NODELAY turned on""" |
| 171 |
with ProxyManager(self.proxy_url) as http: |
| 172 |
hc2 = http.connection_from_host(self.http_host, self.http_port) |
| 173 |
conn = hc2._get_conn() |
| 174 |
try: |
| 175 |
hc2._make_request(conn, "GET", f"{self.http_url}/") |
| 176 |
tcp_nodelay_setting = conn.sock.getsockopt( # type: ignore[attr-defined] |
| 177 |
socket.IPPROTO_TCP, socket.TCP_NODELAY |
| 178 |
) |
| 179 |
assert tcp_nodelay_setting == 0, ( |
| 180 |
"Expected TCP_NODELAY for proxies to be set " |
| 181 |
"to zero, instead was %s" % tcp_nodelay_setting |
| 182 |
) |
| 183 |
finally: |
| 184 |
conn.close() |
| 185 |
|
| 186 |
@pytest.mark.parametrize("proxy_scheme", ["http", "https"]) |
| 187 |
@pytest.mark.parametrize("target_scheme", ["http", "https"]) |
| 188 |
def test_proxy_conn_fail_from_dns( |
| 189 |
self, proxy_scheme: str, target_scheme: str |
| 190 |
) -> None: |
| 191 |
host, port = get_unreachable_address() |
| 192 |
with proxy_from_url( |
| 193 |
f"{proxy_scheme}://{host}:{port}/", retries=1, timeout=LONG_TIMEOUT |
| 194 |
) as http: |
| 195 |
if target_scheme == "https": |
| 196 |
target_url = self.https_url |
| 197 |
else: |
| 198 |
target_url = self.http_url |
| 199 |
|
| 200 |
with pytest.raises(MaxRetryError) as e: |
| 201 |
http.request("GET", f"{target_url}/") |
| 202 |
assert isinstance(e.value.reason, ProxyError) |
| 203 |
assert isinstance( |
| 204 |
e.value.reason.original_error, urllib3.exceptions.NameResolutionError |
| 205 |
) |
| 206 |
|
| 207 |
def test_oldapi(self) -> None: |
| 208 |
with ProxyManager( |
| 209 |
connection_from_url(self.proxy_url), ca_certs=DEFAULT_CA # type: ignore[arg-type] |
| 210 |
) as http: |
| 211 |
r = http.request("GET", f"{self.http_url}/") |
| 212 |
assert r.status == 200 |
| 213 |
|
| 214 |
r = http.request("GET", f"{self.https_url}/") |
| 215 |
assert r.status == 200 |
| 216 |
|
| 217 |
@resolvesLocalhostFQDN() |
| 218 |
def test_proxy_https_fqdn(self) -> None: |
| 219 |
with proxy_from_url(self.proxy_url, ca_certs=DEFAULT_CA) as http: |
| 220 |
r = http.request("GET", f"{self.https_url_fqdn}/") |
| 221 |
assert r.status == 200 |
| 222 |
|
| 223 |
def test_proxy_verified(self) -> None: |
| 224 |
with proxy_from_url( |
| 225 |
self.proxy_url, cert_reqs="REQUIRED", ca_certs=self.bad_ca_path |
| 226 |
) as http: |
| 227 |
with http._new_pool( |
| 228 |
"https", self.https_host, self.https_port |
| 229 |
) as https_pool: |
| 230 |
with pytest.raises(MaxRetryError) as e: |
| 231 |
https_pool.request("GET", "/", retries=0) |
| 232 |
assert isinstance(e.value.reason, SSLError) |
| 233 |
assert ( |
| 234 |
"certificate verify failed" in str(e.value.reason) |
| 235 |
# PyPy is more specific |
| 236 |
or "self signed certificate in certificate chain" in str(e.value.reason) |
| 237 |
), f"Expected 'certificate verify failed', instead got: {e.value.reason!r}" |
| 238 |
|
| 239 |
http = proxy_from_url( |
| 240 |
self.proxy_url, cert_reqs="REQUIRED", ca_certs=DEFAULT_CA |
| 241 |
) |
| 242 |
with http._new_pool( |
| 243 |
"https", self.https_host, self.https_port |
| 244 |
) as https_pool2: |
| 245 |
with contextlib.closing(https_pool._new_conn()) as conn: |
| 246 |
assert conn.__class__ == VerifiedHTTPSConnection |
| 247 |
https_pool2.request( |
| 248 |
"GET", "/" |
| 249 |
) # Should succeed without exceptions. |
| 250 |
|
| 251 |
http = proxy_from_url( |
| 252 |
self.proxy_url, cert_reqs="REQUIRED", ca_certs=DEFAULT_CA |
| 253 |
) |
| 254 |
with http._new_pool( |
| 255 |
"https", "127.0.0.1", self.https_port |
| 256 |
) as https_fail_pool: |
| 257 |
with pytest.raises( |
| 258 |
MaxRetryError, match="doesn't match|IP address mismatch" |
| 259 |
) as e: |
| 260 |
https_fail_pool.request("GET", "/", retries=0) |
| 261 |
assert isinstance(e.value.reason, SSLError) |
| 262 |
|
| 263 |
def test_redirect(self) -> None: |
| 264 |
with proxy_from_url(self.proxy_url) as http: |
| 265 |
r = http.request( |
| 266 |
"GET", |
| 267 |
f"{self.http_url}/redirect", |
| 268 |
fields={"target": f"{self.http_url}/"}, |
| 269 |
redirect=False, |
| 270 |
) |
| 271 |
|
| 272 |
assert r.status == 303 |
| 273 |
|
| 274 |
r = http.request( |
| 275 |
"GET", |
| 276 |
f"{self.http_url}/redirect", |
| 277 |
fields={"target": f"{self.http_url}/"}, |
| 278 |
) |
| 279 |
|
| 280 |
assert r.status == 200 |
| 281 |
assert r.data == b"Dummy server!" |
| 282 |
|
| 283 |
def test_cross_host_redirect(self) -> None: |
| 284 |
with proxy_from_url(self.proxy_url) as http: |
| 285 |
cross_host_location = f"{self.http_url_alt}/echo?a=b" |
| 286 |
with pytest.raises(MaxRetryError): |
| 287 |
http.request( |
| 288 |
"GET", |
| 289 |
f"{self.http_url}/redirect", |
| 290 |
fields={"target": cross_host_location}, |
| 291 |
retries=0, |
| 292 |
) |
| 293 |
|
| 294 |
r = http.request( |
| 295 |
"GET", |
| 296 |
f"{self.http_url}/redirect", |
| 297 |
fields={"target": f"{self.http_url_alt}/echo?a=b"}, |
| 298 |
retries=1, |
| 299 |
) |
| 300 |
assert isinstance(r, HTTPResponse) |
| 301 |
assert r._pool is not None |
| 302 |
assert r._pool.host != self.http_host_alt |
| 303 |
|
| 304 |
_sensitive_headers = { |
| 305 |
"Authorization": "foo", |
| 306 |
"Proxy-Authorization": "bar", |
| 307 |
"Cookie": "foo=bar", |
| 308 |
} |
| 309 |
|
| 310 |
@pytest.mark.parametrize( |
| 311 |
"sensitive_headers", |
| 312 |
(_sensitive_headers, {k.lower(): v for k, v in _sensitive_headers.items()}), |
| 313 |
ids=("capitalized", "lowercase"), |
| 314 |
) |
| 315 |
def test_cross_host_redirect_remove_headers_via_proxy_manager( |
| 316 |
self, sensitive_headers: dict[str, str] |
| 317 |
) -> None: |
| 318 |
headers_url = f"{self.http_url_alt}/headers" |
| 319 |
initial_url = f"{self.http_url}/redirect?target={headers_url}" |
| 320 |
with proxy_from_url(self.proxy_url) as proxy_mgr: |
| 321 |
r = proxy_mgr.request( |
| 322 |
"GET", initial_url, headers=sensitive_headers, retries=1 |
| 323 |
) |
| 324 |
assert r.status == 200 |
| 325 |
assert r.retries is not None |
| 326 |
assert r.retries.history == ( |
| 327 |
RequestHistory( |
| 328 |
method="GET", |
| 329 |
url=initial_url, |
| 330 |
error=None, |
| 331 |
status=303, |
| 332 |
redirect_location=headers_url, |
| 333 |
), |
| 334 |
) |
| 335 |
data = r.json() |
| 336 |
for header in sensitive_headers: |
| 337 |
assert header not in data |
| 338 |
|
| 339 |
@pytest.mark.parametrize( |
| 340 |
"sensitive_headers", |
| 341 |
(_sensitive_headers, {k.lower(): v for k, v in _sensitive_headers.items()}), |
| 342 |
ids=("capitalized", "lowercase"), |
| 343 |
) |
| 344 |
def test_cross_host_redirect_remove_headers_via_pool( |
| 345 |
self, sensitive_headers: dict[str, str] |
| 346 |
) -> None: |
| 347 |
headers_url = f"{self.http_url_alt}/headers" |
| 348 |
initial_url = f"{self.http_url}/redirect?target={headers_url}" |
| 349 |
with proxy_from_url(self.proxy_url) as proxy_mgr: |
| 350 |
pool = proxy_mgr.connection_from_url(self.http_url) |
| 351 |
r = pool.urlopen( |
| 352 |
"GET", |
| 353 |
initial_url, |
| 354 |
headers=sensitive_headers, |
| 355 |
retries=1, |
| 356 |
redirect=True, |
| 357 |
assert_same_host=False, |
| 358 |
preload_content=True, |
| 359 |
) |
| 360 |
assert r.status == 200 |
| 361 |
assert r.retries is not None |
| 362 |
assert r.retries.history == ( |
| 363 |
RequestHistory( |
| 364 |
method="GET", |
| 365 |
url=initial_url, |
| 366 |
error=None, |
| 367 |
status=303, |
| 368 |
redirect_location=headers_url, |
| 369 |
), |
| 370 |
) |
| 371 |
data = r.json() |
| 372 |
for header in sensitive_headers: |
| 373 |
assert header not in data |
| 374 |
|
| 375 |
def test_cross_protocol_redirect(self) -> None: |
| 376 |
with proxy_from_url(self.proxy_url, ca_certs=DEFAULT_CA) as http: |
| 377 |
cross_protocol_location = f"{self.https_url}/echo?a=b" |
| 378 |
with pytest.raises(MaxRetryError): |
| 379 |
http.request( |
| 380 |
"GET", |
| 381 |
f"{self.http_url}/redirect", |
| 382 |
fields={"target": cross_protocol_location}, |
| 383 |
retries=0, |
| 384 |
) |
| 385 |
|
| 386 |
r = http.request( |
| 387 |
"GET", |
| 388 |
f"{self.http_url}/redirect", |
| 389 |
fields={"target": f"{self.https_url}/echo?a=b"}, |
| 390 |
retries=1, |
| 391 |
) |
| 392 |
assert isinstance(r, HTTPResponse) |
| 393 |
assert r._pool is not None |
| 394 |
assert r._pool.host == self.https_host |
| 395 |
|
| 396 |
def test_headers(self) -> None: |
| 397 |
with proxy_from_url( |
| 398 |
self.proxy_url, |
| 399 |
headers={"Foo": "bar"}, |
| 400 |
proxy_headers={"Hickory": "dickory"}, |
| 401 |
ca_certs=DEFAULT_CA, |
| 402 |
) as http: |
| 403 |
r = http.request_encode_url("GET", f"{self.http_url}/headers") |
| 404 |
returned_headers = r.json() |
| 405 |
assert returned_headers.get("Foo") == "bar" |
| 406 |
assert returned_headers.get("Hickory") == "dickory" |
| 407 |
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}" |
| 408 |
|
| 409 |
r = http.request_encode_url("GET", f"{self.http_url_alt}/headers") |
| 410 |
returned_headers = r.json() |
| 411 |
assert returned_headers.get("Foo") == "bar" |
| 412 |
assert returned_headers.get("Hickory") == "dickory" |
| 413 |
assert ( |
| 414 |
returned_headers.get("Host") == f"{self.http_host_alt}:{self.http_port}" |
| 415 |
) |
| 416 |
|
| 417 |
r = http.request_encode_url("GET", f"{self.https_url}/headers") |
| 418 |
returned_headers = r.json() |
| 419 |
assert returned_headers.get("Foo") == "bar" |
| 420 |
assert returned_headers.get("Hickory") is None |
| 421 |
assert ( |
| 422 |
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}" |
| 423 |
) |
| 424 |
|
| 425 |
r = http.request_encode_body("POST", f"{self.http_url}/headers") |
| 426 |
returned_headers = r.json() |
| 427 |
assert returned_headers.get("Foo") == "bar" |
| 428 |
assert returned_headers.get("Hickory") == "dickory" |
| 429 |
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}" |
| 430 |
|
| 431 |
r = http.request_encode_url( |
| 432 |
"GET", f"{self.http_url}/headers", headers={"Baz": "quux"} |
| 433 |
) |
| 434 |
returned_headers = r.json() |
| 435 |
assert returned_headers.get("Foo") is None |
| 436 |
assert returned_headers.get("Baz") == "quux" |
| 437 |
assert returned_headers.get("Hickory") == "dickory" |
| 438 |
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}" |
| 439 |
|
| 440 |
r = http.request_encode_url( |
| 441 |
"GET", f"{self.https_url}/headers", headers={"Baz": "quux"} |
| 442 |
) |
| 443 |
returned_headers = r.json() |
| 444 |
assert returned_headers.get("Foo") is None |
| 445 |
assert returned_headers.get("Baz") == "quux" |
| 446 |
assert returned_headers.get("Hickory") is None |
| 447 |
assert ( |
| 448 |
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}" |
| 449 |
) |
| 450 |
|
| 451 |
r = http.request_encode_body( |
| 452 |
"GET", f"{self.http_url}/headers", headers={"Baz": "quux"} |
| 453 |
) |
| 454 |
returned_headers = r.json() |
| 455 |
assert returned_headers.get("Foo") is None |
| 456 |
assert returned_headers.get("Baz") == "quux" |
| 457 |
assert returned_headers.get("Hickory") == "dickory" |
| 458 |
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}" |
| 459 |
|
| 460 |
r = http.request_encode_body( |
| 461 |
"GET", f"{self.https_url}/headers", headers={"Baz": "quux"} |
| 462 |
) |
| 463 |
returned_headers = r.json() |
| 464 |
assert returned_headers.get("Foo") is None |
| 465 |
assert returned_headers.get("Baz") == "quux" |
| 466 |
assert returned_headers.get("Hickory") is None |
| 467 |
assert ( |
| 468 |
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}" |
| 469 |
) |
| 470 |
|
| 471 |
def test_https_headers(self) -> None: |
| 472 |
with proxy_from_url( |
| 473 |
self.https_proxy_url, |
| 474 |
headers={"Foo": "bar"}, |
| 475 |
proxy_headers={"Hickory": "dickory"}, |
| 476 |
ca_certs=DEFAULT_CA, |
| 477 |
) as http: |
| 478 |
r = http.request_encode_url("GET", f"{self.http_url}/headers") |
| 479 |
returned_headers = r.json() |
| 480 |
assert returned_headers.get("Foo") == "bar" |
| 481 |
assert returned_headers.get("Hickory") == "dickory" |
| 482 |
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}" |
| 483 |
|
| 484 |
r = http.request_encode_url("GET", f"{self.http_url_alt}/headers") |
| 485 |
returned_headers = r.json() |
| 486 |
assert returned_headers.get("Foo") == "bar" |
| 487 |
assert returned_headers.get("Hickory") == "dickory" |
| 488 |
assert ( |
| 489 |
returned_headers.get("Host") == f"{self.http_host_alt}:{self.http_port}" |
| 490 |
) |
| 491 |
|
| 492 |
r = http.request_encode_body( |
| 493 |
"GET", f"{self.https_url}/headers", headers={"Baz": "quux"} |
| 494 |
) |
| 495 |
returned_headers = r.json() |
| 496 |
assert returned_headers.get("Foo") is None |
| 497 |
assert returned_headers.get("Baz") == "quux" |
| 498 |
assert returned_headers.get("Hickory") is None |
| 499 |
assert ( |
| 500 |
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}" |
| 501 |
) |
| 502 |
|
| 503 |
def test_https_headers_forwarding_for_https(self) -> None: |
| 504 |
with proxy_from_url( |
| 505 |
self.https_proxy_url, |
| 506 |
headers={"Foo": "bar"}, |
| 507 |
proxy_headers={"Hickory": "dickory"}, |
| 508 |
ca_certs=DEFAULT_CA, |
| 509 |
use_forwarding_for_https=True, |
| 510 |
) as http: |
| 511 |
r = http.request_encode_url("GET", f"{self.https_url}/headers") |
| 512 |
returned_headers = r.json() |
| 513 |
assert returned_headers.get("Foo") == "bar" |
| 514 |
assert returned_headers.get("Hickory") == "dickory" |
| 515 |
assert ( |
| 516 |
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}" |
| 517 |
) |
| 518 |
|
| 519 |
def test_headerdict(self) -> None: |
| 520 |
default_headers = HTTPHeaderDict(a="b") |
| 521 |
proxy_headers = HTTPHeaderDict() |
| 522 |
proxy_headers.add("foo", "bar") |
| 523 |
|
| 524 |
with proxy_from_url( |
| 525 |
self.proxy_url, headers=default_headers, proxy_headers=proxy_headers |
| 526 |
) as http: |
| 527 |
request_headers = HTTPHeaderDict(baz="quux") |
| 528 |
r = http.request("GET", f"{self.http_url}/headers", headers=request_headers) |
| 529 |
returned_headers = r.json() |
| 530 |
assert returned_headers.get("Foo") == "bar" |
| 531 |
assert returned_headers.get("Baz") == "quux" |
| 532 |
|
| 533 |
def test_proxy_pooling(self) -> None: |
| 534 |
with proxy_from_url(self.proxy_url, cert_reqs="NONE") as http: |
| 535 |
for x in range(2): |
| 536 |
http.urlopen("GET", self.http_url) |
| 537 |
assert len(http.pools) == 1 |
| 538 |
|
| 539 |
for x in range(2): |
| 540 |
http.urlopen("GET", self.http_url_alt) |
| 541 |
assert len(http.pools) == 1 |
| 542 |
|
| 543 |
for x in range(2): |
| 544 |
with pytest.warns(InsecureRequestWarning): |
| 545 |
http.urlopen("GET", self.https_url) |
| 546 |
assert len(http.pools) == 2 |
| 547 |
|
| 548 |
for x in range(2): |
| 549 |
with pytest.warns(InsecureRequestWarning): |
| 550 |
http.urlopen("GET", self.https_url_alt) |
| 551 |
assert len(http.pools) == 3 |
| 552 |
|
| 553 |
def test_proxy_pooling_ext(self) -> None: |
| 554 |
with proxy_from_url(self.proxy_url) as http: |
| 555 |
hc1 = http.connection_from_url(self.http_url) |
| 556 |
hc2 = http.connection_from_host(self.http_host, self.http_port) |
| 557 |
hc3 = http.connection_from_url(self.http_url_alt) |
| 558 |
hc4 = http.connection_from_host(self.http_host_alt, self.http_port) |
| 559 |
assert hc1 == hc2 |
| 560 |
assert hc2 == hc3 |
| 561 |
assert hc3 == hc4 |
| 562 |
|
| 563 |
sc1 = http.connection_from_url(self.https_url) |
| 564 |
sc2 = http.connection_from_host( |
| 565 |
self.https_host, self.https_port, scheme="https" |
| 566 |
) |
| 567 |
sc3 = http.connection_from_url(self.https_url_alt) |
| 568 |
sc4 = http.connection_from_host( |
| 569 |
self.https_host_alt, self.https_port, scheme="https" |
| 570 |
) |
| 571 |
assert sc1 == sc2 |
| 572 |
assert sc2 != sc3 |
| 573 |
assert sc3 == sc4 |
| 574 |
|
| 575 |
@requires_network() |
| 576 |
@pytest.mark.parametrize( |
| 577 |
["proxy_scheme", "target_scheme", "use_forwarding_for_https"], |
| 578 |
[ |
| 579 |
("http", "http", False), |
| 580 |
("https", "http", False), |
| 581 |
# 'use_forwarding_for_https' is only valid for HTTPS+HTTPS. |
| 582 |
("https", "https", True), |
| 583 |
], |
| 584 |
) |
| 585 |
def test_forwarding_proxy_request_timeout( |
| 586 |
self, proxy_scheme: str, target_scheme: str, use_forwarding_for_https: bool |
| 587 |
) -> None: |
| 588 |
proxy_url = self.https_proxy_url if proxy_scheme == "https" else self.proxy_url |
| 589 |
target_url = f"{target_scheme}://{TARPIT_HOST}" |
| 590 |
|
| 591 |
with proxy_from_url( |
| 592 |
proxy_url, |
| 593 |
ca_certs=DEFAULT_CA, |
| 594 |
use_forwarding_for_https=use_forwarding_for_https, |
| 595 |
) as proxy: |
| 596 |
with pytest.raises(MaxRetryError) as e: |
| 597 |
timeout = Timeout(connect=LONG_TIMEOUT, read=SHORT_TIMEOUT) |
| 598 |
proxy.request("GET", target_url, timeout=timeout) |
| 599 |
|
| 600 |
# We sent the request to the proxy but didn't get any response |
| 601 |
# so we're not sure if that's being caused by the proxy or the |
| 602 |
# target so we put the blame on the target. |
| 603 |
assert isinstance(e.value.reason, ReadTimeoutError) |
| 604 |
|
| 605 |
@requires_network() |
| 606 |
@pytest.mark.parametrize( |
| 607 |
["proxy_scheme", "target_scheme"], [("http", "https"), ("https", "https")] |
| 608 |
) |
| 609 |
def test_tunneling_proxy_request_timeout( |
| 610 |
self, proxy_scheme: str, target_scheme: str |
| 611 |
) -> None: |
| 612 |
proxy_url = self.https_proxy_url if proxy_scheme == "https" else self.proxy_url |
| 613 |
target_url = f"{target_scheme}://{TARPIT_HOST}" |
| 614 |
|
| 615 |
with proxy_from_url( |
| 616 |
proxy_url, |
| 617 |
ca_certs=DEFAULT_CA, |
| 618 |
) as proxy: |
| 619 |
with pytest.raises(MaxRetryError) as e: |
| 620 |
timeout = Timeout(connect=LONG_TIMEOUT, read=SHORT_TIMEOUT) |
| 621 |
proxy.request("GET", target_url, timeout=timeout) |
| 622 |
|
| 623 |
assert isinstance(e.value.reason, ReadTimeoutError) |
| 624 |
|
| 625 |
@requires_network() |
| 626 |
@pytest.mark.parametrize( |
| 627 |
["proxy_scheme", "target_scheme", "use_forwarding_for_https"], |
| 628 |
[ |
| 629 |
("http", "http", False), |
| 630 |
("https", "http", False), |
| 631 |
# 'use_forwarding_for_https' is only valid for HTTPS+HTTPS. |
| 632 |
("https", "https", True), |
| 633 |
], |
| 634 |
) |
| 635 |
def test_forwarding_proxy_connect_timeout( |
| 636 |
self, proxy_scheme: str, target_scheme: str, use_forwarding_for_https: bool |
| 637 |
) -> None: |
| 638 |
proxy_url = f"{proxy_scheme}://{TARPIT_HOST}" |
| 639 |
target_url = self.https_url if target_scheme == "https" else self.http_url |
| 640 |
|
| 641 |
with proxy_from_url( |
| 642 |
proxy_url, |
| 643 |
ca_certs=DEFAULT_CA, |
| 644 |
timeout=SHORT_TIMEOUT, |
| 645 |
use_forwarding_for_https=use_forwarding_for_https, |
| 646 |
) as proxy: |
| 647 |
with pytest.raises(MaxRetryError) as e: |
| 648 |
proxy.request("GET", target_url) |
| 649 |
|
| 650 |
assert isinstance(e.value.reason, ProxyError) |
| 651 |
assert isinstance(e.value.reason.original_error, ConnectTimeoutError) |
| 652 |
|
| 653 |
@requires_network() |
| 654 |
@pytest.mark.parametrize( |
| 655 |
["proxy_scheme", "target_scheme"], [("http", "https"), ("https", "https")] |
| 656 |
) |
| 657 |
def test_tunneling_proxy_connect_timeout( |
| 658 |
self, proxy_scheme: str, target_scheme: str |
| 659 |
) -> None: |
| 660 |
proxy_url = f"{proxy_scheme}://{TARPIT_HOST}" |
| 661 |
target_url = self.https_url if target_scheme == "https" else self.http_url |
| 662 |
|
| 663 |
with proxy_from_url( |
| 664 |
proxy_url, ca_certs=DEFAULT_CA, timeout=SHORT_TIMEOUT |
| 665 |
) as proxy: |
| 666 |
with pytest.raises(MaxRetryError) as e: |
| 667 |
proxy.request("GET", target_url) |
| 668 |
|
| 669 |
assert isinstance(e.value.reason, ProxyError) |
| 670 |
assert isinstance(e.value.reason.original_error, ConnectTimeoutError) |
| 671 |
|
| 672 |
@requires_network() |
| 673 |
@pytest.mark.parametrize( |
| 674 |
["target_scheme", "use_forwarding_for_https"], |
| 675 |
[ |
| 676 |
("http", False), |
| 677 |
("https", False), |
| 678 |
("https", True), |
| 679 |
], |
| 680 |
) |
| 681 |
def test_https_proxy_tls_error( |
| 682 |
self, target_scheme: str, use_forwarding_for_https: str |
| 683 |
) -> None: |
| 684 |
target_url = self.https_url if target_scheme == "https" else self.http_url |
| 685 |
proxy_ctx = ssl.create_default_context() |
| 686 |
with proxy_from_url( |
| 687 |
self.https_proxy_url, |
| 688 |
proxy_ssl_context=proxy_ctx, |
| 689 |
use_forwarding_for_https=use_forwarding_for_https, |
| 690 |
) as proxy: |
| 691 |
with pytest.raises(MaxRetryError) as e: |
| 692 |
proxy.request("GET", target_url) |
| 693 |
assert isinstance(e.value.reason, ProxyError) |
| 694 |
assert isinstance(e.value.reason.original_error, SSLError) |
| 695 |
|
| 696 |
@requires_network() |
| 697 |
@pytest.mark.parametrize( |
| 698 |
["proxy_scheme", "use_forwarding_for_https"], |
| 699 |
[ |
| 700 |
("http", False), |
| 701 |
("https", False), |
| 702 |
("https", True), |
| 703 |
], |
| 704 |
) |
| 705 |
def test_proxy_https_target_tls_error( |
| 706 |
self, proxy_scheme: str, use_forwarding_for_https: str |
| 707 |
) -> None: |
| 708 |
if proxy_scheme == "https" and use_forwarding_for_https: |
| 709 |
pytest.skip("Test is expected to fail due to urllib3/urllib3#2577") |
| 710 |
|
| 711 |
proxy_url = self.https_proxy_url if proxy_scheme == "https" else self.proxy_url |
| 712 |
proxy_ctx = ssl.create_default_context() |
| 713 |
proxy_ctx.load_verify_locations(DEFAULT_CA) |
| 714 |
ctx = ssl.create_default_context() |
| 715 |
|
| 716 |
with proxy_from_url( |
| 717 |
proxy_url, |
| 718 |
proxy_ssl_context=proxy_ctx, |
| 719 |
ssl_context=ctx, |
| 720 |
use_forwarding_for_https=use_forwarding_for_https, |
| 721 |
) as proxy: |
| 722 |
with pytest.raises(MaxRetryError) as e: |
| 723 |
proxy.request("GET", self.https_url) |
| 724 |
assert isinstance(e.value.reason, SSLError) |
| 725 |
|
| 726 |
def test_scheme_host_case_insensitive(self) -> None: |
| 727 |
"""Assert that upper-case schemes and hosts are normalized.""" |
| 728 |
with proxy_from_url(self.proxy_url.upper(), ca_certs=DEFAULT_CA) as http: |
| 729 |
r = http.request("GET", f"{self.http_url.upper()}/") |
| 730 |
assert r.status == 200 |
| 731 |
|
| 732 |
r = http.request("GET", f"{self.https_url.upper()}/") |
| 733 |
assert r.status == 200 |
| 734 |
|
| 735 |
@pytest.mark.parametrize( |
| 736 |
"url, error_msg", |
| 737 |
[ |
| 738 |
( |
| 739 |
"127.0.0.1", |
| 740 |
"Proxy URL had no scheme, should start with http:// or https://", |
| 741 |
), |
| 742 |
( |
| 743 |
"localhost:8080", |
| 744 |
"Proxy URL had no scheme, should start with http:// or https://", |
| 745 |
), |
| 746 |
( |
| 747 |
"ftp://google.com", |
| 748 |
"Proxy URL had unsupported scheme ftp, should use http:// or https://", |
| 749 |
), |
| 750 |
], |
| 751 |
) |
| 752 |
def test_invalid_schema(self, url: str, error_msg: str) -> None: |
| 753 |
with pytest.raises(ProxySchemeUnknown, match=error_msg): |
| 754 |
proxy_from_url(url) |
| 755 |
|
| 756 |
|
| 757 |
@pytest.mark.skipif(not HAS_IPV6, reason="Only runs on IPv6 systems") |
| 758 |
class TestIPv6HTTPProxyManager(IPv6HypercornDummyProxyTestCase): |
| 759 |
@classmethod |
| 760 |
def setup_class(cls) -> None: |
| 761 |
super().setup_class() |
| 762 |
cls.http_url = f"http://{cls.http_host}:{int(cls.http_port)}" |
| 763 |
cls.http_url_alt = f"http://{cls.http_host_alt}:{int(cls.http_port)}" |
| 764 |
cls.https_url = f"https://{cls.https_host}:{int(cls.https_port)}" |
| 765 |
cls.https_url_alt = f"https://{cls.https_host_alt}:{int(cls.https_port)}" |
| 766 |
cls.proxy_url = f"http://[{cls.proxy_host}]:{int(cls.proxy_port)}" |
| 767 |
|
| 768 |
def test_basic_ipv6_proxy(self) -> None: |
| 769 |
with proxy_from_url(self.proxy_url, ca_certs=DEFAULT_CA) as http: |
| 770 |
r = http.request("GET", f"{self.http_url}/") |
| 771 |
assert r.status == 200 |
| 772 |
|
| 773 |
r = http.request("GET", f"{self.https_url}/") |
| 774 |
assert r.status == 200 |
| 775 |
|
| 776 |
|
| 777 |
class TestHTTPSProxyVerification: |
| 778 |
@staticmethod |
| 779 |
def _get_proxy_fingerprint_md5(ca_path: str) -> str: |
| 780 |
proxy_pem_path = pathlib.Path(ca_path).parent / "proxy.pem" |
| 781 |
proxy_der = ssl.PEM_cert_to_DER_cert(proxy_pem_path.read_text()) |
| 782 |
proxy_hashed = hashlib.md5(proxy_der).digest() |
| 783 |
fingerprint = binascii.hexlify(proxy_hashed).decode("ascii") |
| 784 |
return fingerprint |
| 785 |
|
| 786 |
@staticmethod |
| 787 |
def _get_certificate_formatted_proxy_host(host: str) -> str: |
| 788 |
try: |
| 789 |
addr = ipaddress.ip_address(host) |
| 790 |
except ValueError: |
| 791 |
return host |
| 792 |
|
| 793 |
if addr.version != 6: |
| 794 |
return host |
| 795 |
|
| 796 |
# Transform ipv6 like '::1' to 0:0:0:0:0:0:0:1 via '0000:0000:0000:0000:0000:0000:0000:0001' |
| 797 |
return addr.exploded.replace("0000", "0").replace("000", "") |
| 798 |
|
| 799 |
def test_https_proxy_assert_fingerprint_md5( |
| 800 |
self, no_san_proxy_with_server: tuple[ServerConfig, ServerConfig] |
| 801 |
) -> None: |
| 802 |
proxy, server = no_san_proxy_with_server |
| 803 |
proxy_url = f"https://{proxy.host}:{proxy.port}" |
| 804 |
destination_url = f"https://{server.host}:{server.port}" |
| 805 |
|
| 806 |
proxy_ctx = urllib3.util.ssl_.create_urllib3_context(verify_flags=0) |
| 807 |
proxy_fingerprint = self._get_proxy_fingerprint_md5(proxy.ca_certs) |
| 808 |
with proxy_from_url( |
| 809 |
proxy_url, |
| 810 |
ca_certs=proxy.ca_certs, |
| 811 |
proxy_ssl_context=proxy_ctx, |
| 812 |
proxy_assert_fingerprint=proxy_fingerprint, |
| 813 |
) as https: |
| 814 |
https.request("GET", destination_url) |
| 815 |
|
| 816 |
def test_https_proxy_assert_fingerprint_md5_non_matching( |
| 817 |
self, no_san_proxy_with_server: tuple[ServerConfig, ServerConfig] |
| 818 |
) -> None: |
| 819 |
proxy, server = no_san_proxy_with_server |
| 820 |
proxy_url = f"https://{proxy.host}:{proxy.port}" |
| 821 |
destination_url = f"https://{server.host}:{server.port}" |
| 822 |
|
| 823 |
proxy_ctx = urllib3.util.ssl_.create_urllib3_context(verify_flags=0) |
| 824 |
proxy_fingerprint = self._get_proxy_fingerprint_md5(proxy.ca_certs) |
| 825 |
new_char = "b" if proxy_fingerprint[5] == "a" else "a" |
| 826 |
proxy_fingerprint = proxy_fingerprint[:5] + new_char + proxy_fingerprint[6:] |
| 827 |
|
| 828 |
with proxy_from_url( |
| 829 |
proxy_url, |
| 830 |
ca_certs=proxy.ca_certs, |
| 831 |
proxy_ssl_context=proxy_ctx, |
| 832 |
proxy_assert_fingerprint=proxy_fingerprint, |
| 833 |
) as https: |
| 834 |
with pytest.raises(MaxRetryError) as e: |
| 835 |
https.request("GET", destination_url) |
| 836 |
|
| 837 |
assert "Fingerprints did not match" in str(e) |
| 838 |
|
| 839 |
def test_https_proxy_assert_hostname( |
| 840 |
self, san_proxy_with_server: tuple[ServerConfig, ServerConfig] |
| 841 |
) -> None: |
| 842 |
proxy, server = san_proxy_with_server |
| 843 |
destination_url = f"https://{server.host}:{server.port}" |
| 844 |
|
| 845 |
with proxy_from_url( |
| 846 |
proxy.base_url, ca_certs=proxy.ca_certs, proxy_assert_hostname=proxy.host |
| 847 |
) as https: |
| 848 |
https.request("GET", destination_url) |
| 849 |
|
| 850 |
def test_https_proxy_assert_hostname_non_matching( |
| 851 |
self, san_proxy_with_server: tuple[ServerConfig, ServerConfig] |
| 852 |
) -> None: |
| 853 |
proxy, server = san_proxy_with_server |
| 854 |
destination_url = f"https://{server.host}:{server.port}" |
| 855 |
|
| 856 |
proxy_hostname = "example.com" |
| 857 |
with proxy_from_url( |
| 858 |
proxy.base_url, |
| 859 |
ca_certs=proxy.ca_certs, |
| 860 |
proxy_assert_hostname=proxy_hostname, |
| 861 |
) as https: |
| 862 |
with pytest.raises(MaxRetryError) as e: |
| 863 |
https.request("GET", destination_url) |
| 864 |
|
| 865 |
proxy_host = self._get_certificate_formatted_proxy_host(proxy.host) |
| 866 |
msg = f"hostname \\'{proxy_hostname}\\' doesn\\'t match \\'{proxy_host}\\'" |
| 867 |
assert msg in str(e) |
| 868 |
|
| 869 |
def test_https_proxy_hostname_verification( |
| 870 |
self, no_localhost_san_server: ServerConfig |
| 871 |
) -> None: |
| 872 |
bad_server = no_localhost_san_server |
| 873 |
bad_proxy_url = f"https://{bad_server.host}:{bad_server.port}" |
| 874 |
|
| 875 |
# An exception will be raised before we contact the destination domain. |
| 876 |
test_url = "testing.com" |
| 877 |
with proxy_from_url(bad_proxy_url, ca_certs=bad_server.ca_certs) as https: |
| 878 |
with pytest.raises(MaxRetryError) as e: |
| 879 |
https.request("GET", "http://%s/" % test_url) |
| 880 |
assert isinstance(e.value.reason, ProxyError) |
| 881 |
|
| 882 |
ssl_error = e.value.reason.original_error |
| 883 |
assert isinstance(ssl_error, SSLError) |
| 884 |
assert "hostname 'localhost' doesn't match" in str( |
| 885 |
ssl_error |
| 886 |
) or "Hostname mismatch" in str(ssl_error) |
| 887 |
|
| 888 |
with pytest.raises(MaxRetryError) as e: |
| 889 |
https.request("GET", "https://%s/" % test_url) |
| 890 |
assert isinstance(e.value.reason, ProxyError) |
| 891 |
|
| 892 |
ssl_error = e.value.reason.original_error |
| 893 |
assert isinstance(ssl_error, SSLError) |
| 894 |
assert "hostname 'localhost' doesn't match" in str( |
| 895 |
ssl_error |
| 896 |
) or "Hostname mismatch" in str(ssl_error) |
| 897 |
|
| 898 |
def test_https_proxy_ipv4_san( |
| 899 |
self, ipv4_san_proxy_with_server: tuple[ServerConfig, ServerConfig] |
| 900 |
) -> None: |
| 901 |
proxy, server = ipv4_san_proxy_with_server |
| 902 |
proxy_url = f"https://{proxy.host}:{proxy.port}" |
| 903 |
destination_url = f"https://{server.host}:{server.port}" |
| 904 |
with proxy_from_url(proxy_url, ca_certs=proxy.ca_certs) as https: |
| 905 |
r = https.request("GET", destination_url) |
| 906 |
assert r.status == 200 |
| 907 |
|
| 908 |
def test_https_proxy_ipv6_san( |
| 909 |
self, ipv6_san_proxy_with_server: tuple[ServerConfig, ServerConfig] |
| 910 |
) -> None: |
| 911 |
proxy, server = ipv6_san_proxy_with_server |
| 912 |
proxy_url = f"https://[{proxy.host}]:{proxy.port}" |
| 913 |
destination_url = f"https://{server.host}:{server.port}" |
| 914 |
with proxy_from_url(proxy_url, ca_certs=proxy.ca_certs) as https: |
| 915 |
r = https.request("GET", destination_url) |
| 916 |
assert r.status == 200 |
| 917 |
|
| 918 |
@pytest.mark.parametrize("target_scheme", ["http", "https"]) |
| 919 |
def test_https_proxy_no_san( |
| 920 |
self, |
| 921 |
no_san_proxy_with_server: tuple[ServerConfig, ServerConfig], |
| 922 |
target_scheme: str, |
| 923 |
) -> None: |
| 924 |
proxy, server = no_san_proxy_with_server |
| 925 |
proxy_url = f"https://{proxy.host}:{proxy.port}" |
| 926 |
destination_url = f"{target_scheme}://{server.host}:{server.port}" |
| 927 |
|
| 928 |
with proxy_from_url(proxy_url, ca_certs=proxy.ca_certs) as https: |
| 929 |
with pytest.raises(MaxRetryError) as e: |
| 930 |
https.request("GET", destination_url) |
| 931 |
assert isinstance(e.value.reason, ProxyError) |
| 932 |
|
| 933 |
ssl_error = e.value.reason.original_error |
| 934 |
assert isinstance(ssl_error, SSLError) |
| 935 |
assert ( |
| 936 |
"no appropriate subjectAltName fields were found" in str(ssl_error) |
| 937 |
or "Hostname mismatch, certificate is not valid for 'localhost'" |
| 938 |
in str(ssl_error) |
| 939 |
or "Empty Subject Alternative Name extension" in str(ssl_error) |
| 940 |
) |
| 941 |
|
| 942 |
def test_https_proxy_no_san_hostname_checks_common_name( |
| 943 |
self, no_san_proxy_with_server: tuple[ServerConfig, ServerConfig] |
| 944 |
) -> None: |
| 945 |
proxy, server = no_san_proxy_with_server |
| 946 |
proxy_url = f"https://{proxy.host}:{proxy.port}" |
| 947 |
destination_url = f"https://{server.host}:{server.port}" |
| 948 |
|
| 949 |
proxy_ctx = urllib3.util.ssl_.create_urllib3_context(verify_flags=0) |
| 950 |
try: |
| 951 |
proxy_ctx.hostname_checks_common_name = True |
| 952 |
# PyPy doesn't like us setting 'hostname_checks_common_name' |
| 953 |
# but also has it enabled by default so we need to handle that. |
| 954 |
except AttributeError: |
| 955 |
pass |
| 956 |
if getattr(proxy_ctx, "hostname_checks_common_name", False) is not True: |
| 957 |
pytest.skip("Test requires 'SSLContext.hostname_checks_common_name=True'") |
| 958 |
|
| 959 |
with proxy_from_url( |
| 960 |
proxy_url, ca_certs=proxy.ca_certs, proxy_ssl_context=proxy_ctx |
| 961 |
) as https: |
| 962 |
https.request("GET", destination_url) |