Skip to content

Commit

Permalink
Don't use pyOpenSSL unless no SNI is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmlarson authored and nateprewitt committed May 1, 2020
1 parent bfb93d4 commit db47b9b
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,22 @@ def _check_cryptography(cryptography_version):
"version!".format(urllib3.__version__, chardet.__version__),
RequestsDependencyWarning)

# Attempt to enable urllib3's SNI support, if possible
# Attempt to enable urllib3's fallback for SNI support
# if the standard library doesn't support SNI or the
# 'ssl' library isn't available.
try:
from urllib3.contrib import pyopenssl
pyopenssl.inject_into_urllib3()
try:
import ssl
except ImportError:
ssl = None

if not getattr(ssl, "HAS_SNI", False):
from urllib3.contrib import pyopenssl
pyopenssl.inject_into_urllib3()

# Check cryptography version
from cryptography import __version__ as cryptography_version
_check_cryptography(cryptography_version)
# Check cryptography version
from cryptography import __version__ as cryptography_version
_check_cryptography(cryptography_version)
except ImportError:
pass

Expand Down

3 comments on commit db47b9b

@deajan
Copy link

@deajan deajan commented on db47b9b Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular commit creates an issue with modules that use requests namespace, when compiled with Nuitka.
This may also happen when frozen with PyInstaller or else.

Example when using requests-pkcs12

Traceback (most recent call last):
  File "C:\GIT\IMF-IN~1\PYTHON~1\TEST~1.DIS\tesT.py", line 3, in <module>
  File "C:\GIT\IMF-IN~1\PYTHON~1\TEST~1.DIS\requests_pkcs12.py", line 24, in <module requests_pkcs12>
ModuleNotFoundError: No module named 'requests.packages.urllib3.contrib.pyopenssl

When adding from urllib3.contrib import pyopenssl manually to the above file (requests/__init__.py) again, the error gets resolved.
Adding the above import statement to my project instead doesn't resolve the issue, since I cannot modifiy the requests namespace.

Is there any chance this commit may be reverted ?

If not, all packages that depend on requests namespace must be updated.
Example for requests-pkcs12:

from urllib3.contrib.pyopenssl import PyOpenSSLContext
#from requests.packages.urllib3.contrib.pyopenssl import PyOpenSSLContext

@sethmlarson
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please open an issue with further information like Python version, OS, etc and reference this commit instead of a commit comment?

@deajan
Copy link

@deajan deajan commented on db47b9b Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ;) #5561

Please sign in to comment.