Skip to content

Commit 6198a56

Browse files
[PR #9368/02d8dba9 backport][3.10] Avoid using the proxy headers in the ConnectionKey if no proxy is in use (#9378)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 456cf5e commit 6198a56

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

CHANGES/9368.bugfix.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed proxy headers being used in the ``ConnectionKey`` hash when a proxy was not being used -- by :user:`bdraco`.
2+
3+
If default headers are used, they are also used for proxy headers. This could have led to creating connections that were not needed when one was already available.

aiohttp/client_reqrep.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,16 @@ def update_proxy(
626626
proxy_auth: Optional[BasicAuth],
627627
proxy_headers: Optional[LooseHeaders],
628628
) -> None:
629+
self.proxy = proxy
630+
if proxy is None:
631+
self.proxy_auth = None
632+
self.proxy_headers = None
633+
return
634+
629635
if proxy_auth and not isinstance(proxy_auth, helpers.BasicAuth):
630636
raise ValueError("proxy_auth must be None or BasicAuth() tuple")
631-
self.proxy = proxy
632637
self.proxy_auth = proxy_auth
638+
633639
if proxy_headers is not None and not isinstance(
634640
proxy_headers, (MultiDict, MultiDictProxy)
635641
):

tests/test_client_request.py

+27
Original file line numberDiff line numberDiff line change
@@ -1467,3 +1467,30 @@ def test_basicauth_from_empty_netrc(
14671467
"""Test that no Authorization header is sent when netrc is empty"""
14681468
req = make_request("get", "http://example.com", trust_env=True)
14691469
assert hdrs.AUTHORIZATION not in req.headers
1470+
1471+
1472+
async def test_connection_key_with_proxy() -> None:
1473+
"""Verify the proxy headers are included in the ConnectionKey when a proxy is used."""
1474+
proxy = URL("http://proxy.example.com")
1475+
req = ClientRequest(
1476+
"GET",
1477+
URL("http://example.com"),
1478+
proxy=proxy,
1479+
proxy_headers={"X-Proxy": "true"},
1480+
loop=asyncio.get_running_loop(),
1481+
)
1482+
assert req.connection_key.proxy_headers_hash is not None
1483+
await req.close()
1484+
1485+
1486+
async def test_connection_key_without_proxy() -> None:
1487+
"""Verify the proxy headers are not included in the ConnectionKey when a proxy is used."""
1488+
# If proxy is unspecified, proxy_headers should be ignored
1489+
req = ClientRequest(
1490+
"GET",
1491+
URL("http://example.com"),
1492+
proxy_headers={"X-Proxy": "true"},
1493+
loop=asyncio.get_running_loop(),
1494+
)
1495+
assert req.connection_key.proxy_headers_hash is None
1496+
await req.close()

0 commit comments

Comments
 (0)