forked from urllib3/urllib3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_proxy_poolmanager.py
746 lines (647 loc) · 29.1 KB
/
test_proxy_poolmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
import os.path
import shutil
import socket
import ssl
import tempfile
from test import LONG_TIMEOUT, SHORT_TIMEOUT, onlySecureTransport, withPyOpenSSL
from test.conftest import ServerConfig
from typing import Tuple
import pytest
import trustme
import urllib3.exceptions
from dummyserver.server import DEFAULT_CA, HAS_IPV6, get_unreachable_address
from dummyserver.testcase import HTTPDummyProxyTestCase, IPv6HTTPDummyProxyTestCase
from urllib3 import HTTPResponse
from urllib3._collections import HTTPHeaderDict
from urllib3.connection import VerifiedHTTPSConnection
from urllib3.connectionpool import connection_from_url
from urllib3.exceptions import (
ConnectTimeoutError,
MaxRetryError,
ProxyError,
ProxySchemeUnknown,
ProxySchemeUnsupported,
ReadTimeoutError,
SSLError,
)
from urllib3.poolmanager import ProxyManager, proxy_from_url
from urllib3.util.ssl_ import create_urllib3_context
from urllib3.util.timeout import Timeout
from .. import TARPIT_HOST, requires_network
# Retry failed tests
pytestmark = pytest.mark.flaky
class TestHTTPProxyManager(HTTPDummyProxyTestCase):
@classmethod
def setup_class(cls) -> None:
super().setup_class()
cls.http_url = f"http://{cls.http_host}:{int(cls.http_port)}"
cls.http_url_alt = f"http://{cls.http_host_alt}:{int(cls.http_port)}"
cls.https_url = f"https://{cls.https_host}:{int(cls.https_port)}"
cls.https_url_alt = f"https://{cls.https_host_alt}:{int(cls.https_port)}"
cls.proxy_url = f"http://{cls.proxy_host}:{int(cls.proxy_port)}"
cls.https_proxy_url = f"https://{cls.proxy_host}:{int(cls.https_proxy_port)}"
# Generate another CA to test verification failure
cls.certs_dir = tempfile.mkdtemp()
bad_ca = trustme.CA()
cls.bad_ca_path = os.path.join(cls.certs_dir, "ca_bad.pem")
bad_ca.cert_pem.write_to_path(cls.bad_ca_path)
@classmethod
def teardown_class(cls) -> None:
super().teardown_class()
shutil.rmtree(cls.certs_dir)
def test_basic_proxy(self) -> None:
with proxy_from_url(self.proxy_url, ca_certs=DEFAULT_CA) as http:
r = http.request("GET", f"{self.http_url}/")
assert r.status == 200
r = http.request("GET", f"{self.https_url}/")
assert r.status == 200
def test_https_proxy(self) -> None:
with proxy_from_url(self.https_proxy_url, ca_certs=DEFAULT_CA) as https:
r = https.request("GET", f"{self.https_url}/")
assert r.status == 200
r = https.request("GET", f"{self.http_url}/")
assert r.status == 200
def test_https_proxy_with_proxy_ssl_context(self) -> None:
proxy_ssl_context = create_urllib3_context()
proxy_ssl_context.load_verify_locations(DEFAULT_CA)
with proxy_from_url(
self.https_proxy_url,
proxy_ssl_context=proxy_ssl_context,
ca_certs=DEFAULT_CA,
) as https:
r = https.request("GET", f"{self.https_url}/")
assert r.status == 200
r = https.request("GET", f"{self.http_url}/")
assert r.status == 200
@withPyOpenSSL
def test_https_proxy_pyopenssl_not_supported(self) -> None:
with proxy_from_url(self.https_proxy_url, ca_certs=DEFAULT_CA) as https:
r = https.request("GET", f"{self.http_url}/")
assert r.status == 200
with pytest.raises(
ProxySchemeUnsupported, match="isn't available on non-native SSLContext"
):
https.request("GET", f"{self.https_url}/")
@onlySecureTransport()
def test_https_proxy_securetransport_not_supported(self) -> None:
with proxy_from_url(self.https_proxy_url, ca_certs=DEFAULT_CA) as https:
r = https.request("GET", f"{self.http_url}/")
assert r.status == 200
with pytest.raises(
ProxySchemeUnsupported, match="isn't available on non-native SSLContext"
):
https.request("GET", f"{self.https_url}/")
def test_https_proxy_forwarding_for_https(self) -> None:
with proxy_from_url(
self.https_proxy_url,
ca_certs=DEFAULT_CA,
use_forwarding_for_https=True,
) as https:
r = https.request("GET", f"{self.http_url}/")
assert r.status == 200
r = https.request("GET", f"{self.https_url}/")
assert r.status == 200
def test_nagle_proxy(self) -> None:
"""Test that proxy connections do not have TCP_NODELAY turned on"""
with ProxyManager(self.proxy_url) as http:
hc2 = http.connection_from_host(self.http_host, self.http_port)
conn = hc2._get_conn()
try:
hc2._make_request(conn, "GET", "/")
tcp_nodelay_setting = conn.sock.getsockopt(
socket.IPPROTO_TCP, socket.TCP_NODELAY
)
assert tcp_nodelay_setting == 0, (
"Expected TCP_NODELAY for proxies to be set "
"to zero, instead was %s" % tcp_nodelay_setting
)
finally:
conn.close()
@pytest.mark.parametrize("proxy_scheme", ["http", "https"])
@pytest.mark.parametrize("target_scheme", ["http", "https"])
def test_proxy_conn_fail_from_dns(
self, proxy_scheme: str, target_scheme: str
) -> None:
host, port = get_unreachable_address()
with proxy_from_url(
f"{proxy_scheme}://{host}:{port}/", retries=1, timeout=LONG_TIMEOUT
) as http:
if target_scheme == "https":
target_url = self.https_url
else:
target_url = self.http_url
with pytest.raises(MaxRetryError) as e:
http.request("GET", f"{target_url}/")
assert type(e.value.reason) == ProxyError
assert (
type(e.value.reason.original_error)
== urllib3.exceptions.NameResolutionError
)
def test_oldapi(self) -> None:
with ProxyManager(
connection_from_url(self.proxy_url), ca_certs=DEFAULT_CA # type: ignore[arg-type]
) as http:
r = http.request("GET", f"{self.http_url}/")
assert r.status == 200
r = http.request("GET", f"{self.https_url}/")
assert r.status == 200
def test_proxy_verified(self) -> None:
with proxy_from_url(
self.proxy_url, cert_reqs="REQUIRED", ca_certs=self.bad_ca_path
) as http:
https_pool = http._new_pool("https", self.https_host, self.https_port)
with pytest.raises(MaxRetryError) as e:
https_pool.request("GET", "/", retries=0)
assert isinstance(e.value.reason, SSLError)
assert (
"certificate verify failed" in str(e.value.reason)
# PyPy is more specific
or "self signed certificate in certificate chain" in str(e.value.reason)
), f"Expected 'certificate verify failed', instead got: {e.value.reason!r}"
http = proxy_from_url(
self.proxy_url, cert_reqs="REQUIRED", ca_certs=DEFAULT_CA
)
https_pool = http._new_pool("https", self.https_host, self.https_port)
conn = https_pool._new_conn()
assert conn.__class__ == VerifiedHTTPSConnection
https_pool.request("GET", "/") # Should succeed without exceptions.
http = proxy_from_url(
self.proxy_url, cert_reqs="REQUIRED", ca_certs=DEFAULT_CA
)
https_fail_pool = http._new_pool("https", "127.0.0.1", self.https_port)
with pytest.raises(
MaxRetryError, match="doesn't match|IP address mismatch"
) as e:
https_fail_pool.request("GET", "/", retries=0)
assert isinstance(e.value.reason, SSLError)
def test_redirect(self) -> None:
with proxy_from_url(self.proxy_url) as http:
r = http.request(
"GET",
f"{self.http_url}/redirect",
fields={"target": f"{self.http_url}/"},
redirect=False,
)
assert r.status == 303
r = http.request(
"GET",
f"{self.http_url}/redirect",
fields={"target": f"{self.http_url}/"},
)
assert r.status == 200
assert r.data == b"Dummy server!"
def test_cross_host_redirect(self) -> None:
with proxy_from_url(self.proxy_url) as http:
cross_host_location = f"{self.http_url_alt}/echo?a=b"
with pytest.raises(MaxRetryError):
http.request(
"GET",
f"{self.http_url}/redirect",
fields={"target": cross_host_location},
retries=0,
)
r = http.request(
"GET",
f"{self.http_url}/redirect",
fields={"target": f"{self.http_url_alt}/echo?a=b"},
retries=1,
)
assert isinstance(r, HTTPResponse)
assert r._pool is not None
assert r._pool.host != self.http_host_alt
def test_cross_protocol_redirect(self) -> None:
with proxy_from_url(self.proxy_url, ca_certs=DEFAULT_CA) as http:
cross_protocol_location = f"{self.https_url}/echo?a=b"
with pytest.raises(MaxRetryError):
http.request(
"GET",
f"{self.http_url}/redirect",
fields={"target": cross_protocol_location},
retries=0,
)
r = http.request(
"GET",
f"{self.http_url}/redirect",
fields={"target": f"{self.https_url}/echo?a=b"},
retries=1,
)
assert isinstance(r, HTTPResponse)
assert r._pool is not None
assert r._pool.host == self.https_host
def test_headers(self) -> None:
with proxy_from_url(
self.proxy_url,
headers={"Foo": "bar"},
proxy_headers={"Hickory": "dickory"},
ca_certs=DEFAULT_CA,
) as http:
r = http.request_encode_url("GET", f"{self.http_url}/headers")
returned_headers = r.json()
assert returned_headers.get("Foo") == "bar"
assert returned_headers.get("Hickory") == "dickory"
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}"
r = http.request_encode_url("GET", f"{self.http_url_alt}/headers")
returned_headers = r.json()
assert returned_headers.get("Foo") == "bar"
assert returned_headers.get("Hickory") == "dickory"
assert (
returned_headers.get("Host") == f"{self.http_host_alt}:{self.http_port}"
)
r = http.request_encode_url("GET", f"{self.https_url}/headers")
returned_headers = r.json()
assert returned_headers.get("Foo") == "bar"
assert returned_headers.get("Hickory") is None
assert (
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}"
)
r = http.request_encode_body("POST", f"{self.http_url}/headers")
returned_headers = r.json()
assert returned_headers.get("Foo") == "bar"
assert returned_headers.get("Hickory") == "dickory"
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}"
r = http.request_encode_url(
"GET", f"{self.http_url}/headers", headers={"Baz": "quux"}
)
returned_headers = r.json()
assert returned_headers.get("Foo") is None
assert returned_headers.get("Baz") == "quux"
assert returned_headers.get("Hickory") == "dickory"
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}"
r = http.request_encode_url(
"GET", f"{self.https_url}/headers", headers={"Baz": "quux"}
)
returned_headers = r.json()
assert returned_headers.get("Foo") is None
assert returned_headers.get("Baz") == "quux"
assert returned_headers.get("Hickory") is None
assert (
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}"
)
r = http.request_encode_body(
"GET", f"{self.http_url}/headers", headers={"Baz": "quux"}
)
returned_headers = r.json()
assert returned_headers.get("Foo") is None
assert returned_headers.get("Baz") == "quux"
assert returned_headers.get("Hickory") == "dickory"
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}"
r = http.request_encode_body(
"GET", f"{self.https_url}/headers", headers={"Baz": "quux"}
)
returned_headers = r.json()
assert returned_headers.get("Foo") is None
assert returned_headers.get("Baz") == "quux"
assert returned_headers.get("Hickory") is None
assert (
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}"
)
def test_https_headers(self) -> None:
with proxy_from_url(
self.https_proxy_url,
headers={"Foo": "bar"},
proxy_headers={"Hickory": "dickory"},
ca_certs=DEFAULT_CA,
) as http:
r = http.request_encode_url("GET", f"{self.http_url}/headers")
returned_headers = r.json()
assert returned_headers.get("Foo") == "bar"
assert returned_headers.get("Hickory") == "dickory"
assert returned_headers.get("Host") == f"{self.http_host}:{self.http_port}"
r = http.request_encode_url("GET", f"{self.http_url_alt}/headers")
returned_headers = r.json()
assert returned_headers.get("Foo") == "bar"
assert returned_headers.get("Hickory") == "dickory"
assert (
returned_headers.get("Host") == f"{self.http_host_alt}:{self.http_port}"
)
r = http.request_encode_body(
"GET", f"{self.https_url}/headers", headers={"Baz": "quux"}
)
returned_headers = r.json()
assert returned_headers.get("Foo") is None
assert returned_headers.get("Baz") == "quux"
assert returned_headers.get("Hickory") is None
assert (
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}"
)
def test_https_headers_forwarding_for_https(self) -> None:
with proxy_from_url(
self.https_proxy_url,
headers={"Foo": "bar"},
proxy_headers={"Hickory": "dickory"},
ca_certs=DEFAULT_CA,
use_forwarding_for_https=True,
) as http:
r = http.request_encode_url("GET", f"{self.https_url}/headers")
returned_headers = r.json()
assert returned_headers.get("Foo") == "bar"
assert returned_headers.get("Hickory") == "dickory"
assert (
returned_headers.get("Host") == f"{self.https_host}:{self.https_port}"
)
def test_headerdict(self) -> None:
default_headers = HTTPHeaderDict(a="b")
proxy_headers = HTTPHeaderDict()
proxy_headers.add("foo", "bar")
with proxy_from_url(
self.proxy_url, headers=default_headers, proxy_headers=proxy_headers
) as http:
request_headers = HTTPHeaderDict(baz="quux")
r = http.request("GET", f"{self.http_url}/headers", headers=request_headers)
returned_headers = r.json()
assert returned_headers.get("Foo") == "bar"
assert returned_headers.get("Baz") == "quux"
def test_proxy_pooling(self) -> None:
with proxy_from_url(self.proxy_url, cert_reqs="NONE") as http:
for x in range(2):
http.urlopen("GET", self.http_url)
assert len(http.pools) == 1
for x in range(2):
http.urlopen("GET", self.http_url_alt)
assert len(http.pools) == 1
for x in range(2):
http.urlopen("GET", self.https_url)
assert len(http.pools) == 2
for x in range(2):
http.urlopen("GET", self.https_url_alt)
assert len(http.pools) == 3
def test_proxy_pooling_ext(self) -> None:
with proxy_from_url(self.proxy_url) as http:
hc1 = http.connection_from_url(self.http_url)
hc2 = http.connection_from_host(self.http_host, self.http_port)
hc3 = http.connection_from_url(self.http_url_alt)
hc4 = http.connection_from_host(self.http_host_alt, self.http_port)
assert hc1 == hc2
assert hc2 == hc3
assert hc3 == hc4
sc1 = http.connection_from_url(self.https_url)
sc2 = http.connection_from_host(
self.https_host, self.https_port, scheme="https"
)
sc3 = http.connection_from_url(self.https_url_alt)
sc4 = http.connection_from_host(
self.https_host_alt, self.https_port, scheme="https"
)
assert sc1 == sc2
assert sc2 != sc3
assert sc3 == sc4
@requires_network()
@pytest.mark.parametrize(
["proxy_scheme", "target_scheme", "use_forwarding_for_https"],
[
("http", "http", False),
("https", "http", False),
# 'use_forwarding_for_https' is only valid for HTTPS+HTTPS.
("https", "https", True),
],
)
def test_forwarding_proxy_request_timeout(
self, proxy_scheme: str, target_scheme: str, use_forwarding_for_https: bool
) -> None:
proxy_url = self.https_proxy_url if proxy_scheme == "https" else self.proxy_url
target_url = f"{target_scheme}://{TARPIT_HOST}"
with proxy_from_url(
proxy_url,
ca_certs=DEFAULT_CA,
use_forwarding_for_https=use_forwarding_for_https,
) as proxy:
with pytest.raises(MaxRetryError) as e:
timeout = Timeout(connect=LONG_TIMEOUT, read=SHORT_TIMEOUT)
proxy.request("GET", target_url, timeout=timeout)
# We sent the request to the proxy but didn't get any response
# so we're not sure if that's being caused by the proxy or the
# target so we put the blame on the target.
assert type(e.value.reason) == ReadTimeoutError
@requires_network()
@pytest.mark.parametrize(
["proxy_scheme", "target_scheme"], [("http", "https"), ("https", "https")]
)
def test_tunneling_proxy_request_timeout(
self, proxy_scheme: str, target_scheme: str
) -> None:
proxy_url = self.https_proxy_url if proxy_scheme == "https" else self.proxy_url
target_url = f"{target_scheme}://{TARPIT_HOST}"
with proxy_from_url(
proxy_url,
ca_certs=DEFAULT_CA,
) as proxy:
with pytest.raises(MaxRetryError) as e:
timeout = Timeout(connect=LONG_TIMEOUT, read=SHORT_TIMEOUT)
proxy.request("GET", target_url, timeout=timeout)
assert type(e.value.reason) == ReadTimeoutError
@requires_network()
@pytest.mark.parametrize(
["proxy_scheme", "target_scheme", "use_forwarding_for_https"],
[
("http", "http", False),
("https", "http", False),
# 'use_forwarding_for_https' is only valid for HTTPS+HTTPS.
("https", "https", True),
],
)
def test_forwarding_proxy_connect_timeout(
self, proxy_scheme: str, target_scheme: str, use_forwarding_for_https: bool
) -> None:
proxy_url = f"{proxy_scheme}://{TARPIT_HOST}"
target_url = self.https_url if target_scheme == "https" else self.http_url
with proxy_from_url(
proxy_url,
ca_certs=DEFAULT_CA,
timeout=SHORT_TIMEOUT,
use_forwarding_for_https=use_forwarding_for_https,
) as proxy:
with pytest.raises(MaxRetryError) as e:
proxy.request("GET", target_url)
assert type(e.value.reason) == ProxyError
assert type(e.value.reason.original_error) == ConnectTimeoutError
@requires_network()
@pytest.mark.parametrize(
["proxy_scheme", "target_scheme"], [("http", "https"), ("https", "https")]
)
def test_tunneling_proxy_connect_timeout(
self, proxy_scheme: str, target_scheme: str
) -> None:
proxy_url = f"{proxy_scheme}://{TARPIT_HOST}"
target_url = self.https_url if target_scheme == "https" else self.http_url
with proxy_from_url(
proxy_url, ca_certs=DEFAULT_CA, timeout=SHORT_TIMEOUT
) as proxy:
with pytest.raises(MaxRetryError) as e:
proxy.request("GET", target_url)
assert type(e.value.reason) == ProxyError
assert type(e.value.reason.original_error) == ConnectTimeoutError
@requires_network()
@pytest.mark.parametrize(
["target_scheme", "use_forwarding_for_https"],
[
("http", False),
("https", False),
("https", True),
],
)
def test_https_proxy_tls_error(
self, target_scheme: str, use_forwarding_for_https: str
) -> None:
target_url = self.https_url if target_scheme == "https" else self.http_url
proxy_ctx = ssl.create_default_context()
with proxy_from_url(
self.https_proxy_url,
proxy_ssl_context=proxy_ctx,
use_forwarding_for_https=use_forwarding_for_https,
) as proxy:
with pytest.raises(MaxRetryError) as e:
proxy.request("GET", target_url)
assert type(e.value.reason) == ProxyError
assert type(e.value.reason.original_error) == SSLError
@requires_network()
@pytest.mark.parametrize(
["proxy_scheme", "use_forwarding_for_https"],
[
("http", False),
("https", False),
("https", True),
],
)
def test_proxy_https_target_tls_error(
self, proxy_scheme: str, use_forwarding_for_https: str
) -> None:
if proxy_scheme == "https" and use_forwarding_for_https:
pytest.skip("Test is expected to fail due to urllib3/urllib3#2577")
proxy_url = self.https_proxy_url if proxy_scheme == "https" else self.proxy_url
proxy_ctx = ssl.create_default_context()
proxy_ctx.load_verify_locations(DEFAULT_CA)
ctx = ssl.create_default_context()
with proxy_from_url(
proxy_url,
proxy_ssl_context=proxy_ctx,
ssl_context=ctx,
use_forwarding_for_https=use_forwarding_for_https,
) as proxy:
with pytest.raises(MaxRetryError) as e:
proxy.request("GET", self.https_url)
assert type(e.value.reason) == SSLError
def test_scheme_host_case_insensitive(self) -> None:
"""Assert that upper-case schemes and hosts are normalized."""
with proxy_from_url(self.proxy_url.upper(), ca_certs=DEFAULT_CA) as http:
r = http.request("GET", f"{self.http_url.upper()}/")
assert r.status == 200
r = http.request("GET", f"{self.https_url.upper()}/")
assert r.status == 200
@pytest.mark.parametrize(
"url, error_msg",
[
(
"127.0.0.1",
"Proxy URL had no scheme, should start with http:// or https://",
),
(
"localhost:8080",
"Proxy URL had no scheme, should start with http:// or https://",
),
(
"ftp://google.com",
"Proxy URL had unsupported scheme ftp, should use http:// or https://",
),
],
)
def test_invalid_schema(self, url: str, error_msg: str) -> None:
with pytest.raises(ProxySchemeUnknown, match=error_msg):
proxy_from_url(url)
@pytest.mark.skipif(not HAS_IPV6, reason="Only runs on IPv6 systems")
class TestIPv6HTTPProxyManager(IPv6HTTPDummyProxyTestCase):
@classmethod
def setup_class(cls) -> None:
HTTPDummyProxyTestCase.setup_class()
cls.http_url = f"http://{cls.http_host}:{int(cls.http_port)}"
cls.http_url_alt = f"http://{cls.http_host_alt}:{int(cls.http_port)}"
cls.https_url = f"https://{cls.https_host}:{int(cls.https_port)}"
cls.https_url_alt = f"https://{cls.https_host_alt}:{int(cls.https_port)}"
cls.proxy_url = f"http://[{cls.proxy_host}]:{int(cls.proxy_port)}"
def test_basic_ipv6_proxy(self) -> None:
with proxy_from_url(self.proxy_url, ca_certs=DEFAULT_CA) as http:
r = http.request("GET", f"{self.http_url}/")
assert r.status == 200
r = http.request("GET", f"{self.https_url}/")
assert r.status == 200
class TestHTTPSProxyVerification:
def test_https_proxy_hostname_verification(
self, no_localhost_san_server: ServerConfig
) -> None:
bad_server = no_localhost_san_server
bad_proxy_url = f"https://{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, ProxyError)
ssl_error = e.value.reason.original_error
assert isinstance(ssl_error, SSLError)
assert "hostname 'localhost' doesn't match" in str(
ssl_error
) or "Hostname mismatch" in str(ssl_error)
with pytest.raises(MaxRetryError) as e:
https.request("GET", "https://%s/" % test_url)
assert isinstance(e.value.reason, ProxyError)
ssl_error = e.value.reason.original_error
assert isinstance(ssl_error, SSLError)
assert "hostname 'localhost' doesn't match" in str(
ssl_error
) or "Hostname mismatch" in str(ssl_error)
def test_https_proxy_ipv4_san(
self, ipv4_san_proxy_with_server: Tuple[ServerConfig, ServerConfig]
) -> None:
proxy, server = ipv4_san_proxy_with_server
proxy_url = f"https://{proxy.host}:{proxy.port}"
destination_url = f"https://{server.host}:{server.port}"
with proxy_from_url(proxy_url, ca_certs=proxy.ca_certs) as https:
r = https.request("GET", destination_url)
assert r.status == 200
def test_https_proxy_ipv6_san(
self, ipv6_san_proxy_with_server: Tuple[ServerConfig, ServerConfig]
) -> None:
proxy, server = ipv6_san_proxy_with_server
proxy_url = f"https://[{proxy.host}]:{proxy.port}"
destination_url = f"https://{server.host}:{server.port}"
with proxy_from_url(proxy_url, ca_certs=proxy.ca_certs) as https:
r = https.request("GET", destination_url)
assert r.status == 200
@pytest.mark.parametrize("target_scheme", ["http", "https"])
def test_https_proxy_no_san(
self,
no_san_proxy_with_server: Tuple[ServerConfig, ServerConfig],
target_scheme: str,
) -> None:
proxy, server = no_san_proxy_with_server
proxy_url = f"https://{proxy.host}:{proxy.port}"
destination_url = f"{target_scheme}://{server.host}:{server.port}"
with proxy_from_url(proxy_url, ca_certs=proxy.ca_certs) as https:
with pytest.raises(MaxRetryError) as e:
https.request("GET", destination_url)
assert isinstance(e.value.reason, ProxyError)
ssl_error = e.value.reason.original_error
assert isinstance(ssl_error, SSLError)
assert "no appropriate subjectAltName fields were found" in str(
ssl_error
) or "Hostname mismatch, certificate is not valid for 'localhost'" in str(
ssl_error
)
def test_https_proxy_no_san_hostname_checks_common_name(
self, no_san_proxy_with_server: Tuple[ServerConfig, ServerConfig]
) -> None:
proxy, server = no_san_proxy_with_server
proxy_url = f"https://{proxy.host}:{proxy.port}"
destination_url = f"https://{server.host}:{server.port}"
proxy_ctx = urllib3.util.ssl_.create_urllib3_context()
try:
proxy_ctx.hostname_checks_common_name = True
# PyPy doesn't like us setting 'hostname_checks_common_name'
# but also has it enabled by default so we need to handle that.
except AttributeError:
pass
if getattr(proxy_ctx, "hostname_checks_common_name", False) is not True:
pytest.skip("Test requires 'SSLContext.hostname_checks_common_name=True'")
with proxy_from_url(
proxy_url, ca_certs=proxy.ca_certs, proxy_ssl_context=proxy_ctx
) as https:
https.request("GET", destination_url)