summaryrefslogtreecommitdiffstats
path: root/Lib/ssl.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2016-03-20 12:39:15 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2016-03-20 12:39:15 (GMT)
commitdbcd4576244b9c9acc6201034b1dfcc858c541ed (patch)
treeb7c797fa121078fe2fd39fc04dcde8bb4ec82ab7 /Lib/ssl.py
parent3a4bdb6322fb9cf52baebf5c5ab72c0f8b52a18a (diff)
downloadcpython-dbcd4576244b9c9acc6201034b1dfcc858c541ed.zip
cpython-dbcd4576244b9c9acc6201034b1dfcc858c541ed.tar.gz
cpython-dbcd4576244b9c9acc6201034b1dfcc858c541ed.tar.bz2
Issue #23857: Implement PEP 493
Adds a Python-2-only ssl module API and environment variable to configure the default handling of SSL/TLS certificates for HTTPS connections.
Diffstat (limited to 'Lib/ssl.py')
-rw-r--r--Lib/ssl.py25
1 files changed, 21 insertions, 4 deletions
diff --git a/Lib/ssl.py b/Lib/ssl.py
index 34f7aaa..febc547 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -482,13 +482,30 @@ def _create_unverified_context(protocol=PROTOCOL_SSLv23, cert_reqs=None,
return context
-# Used by http.client if no context is explicitly passed.
-_create_default_https_context = create_default_context
-
-
# Backwards compatibility alias, even though it's not a public name.
_create_stdlib_context = _create_unverified_context
+# PEP 493: Verify HTTPS by default, but allow envvar to override that
+_https_verify_envvar = 'PYTHONHTTPSVERIFY'
+
+def _get_https_context_factory():
+ if not sys.flags.ignore_environment:
+ config_setting = os.environ.get(_https_verify_envvar)
+ if config_setting == '0':
+ return _create_unverified_context
+ return create_default_context
+
+_create_default_https_context = _get_https_context_factory()
+
+# PEP 493: "private" API to configure HTTPS defaults without monkeypatching
+def _https_verify_certificates(enable=True):
+ """Verify server HTTPS certificates by default?"""
+ global _create_default_https_context
+ if enable:
+ _create_default_https_context = create_default_context
+ else:
+ _create_default_https_context = _create_unverified_context
+
class SSLSocket(socket):
"""This class implements a subtype of socket.socket that wraps