Browse code

Prefer the stdlib SSLContext over urllib3 context

We do not go through the effort of finding the right PROTOCOL setting if
we have SSLContext in the stdlib. So we do not want to hit the code
that uses PROTOCOL to set the urllib3-provided ssl context when
SSLContext is available. Also, the urllib3 implementation appears to
have a bug in some recent versions. Preferring the stdlib version will
work around that for those with Python-2.7.9+ as well.

Fixes #26235
Fixes #25402
Fixes #31998

(cherry picked from commit 725ae96e1bb7790cec4a56a9a8a9c5bcb3182951)

Toshio Kuratomi authored on 2017/10/24 05:17:04
Showing 1 changed files
... ...
@@ -698,10 +698,13 @@ class SSLValidationHandler(urllib_request.BaseHandler):
698 698
         return True
699 699
 
700 700
     def _make_context(self, to_add_ca_cert_path):
701
-        if HAS_URLLIB3_PYOPENSSLCONTEXT:
701
+        if HAS_SSLCONTEXT:
702
+            context = create_default_context()
703
+        elif HAS_URLLIB3_PYOPENSSLCONTEXT:
702 704
             context = PyOpenSSLContext(PROTOCOL)
703 705
         else:
704
-            context = create_default_context()
706
+            raise NotImplementedError('Host libraries are too old to support creating an sslcontext')
707
+
705 708
         if to_add_ca_cert_path:
706 709
             context.load_verify_locations(to_add_ca_cert_path)
707 710
         return context
... ...
@@ -710,8 +713,11 @@ class SSLValidationHandler(urllib_request.BaseHandler):
710 710
         tmp_ca_cert_path, to_add_ca_cert_path, paths_checked = self.get_ca_certs()
711 711
         https_proxy = os.environ.get('https_proxy')
712 712
         context = None
713
-        if HAS_SSLCONTEXT or HAS_URLLIB3_PYOPENSSLCONTEXT:
713
+        try:
714 714
             context = self._make_context(to_add_ca_cert_path)
715
+        except Exception:
716
+            # We'll make do with no context below
717
+            pass
715 718
 
716 719
         # Detect if 'no_proxy' environment variable is set and if our URL is included
717 720
         use_proxy = self.detect_no_proxy(req.get_full_url())