Skip to content

Commit 31702b2

Browse files
authored
explicitly throw error for socks5 proxy since is not supported supported currently and will be implied disconnected if connect to (aio-libs#10147)
1 parent c2f4ac4 commit 31702b2

5 files changed

Lines changed: 28 additions & 14 deletions

File tree

aiohttp/client.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,13 @@
8282
ClientWebSocketResponse,
8383
ClientWSTimeout,
8484
)
85-
from .connector import (
86-
HTTP_AND_EMPTY_SCHEMA_SET,
87-
BaseConnector,
88-
NamedPipeConnector,
89-
TCPConnector,
90-
UnixConnector,
91-
)
85+
from .connector import BaseConnector, NamedPipeConnector, TCPConnector, UnixConnector
9286
from .cookiejar import CookieJar
9387
from .helpers import (
9488
_SENTINEL,
9589
DEFAULT_CHUNK_SIZE,
9690
EMPTY_BODY_METHODS,
91+
HTTP_AND_EMPTY_SCHEMA_SET,
9792
TimeoutHandle,
9893
_auth_header_from_netrc,
9994
frozen_dataclass_decorator,

aiohttp/client_reqrep.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from .formdata import FormData
3737
from .helpers import (
3838
_SENTINEL,
39+
HTTP_AND_EMPTY_SCHEMA_SET,
3940
BaseTimerContext,
4041
HeadersDictProxy,
4142
HeadersMixin,
@@ -1295,6 +1296,13 @@ def _update_proxy(
12951296
self.proxy = None
12961297
self.proxy_headers = None
12971298
return
1299+
1300+
if proxy.scheme not in HTTP_AND_EMPTY_SCHEMA_SET:
1301+
raise ValueError(
1302+
f"aiohttp only supports http(s) proxies (got: {proxy.scheme!r}).\n"
1303+
"See third-party libraries for other proxy schemes."
1304+
)
1305+
12981306
# URL-embedded credentials on the proxy map to Proxy-Authorization.
12991307
if proxy.raw_user or proxy.raw_password:
13001308
auth_header = encode_basic_auth(proxy.user or "", proxy.password or "")

aiohttp/connector.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
)
4444
from .helpers import (
4545
_SENTINEL,
46+
HIGH_LEVEL_SCHEMA_SET,
4647
ceil_timeout,
4748
is_canonical_ipv4_address,
4849
is_ip_address,
@@ -66,13 +67,6 @@
6667
ssl = None # type: ignore[assignment]
6768
SSLContext = object # type: ignore[misc,assignment]
6869

69-
EMPTY_SCHEMA_SET = frozenset({""})
70-
HTTP_SCHEMA_SET = frozenset({"http", "https"})
71-
WS_SCHEMA_SET = frozenset({"ws", "wss"})
72-
73-
HTTP_AND_EMPTY_SCHEMA_SET = HTTP_SCHEMA_SET | EMPTY_SCHEMA_SET
74-
HIGH_LEVEL_SCHEMA_SET = HTTP_AND_EMPTY_SCHEMA_SET | WS_SCHEMA_SET
75-
7670
NEEDS_CLEANUP_CLOSED = (3, 13, 0) <= sys.version_info < (
7771
3,
7872
13,

aiohttp/helpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@
119119
)
120120

121121

122+
EMPTY_SCHEMA_SET = frozenset({""})
123+
HTTP_SCHEMA_SET = frozenset({"http", "https"})
124+
WS_SCHEMA_SET = frozenset({"ws", "wss"})
125+
HTTP_AND_EMPTY_SCHEMA_SET = HTTP_SCHEMA_SET | EMPTY_SCHEMA_SET
126+
HIGH_LEVEL_SCHEMA_SET = HTTP_AND_EMPTY_SCHEMA_SET | WS_SCHEMA_SET
127+
128+
122129
CHAR = {chr(i) for i in range(0, 128)}
123130
CTL = {chr(i) for i in range(0, 32)} | {
124131
chr(127),

tests/test_client_request.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ async def test_hostname_err(make_client_request: _RequestMaker) -> None:
242242
make_client_request("get", URL("http://:8080/"))
243243

244244

245+
@pytest.mark.parametrize("scheme", ("socks5", "socks5h"))
246+
async def test_proxy_scheme_err(
247+
make_client_request: _RequestMaker, scheme: str
248+
) -> None:
249+
with pytest.raises(ValueError, match=f"'{scheme}'"):
250+
make_client_request(
251+
"get", URL("http://py.org/"), proxy=URL(f"{scheme}://127.0.0.1:80")
252+
)
253+
254+
245255
async def test_host_header_host_first(make_client_request: _RequestMaker) -> None:
246256
req = make_client_request("get", URL("http://python.org/"))
247257
assert list(req.headers)[0] == hdrs.HOST

0 commit comments

Comments
 (0)