From 3b624b610b63fabd6f610166c7fd6d861d00e898 Mon Sep 17 00:00:00 2001 From: Jake Wallin Date: Tue, 20 Oct 2026 02:14:29 +0000 Subject: [PATCH] gh-155896: Allow overriding TLS roles in StreamWriter.start_tls --- Doc/library/asyncio-stream.rst | 8 +++- Lib/asyncio/streams.py | 4 +- Lib/test/test_asyncio/test_streams.py | 43 +++++++++++++++++++ ...-10-20-02-20-00.gh-issue-155896.v5Rt2H.rst | 2 + 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-10-20-02-20-00.gh-issue-155896.v5Rt2H.rst diff --git a/Doc/library/asyncio-stream.rst b/Doc/library/asyncio-stream.rst index 97741411a9e1c96..9946aeb796fbcfa 100644 --- a/Doc/library/asyncio-stream.rst +++ b/Doc/library/asyncio-stream.rst @@ -399,7 +399,7 @@ StreamWriter to the event loop explicitly with ``await asyncio.sleep(0)`` (see :func:`asyncio.sleep`). - .. method:: start_tls(sslcontext, *, server_hostname=None, \ + .. method:: start_tls(sslcontext, *, server_side=None, server_hostname=None, \ ssl_handshake_timeout=None, ssl_shutdown_timeout=None) :async: @@ -409,6 +409,10 @@ StreamWriter * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`. + * *server_side*: set to ``True`` to perform the server side of the TLS + handshake, or ``False`` to perform the client side. By default + (``None``), the role is inferred from how the stream was created. + * *server_hostname*: sets or overrides the host name that the target server's certificate will be matched against. @@ -426,6 +430,8 @@ StreamWriter Added the *ssl_shutdown_timeout* parameter. .. versionchanged:: 3.16 + Added the *server_side* parameter. + Raises a ``ValueError`` if ``sslcontext.check_hostname`` is ``True`` and ``server_hostname`` is not supplied. diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py index d57460e4ca30f5e..c72db33d3232c60 100644 --- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -395,11 +395,13 @@ async def drain(self): await self._protocol._drain_helper() async def start_tls(self, sslcontext, *, + server_side=None, server_hostname=None, ssl_handshake_timeout=None, ssl_shutdown_timeout=None): """Upgrade an existing stream-based connection to TLS.""" - server_side = self._protocol._client_connected_cb is not None + if server_side is None: + server_side = self._protocol._client_connected_cb is not None protocol = self._protocol await self.drain() new_transport = await self._loop.start_tls( # type: ignore diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 0b5de55fbca80b9..3b03fc2328aa441 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -920,6 +920,49 @@ async def run_test(): self.loop.run_until_complete(run_test()) self.assertEqual(messages, []) + @unittest.skipIf(ssl is None, 'No ssl module') + def test_start_tls_reversed_roles(self): + async def run_test(): + accepted = self.loop.create_future() + async with await asyncio.start_server( + lambda r, w: accepted.set_result((r, w)), + socket_helper.HOSTv4, 0, + ) as server: + address = server.sockets[0].getsockname() + client_reader, client_writer = await asyncio.open_connection( + *address + ) + server_reader, server_writer = await accepted + try: + await asyncio.gather( + client_writer.start_tls( + test_utils.simple_server_sslcontext(), + server_side=True, + ), + server_writer.start_tls( + test_utils.simple_client_sslcontext(), + server_side=False, + ), + ) + client_writer.write(b"hello") + self.assertEqual( + await server_reader.readexactly(5), b"hello" + ) + server_writer.write(b"world") + self.assertEqual( + await client_reader.readexactly(5), b"world" + ) + finally: + client_writer.close() + server_writer.close() + await asyncio.gather( + client_writer.wait_closed(), server_writer.wait_closed() + ) + + self.loop.run_until_complete( + asyncio.wait_for(run_test(), support.SHORT_TIMEOUT) + ) + def test_streamwriter_start_tls_updates_reader_transport(self): reader = asyncio.StreamReader(loop=self.loop) protocol = asyncio.StreamReaderProtocol(reader, loop=self.loop) diff --git a/Misc/NEWS.d/next/Library/2026-10-20-02-20-00.gh-issue-155896.v5Rt2H.rst b/Misc/NEWS.d/next/Library/2026-10-20-02-20-00.gh-issue-155896.v5Rt2H.rst new file mode 100644 index 000000000000000..ace67cbdc5a932d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-20-02-20-00.gh-issue-155896.v5Rt2H.rst @@ -0,0 +1,2 @@ +Allow overriding the TLS client or server role in +:meth:`asyncio.StreamWriter.start_tls` with the *server_side* keyword argument.