Skip to content

Commit a003df3

Browse files
bdracolezgomatt
andauthored
[PR #10726/feff48d backport][3.11] Disable TLS in TLS warning for uvloop (#10767)
Co-authored-by: Matthew Go <[email protected]>
1 parent f69333d commit a003df3

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

CHANGES/7686.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disabled TLS in TLS warning (when using HTTPS proxies) for uvloop and newer Python versions -- by :user:`lezgomatt`.

CONTRIBUTORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Martin Sucha
241241
Mathias Fröjdman
242242
Mathieu Dugré
243243
Matt VanEseltine
244+
Matthew Go
244245
Matthias Marquardt
245246
Matthieu Hauglustaine
246247
Matthieu Rigal

aiohttp/connector.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,13 @@ def _warn_about_tls_in_tls(
12031203
if req.request_info.url.scheme != "https":
12041204
return
12051205

1206-
asyncio_supports_tls_in_tls = getattr(
1206+
# Check if uvloop is being used, which supports TLS in TLS,
1207+
# otherwise assume that asyncio's native transport is being used.
1208+
if type(underlying_transport).__module__.startswith("uvloop"):
1209+
return
1210+
1211+
# Support in asyncio was added in Python 3.11 (bpo-44011)
1212+
asyncio_supports_tls_in_tls = sys.version_info >= (3, 11) or getattr(
12071213
underlying_transport,
12081214
"_start_tls_compatible",
12091215
False,

tests/conftest.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from hashlib import md5, sha1, sha256
88
from pathlib import Path
99
from tempfile import TemporaryDirectory
10-
from typing import Any, Generator
10+
from typing import Any, Generator, Iterator
1111
from unittest import mock
1212
from uuid import uuid4
1313

@@ -27,6 +27,12 @@
2727
except ImportError:
2828
TRUSTME = False
2929

30+
31+
try:
32+
import uvloop
33+
except ImportError:
34+
uvloop = None # type: ignore[assignment]
35+
3036
pytest_plugins = ["aiohttp.pytest_plugin", "pytester"]
3137

3238
IS_HPUX = sys.platform.startswith("hp-ux")
@@ -193,6 +199,16 @@ def selector_loop():
193199
yield _loop
194200

195201

202+
@pytest.fixture
203+
def uvloop_loop() -> Iterator[asyncio.AbstractEventLoop]:
204+
policy = uvloop.EventLoopPolicy()
205+
asyncio.set_event_loop_policy(policy)
206+
207+
with loop_context(policy.new_event_loop) as _loop:
208+
asyncio.set_event_loop(_loop)
209+
yield _loop
210+
211+
196212
@pytest.fixture
197213
def netrc_contents(
198214
tmp_path: Path,

tests/test_proxy_functional.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import os
33
import pathlib
4+
import platform
45
import ssl
56
import sys
67
from re import match as match_regex
@@ -202,6 +203,32 @@ async def test_https_proxy_unsupported_tls_in_tls(
202203
await asyncio.sleep(0.1)
203204

204205

206+
@pytest.mark.usefixtures("uvloop_loop")
207+
@pytest.mark.skipif(
208+
platform.system() == "Windows" or sys.implementation.name != "cpython",
209+
reason="uvloop is not supported on Windows and non-CPython implementations",
210+
)
211+
@pytest.mark.filterwarnings(r"ignore:.*ssl.OP_NO_SSL*")
212+
# Filter out the warning from
213+
# https://github.com/abhinavsingh/proxy.py/blob/30574fd0414005dfa8792a6e797023e862bdcf43/proxy/common/utils.py#L226
214+
# otherwise this test will fail because the proxy will die with an error.
215+
async def test_uvloop_secure_https_proxy(
216+
client_ssl_ctx: ssl.SSLContext,
217+
secure_proxy_url: URL,
218+
) -> None:
219+
"""Ensure HTTPS sites are accessible through a secure proxy without warning when using uvloop."""
220+
conn = aiohttp.TCPConnector()
221+
sess = aiohttp.ClientSession(connector=conn)
222+
url = URL("https://example.com")
223+
224+
async with sess.get(url, proxy=secure_proxy_url, ssl=client_ssl_ctx) as response:
225+
assert response.status == 200
226+
227+
await sess.close()
228+
await conn.close()
229+
await asyncio.sleep(0.1)
230+
231+
205232
@pytest.fixture
206233
def proxy_test_server(aiohttp_raw_server, loop, monkeypatch):
207234
# Handle all proxy requests and imitate remote server response.

0 commit comments

Comments
 (0)