| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from urllib3.exceptions import ( |
| 6 |
LocationParseError, |
| 7 |
MaxRetryError, |
| 8 |
NewConnectionError, |
| 9 |
ProxyError, |
| 10 |
) |
| 11 |
from urllib3.poolmanager import ProxyManager |
| 12 |
from urllib3.response import HTTPResponse |
| 13 |
from urllib3.util.retry import Retry |
| 14 |
from urllib3.util.url import parse_url |
| 15 |
|
| 16 |
from .port_helpers import find_unused_port |
| 17 |
|
| 18 |
|
| 19 |
class TestProxyManager: |
| 20 |
@pytest.mark.parametrize("proxy_scheme", ["http", "https"]) |
| 21 |
def test_proxy_headers(self, proxy_scheme: str) -> None: |
| 22 |
url = "http://pypi.org/project/urllib3/" |
| 23 |
proxy_url = f"{proxy_scheme}://something:1234" |
| 24 |
with ProxyManager(proxy_url) as p: |
| 25 |
# Verify default headers |
| 26 |
default_headers = {"Accept": "*/*", "Host": "pypi.org"} |
| 27 |
headers = p._set_proxy_headers(url) |
| 28 |
|
| 29 |
assert headers == default_headers |
| 30 |
|
| 31 |
# Verify default headers don't overwrite provided headers |
| 32 |
provided_headers = { |
| 33 |
"Accept": "application/json", |
| 34 |
"custom": "header", |
| 35 |
"Host": "test.python.org", |
| 36 |
} |
| 37 |
headers = p._set_proxy_headers(url, provided_headers) |
| 38 |
|
| 39 |
assert headers == provided_headers |
| 40 |
|
| 41 |
# Verify proxy with nonstandard port |
| 42 |
provided_headers = {"Accept": "application/json"} |
| 43 |
expected_headers = provided_headers.copy() |
| 44 |
expected_headers.update({"Host": "pypi.org:8080"}) |
| 45 |
url_with_port = "http://pypi.org:8080/project/urllib3/" |
| 46 |
headers = p._set_proxy_headers(url_with_port, provided_headers) |
| 47 |
|
| 48 |
assert headers == expected_headers |
| 49 |
|
| 50 |
def test_default_port(self) -> None: |
| 51 |
with ProxyManager("http://something") as p: |
| 52 |
assert p.proxy is not None |
| 53 |
assert p.proxy.port == 80 |
| 54 |
with ProxyManager("https://something") as p: |
| 55 |
assert p.proxy is not None |
| 56 |
assert p.proxy.port == 443 |
| 57 |
|
| 58 |
def test_proxy_port_zero(self) -> None: |
| 59 |
with ProxyManager("http://proxy:0") as p: |
| 60 |
assert p.proxy is not None |
| 61 |
assert p.proxy.port == 0 |
| 62 |
|
| 63 |
def test_invalid_scheme(self) -> None: |
| 64 |
with pytest.raises(AssertionError): |
| 65 |
ProxyManager("invalid://host/p") |
| 66 |
with pytest.raises(ValueError): |
| 67 |
ProxyManager("invalid://host/p") |
| 68 |
|
| 69 |
def test_proxy_tunnel(self) -> None: |
| 70 |
http_url = parse_url("http://example.com") |
| 71 |
https_url = parse_url("https://example.com") |
| 72 |
with ProxyManager("http://proxy:8080") as p: |
| 73 |
assert p._proxy_requires_url_absolute_form(http_url) |
| 74 |
assert p._proxy_requires_url_absolute_form(https_url) is False |
| 75 |
|
| 76 |
with ProxyManager("https://proxy:8080") as p: |
| 77 |
assert p._proxy_requires_url_absolute_form(http_url) |
| 78 |
assert p._proxy_requires_url_absolute_form(https_url) is False |
| 79 |
|
| 80 |
with ProxyManager("https://proxy:8080", use_forwarding_for_https=True) as p: |
| 81 |
assert p._proxy_requires_url_absolute_form(http_url) |
| 82 |
assert p._proxy_requires_url_absolute_form(https_url) |
| 83 |
|
| 84 |
@pytest.mark.parametrize("proxy_scheme", ["http", "https"]) |
| 85 |
def test_absolute_form_request_target_strips_fragment_for_custom_pool( |
| 86 |
self, proxy_scheme: str |
| 87 |
) -> None: |
| 88 |
class CustomConnectionPool: |
| 89 |
requested_urls: list[str] = [] |
| 90 |
|
| 91 |
def __init__(self, host: str, port: int | None = None, **kw: object): |
| 92 |
pass |
| 93 |
|
| 94 |
def urlopen(self, method: str, url: str, **kw: object) -> HTTPResponse: |
| 95 |
self.requested_urls.append(url) |
| 96 |
return HTTPResponse(status=200) |
| 97 |
|
| 98 |
with ProxyManager(f"{proxy_scheme}://proxy:8080") as p: |
| 99 |
p.pool_classes_by_scheme = p.pool_classes_by_scheme.copy() |
| 100 |
p.pool_classes_by_scheme[proxy_scheme] = CustomConnectionPool |
| 101 |
response = p.urlopen( |
| 102 |
"GET", |
| 103 |
"http://example.com/path?x=1#marker=value", |
| 104 |
) |
| 105 |
|
| 106 |
assert response.status == 200 |
| 107 |
assert CustomConnectionPool.requested_urls == ["http://example.com/path?x=1"] |
| 108 |
|
| 109 |
@pytest.mark.parametrize( |
| 110 |
"url", |
| 111 |
[ |
| 112 |
"https://victim.example\r\nInjected/path", |
| 113 |
"https://[::1%25%0d%0a]/", |
| 114 |
], |
| 115 |
) |
| 116 |
def test_proxy_connect_rejects_control_characters_in_tunnel_host( |
| 117 |
self, url: str |
| 118 |
) -> None: |
| 119 |
with ProxyManager("http://proxy:8080") as p: |
| 120 |
with pytest.raises(LocationParseError): |
| 121 |
p.connection_from_url(url) |
| 122 |
|
| 123 |
@pytest.mark.parametrize("host", ["foo%.example", "foo%zz.example"]) |
| 124 |
def test_proxy_connect_rejects_malformed_percent_escapes_in_tunnel_host( |
| 125 |
self, host: str |
| 126 |
) -> None: |
| 127 |
with ProxyManager("http://proxy:8080") as p: |
| 128 |
with pytest.raises(LocationParseError): |
| 129 |
p.connection_from_host(host, scheme="https") |
| 130 |
|
| 131 |
def test_proxy_connect_retry(self) -> None: |
| 132 |
retry = Retry(total=None, connect=False) |
| 133 |
port = find_unused_port() |
| 134 |
with ProxyManager(f"http://localhost:{port}") as p: |
| 135 |
with pytest.raises(ProxyError) as ei: |
| 136 |
p.urlopen("HEAD", url="http://localhost/", retries=retry) |
| 137 |
assert isinstance(ei.value.original_error, NewConnectionError) |
| 138 |
|
| 139 |
retry = Retry(total=None, connect=2) |
| 140 |
with ProxyManager(f"http://localhost:{port}") as p: |
| 141 |
with pytest.raises(MaxRetryError) as ei1: |
| 142 |
p.urlopen("HEAD", url="http://localhost/", retries=retry) |
| 143 |
assert ei1.value.reason is not None |
| 144 |
assert isinstance(ei1.value.reason, ProxyError) |
| 145 |
assert isinstance(ei1.value.reason.original_error, NewConnectionError) |