mirror of
https://github.com/python/cpython.git
synced 2025-10-08 16:11:51 +00:00
[3.7] bpo-32951: Disable SSLSocket/SSLObject constructor (GH-5864) (#5925)
Direct instantiation of SSLSocket and SSLObject objects is now prohibited.
The constructors were never documented, tested, or designed as public
constructors. The SSLSocket constructor had limitations. For example it was
not possible to enabled hostname verification except was
ssl_version=PROTOCOL_TLS_CLIENT with cert_reqs=CERT_REQUIRED.
SSLContext.wrap_socket() and SSLContext.wrap_bio are the recommended API
to construct SSLSocket and SSLObject instances. ssl.wrap_socket() is
also deprecated.
The only test case for direct instantiation was added a couple of days
ago for IDNA testing.
Signed-off-by: Christian Heimes <christian@python.org>
(cherry picked from commit 9d50ab563d
)
Co-authored-by: Christian Heimes <christian@python.org>
This commit is contained in:
parent
102d5204ad
commit
89c2051a55
5 changed files with 105 additions and 82 deletions
|
@ -263,6 +263,11 @@ class BasicSocketTests(unittest.TestCase):
|
|||
ssl.OP_NO_TLSv1_2
|
||||
self.assertEqual(ssl.PROTOCOL_TLS, ssl.PROTOCOL_SSLv23)
|
||||
|
||||
def test_private_init(self):
|
||||
with self.assertRaisesRegex(TypeError, "public constructor"):
|
||||
with socket.socket() as s:
|
||||
ssl.SSLSocket(s)
|
||||
|
||||
def test_str_for_enums(self):
|
||||
# Make sure that the PROTOCOL_* constants have enum-like string
|
||||
# reprs.
|
||||
|
@ -1657,6 +1662,13 @@ class MemoryBIOTests(unittest.TestCase):
|
|||
self.assertRaises(TypeError, bio.write, 1)
|
||||
|
||||
|
||||
class SSLObjectTests(unittest.TestCase):
|
||||
def test_private_init(self):
|
||||
bio = ssl.MemoryBIO()
|
||||
with self.assertRaisesRegex(TypeError, "public constructor"):
|
||||
ssl.SSLObject(bio, bio)
|
||||
|
||||
|
||||
class SimpleBackgroundTests(unittest.TestCase):
|
||||
"""Tests that connect to a simple server running in the background"""
|
||||
|
||||
|
@ -2735,12 +2747,6 @@ class ThreadedTests(unittest.TestCase):
|
|||
self.assertEqual(s.server_hostname, expected_hostname)
|
||||
self.assertTrue(cert, "Can't get peer certificate.")
|
||||
|
||||
with ssl.SSLSocket(socket.socket(),
|
||||
server_hostname=server_hostname) as s:
|
||||
s.connect((HOST, server.port))
|
||||
s.getpeercert()
|
||||
self.assertEqual(s.server_hostname, expected_hostname)
|
||||
|
||||
# incorrect hostname should raise an exception
|
||||
server = ThreadedEchoServer(context=server_context, chatty=True)
|
||||
with server:
|
||||
|
@ -3999,7 +4005,7 @@ def test_main(verbose=False):
|
|||
|
||||
tests = [
|
||||
ContextTests, BasicSocketTests, SSLErrorTests, MemoryBIOTests,
|
||||
SimpleBackgroundTests, ThreadedTests,
|
||||
SSLObjectTests, SimpleBackgroundTests, ThreadedTests,
|
||||
]
|
||||
|
||||
if support.is_resource_enabled('network'):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue