Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #9368/02d8dba9 backport][3.10] Avoid using the proxy headers in the ConnectionKey if no proxy is in use #9378

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES/9368.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed proxy headers being used in the ``ConnectionKey`` hash when proxy was being used -- by :user:`bdraco`.
bdraco marked this conversation as resolved.
Show resolved Hide resolved
bdraco marked this conversation as resolved.
Show resolved Hide resolved

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.
8 changes: 7 additions & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,16 @@ def update_proxy(
proxy_auth: Optional[BasicAuth],
proxy_headers: Optional[LooseHeaders],
) -> None:
self.proxy = proxy
if proxy is None:
self.proxy_auth = None
self.proxy_headers = None
return

if proxy_auth and not isinstance(proxy_auth, helpers.BasicAuth):
raise ValueError("proxy_auth must be None or BasicAuth() tuple")
self.proxy = proxy
self.proxy_auth = proxy_auth

if proxy_headers is not None and not isinstance(
proxy_headers, (MultiDict, MultiDictProxy)
):
Expand Down
27 changes: 27 additions & 0 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1467,3 +1467,30 @@ def test_basicauth_from_empty_netrc(
"""Test that no Authorization header is sent when netrc is empty"""
req = make_request("get", "http://example.com", trust_env=True)
assert hdrs.AUTHORIZATION not in req.headers


async def test_connection_key_with_proxy() -> None:
"""Verify the proxy headers are included in the ConnectionKey when a proxy is used."""
proxy = URL("http://proxy.example.com")
req = ClientRequest(
"GET",
URL("http://example.com"),
proxy=proxy,
proxy_headers={"X-Proxy": "true"},
loop=asyncio.get_running_loop(),
)
assert req.connection_key.proxy_headers_hash is not None
await req.close()


async def test_connection_key_without_proxy() -> None:
"""Verify the proxy headers are not included in the ConnectionKey when a proxy is used."""
# If proxy is unspecified, proxy_headers should be ignored
req = ClientRequest(
"GET",
URL("http://example.com"),
proxy_headers={"X-Proxy": "true"},
loop=asyncio.get_running_loop(),
)
assert req.connection_key.proxy_headers_hash is None
await req.close()
Loading