Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e86ba4

Browse files
committed
Issue #19422: Explicitly disallow non-SOCK_STREAM sockets in the ssl module, rather than silently let them emit clear text data.
1 parent ecff5e5 commit 3e86ba4

4 files changed

Lines changed: 34 additions & 8 deletions

File tree

Doc/library/ssl.rst

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,16 @@ instead.
141141

142142
Takes an instance ``sock`` of :class:`socket.socket`, and returns an instance
143143
of :class:`ssl.SSLSocket`, a subtype of :class:`socket.socket`, which wraps
144-
the underlying socket in an SSL context. For client-side sockets, the
145-
context construction is lazy; if the underlying socket isn't connected yet,
146-
the context construction will be performed after :meth:`connect` is called on
147-
the socket. For server-side sockets, if the socket has no remote peer, it is
148-
assumed to be a listening socket, and the server-side SSL wrapping is
149-
automatically performed on client connections accepted via the :meth:`accept`
150-
method. :func:`wrap_socket` may raise :exc:`SSLError`.
144+
the underlying socket in an SSL context. ``sock`` must be a
145+
:data:`~socket.SOCK_STREAM` socket; other socket types are unsupported.
146+
147+
For client-side sockets, the context construction is lazy; if the
148+
underlying socket isn't connected yet, the context construction will be
149+
performed after :meth:`connect` is called on the socket. For
150+
server-side sockets, if the socket has no remote peer, it is assumed
151+
to be a listening socket, and the server-side SSL wrapping is
152+
automatically performed on client connections accepted via the
153+
:meth:`accept` method. :func:`wrap_socket` may raise :exc:`SSLError`.
151154

152155
The ``keyfile`` and ``certfile`` parameters specify optional files which
153156
contain a certificate to be used to identify the local side of the
@@ -836,7 +839,10 @@ to speed up repeated connections from the same clients.
836839
server_hostname=None)
837840

838841
Wrap an existing Python socket *sock* and return an :class:`SSLSocket`
839-
object. The SSL socket is tied to the context, its settings and
842+
object. *sock* must be a :data:`~socket.SOCK_STREAM` socket; other socket
843+
types are unsupported.
844+
845+
The returned SSL socket is tied to the context, its settings and
840846
certificates. The parameters *server_side*, *do_handshake_on_connect*
841847
and *suppress_ragged_eofs* have the same meaning as in the top-level
842848
:func:`wrap_socket` function.

Lib/ssl.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
from socket import getnameinfo as _getnameinfo
112112
from socket import error as socket_error
113113
from socket import socket, AF_INET, SOCK_STREAM, create_connection
114+
from socket import SOL_SOCKET, SO_TYPE
114115
import base64 # for DER-to-PEM translation
115116
import traceback
116117
import errno
@@ -296,6 +297,10 @@ def __init__(self, sock=None, keyfile=None, certfile=None,
296297
self.ssl_version = ssl_version
297298
self.ca_certs = ca_certs
298299
self.ciphers = ciphers
300+
# Can't use sock.type as other flags (such as SOCK_NONBLOCK) get
301+
# mixed in.
302+
if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
303+
raise NotImplementedError("only stream sockets are supported")
299304
if server_side and server_hostname:
300305
raise ValueError("server_hostname can only be specified "
301306
"in client mode")

Lib/test/test_ssl.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,18 @@ def test_dealloc_warn(self):
493493
support.gc_collect()
494494
self.assertIn(r, str(cm.warning.args[0]))
495495

496+
def test_unsupported_dtls(self):
497+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
498+
self.addCleanup(s.close)
499+
with self.assertRaises(NotImplementedError) as cx:
500+
ssl.wrap_socket(s, cert_reqs=ssl.CERT_NONE)
501+
self.assertEqual(str(cx.exception), "only stream sockets are supported")
502+
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
503+
with self.assertRaises(NotImplementedError) as cx:
504+
ctx.wrap_socket(s)
505+
self.assertEqual(str(cx.exception), "only stream sockets are supported")
506+
507+
496508
class ContextTests(unittest.TestCase):
497509

498510
@skip_if_broken_ubuntu_ssl

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Core and Builtins
2929
Library
3030
-------
3131

32+
- Issue #19422: Explicitly disallow non-SOCK_STREAM sockets in the ssl
33+
module, rather than silently let them emit clear text data.
34+
3235
- Issue #18116: getpass was always getting an error when testing /dev/tty,
3336
and thus was always falling back to stdin, and would then raise an exception
3437
if stdin could not be used (such as /dev/null). It also leaked an open file.

0 commit comments

Comments
 (0)