FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-33023: SSL pickle error message · python/cpython@2ddc5ec · GitHub

Repository navigation

Commit 2ddc5ec

Browse files
committed
bpo-33023: SSL pickle error message
Improve error message when trying to serialize SSLContext, SSLObject, and SSLSocket objects. The classes can't be pickled or copied by the copy module. Signed-off-by: Christian Heimes <christian@python.org>
1 parent 74f5687 commit 2ddc5ec

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎Lib/ssl.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ def __new__(cls, protocol=PROTOCOL_TLS, *args, **kwargs):
388388
self = _SSLContext.__new__(cls, protocol)
389389
return self
390390

391+
def __getstate__(self):
392+
raise TypeError("cannot serialize {} object".format(
393+
self.__class__.__name__))
394+
391395
def _encode_hostname(self, hostname):
392396
if hostname is None:
393397
return None
@@ -695,6 +699,10 @@ def server_hostname(self):
695699
server hostame is set."""
696700
return self._sslobj.server_hostname
697701

702+
def __getstate__(self):
703+
raise TypeError("cannot serialize {} object".format(
704+
self.__class__.__name__))
705+
698706
def read(self, len=1024, buffer=None):
699707
"""Read up to 'len' bytes from the SSL object and return them.
700708
@@ -880,9 +888,13 @@ def session_reused(self):
880888
if self._sslobj is not None:
881889
return self._sslobj.session_reused
882890

891+
def __getstate__(self):
892+
raise TypeError("cannot serialize {} object".format(
893+
self.__class__.__name__))
894+
883895
def dup(self):
884-
raise NotImplemented("Can't dup() %s instances" %
885-
self.__class__.__name__)
896+
raise NotImplementedError("Can't dup() {} instances".format(
897+
self.__class__.__name__))
886898

887899
def _checkClosed(self, msg=None):
888900
# raise an exception here if you wish to check for spurious closes

‎Lib/test/test_ssl.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import platform
2020
import functools
2121
import sysconfig
22+
import copy
23+
import pickle
2224
try:
2325
import ctypes
2426
except ImportError:
@@ -972,6 +974,17 @@ def test_connect_ex_error(self):
972974
)
973975
self.assertIn(rc, errors)
974976

977+
def test_copy_pickle(self):
978+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
979+
with ctx.wrap_socket(socket.socket(),
980+
server_hostname='localhost') as s:
981+
with self.assertRaises(TypeError):
982+
copy.copy(s)
983+
with self.assertRaises(TypeError):
984+
copy.deepcopy(s)
985+
with self.assertRaises(TypeError):
986+
pickle.dumps(s)
987+
975988

976989
class ContextTests(unittest.TestCase):
977990

@@ -1608,6 +1621,15 @@ class MySSLObject(ssl.SSLObject):
16081621
obj = ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())
16091622
self.assertIsInstance(obj, MySSLObject)
16101623

1624+
def test_copy_pickle(self):
1625+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
1626+
with self.assertRaises(TypeError):
1627+
copy.copy(ctx)
1628+
with self.assertRaises(TypeError):
1629+
copy.deepcopy(ctx)
1630+
with self.assertRaises(TypeError):
1631+
pickle.dumps(ctx)
1632+
16111633

16121634
class SSLErrorTests(unittest.TestCase):
16131635

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve error message when trying to serialize SSLContext, SSLObject, and
2+
SSLSocket objects. The classes can't be pickled or copied by the copy
3+
module.

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL