-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix SSL verification with ssl_cert_reqs="none" and ssl_check_hostname=True #3637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
omerfeyzioglu
wants to merge
6
commits into
redis:master
Choose a base branch
from
omerfeyzioglu:fix-ssl-verification
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−3
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ec8c093
Fix SSL verification with ssl_cert_reqs=none and ssl_check_hostname=True
omerfeyzioglu ee988fd
issue number fixed
omerfeyzioglu 22c18bd
Merge branch 'master' into fix-ssl-verification
petyaslavova 042599c
Add ssl_check_hostname to REDIS_ALLOWED_KEYS and fix default value in…
omerfeyzioglu ce80c42
Merge remote changes
omerfeyzioglu 63dd464
Merge branch 'master' into fix-ssl-verification
omerfeyzioglu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import asyncio | ||
import ssl | ||
import socket | ||
from urllib.parse import urlparse | ||
import pytest | ||
import pytest_asyncio | ||
import redis.asyncio as redis | ||
from redis.exceptions import RedisError, ConnectionError | ||
import ssl | ||
|
||
# Skip test or not based on cryptography installation | ||
try: | ||
import cryptography # noqa | ||
skip_if_cryptography = pytest.mark.skipif(False, reason="") | ||
skip_if_nocryptography = pytest.mark.skipif(False, reason="") | ||
except ImportError: | ||
skip_if_cryptography = pytest.mark.skipif(True, reason="cryptography not installed") | ||
skip_if_nocryptography = pytest.mark.skipif(True, reason="cryptography not installed") | ||
|
||
@pytest.mark.ssl | ||
class TestSSL: | ||
"""Tests for SSL connections in asyncio.""" | ||
|
||
@pytest_asyncio.fixture() | ||
async def _get_client(self, request): | ||
ssl_url = request.config.option.redis_ssl_url | ||
p = urlparse(ssl_url)[1].split(":") | ||
client = redis.Redis(host=p[0], port=p[1], ssl=True) | ||
yield client | ||
await client.aclose() | ||
|
||
async def test_ssl_with_invalid_cert(self, _get_client): | ||
"""Test SSL connection with invalid certificate.""" | ||
pass | ||
|
||
async def test_cert_reqs_none_with_check_hostname(self, request): | ||
"""Test that when ssl_cert_reqs=none is used with ssl_check_hostname=True, | ||
the connection is created successfully with check_hostname internally set to False""" | ||
ssl_url = request.config.option.redis_ssl_url | ||
p = urlparse(ssl_url)[1].split(":") | ||
r = redis.Redis( | ||
host=p[0], | ||
port=p[1], | ||
ssl=True, | ||
ssl_cert_reqs="none", | ||
ssl_check_hostname=True, # This should work now because we handle the incompatibility | ||
) | ||
try: | ||
# Connection should be successful | ||
assert await r.ping() | ||
# check_hostname should have been automatically set to False | ||
assert r.connection_pool.connection_class == redis.SSLConnection | ||
conn = r.connection_pool.make_connection() | ||
assert conn.check_hostname is False | ||
finally: | ||
await r.aclose() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -309,3 +309,25 @@ def test_mock_ocsp_staple(self, request): | |||||||||||||||||
r.ping() | ||||||||||||||||||
assert "no ocsp response present" in str(e) | ||||||||||||||||||
r.close() | ||||||||||||||||||
|
||||||||||||||||||
def test_cert_reqs_none_with_check_hostname(self, request): | ||||||||||||||||||
"""Test that when ssl_cert_reqs=none is used with ssl_check_hostname=True, | ||||||||||||||||||
the connection is created successfully with check_hostname internally set to False""" | ||||||||||||||||||
ssl_url = request.config.option.redis_ssl_url | ||||||||||||||||||
p = urlparse(ssl_url)[1].split(":") | ||||||||||||||||||
r = redis.Redis( | ||||||||||||||||||
host=p[0], | ||||||||||||||||||
port=p[1], | ||||||||||||||||||
Comment on lines
+317
to
+320
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using urlparse(ssl_url).hostname and urlparse(ssl_url).port for better clarity and robustness instead of indexing the parsed URL result.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||
ssl=True, | ||||||||||||||||||
ssl_cert_reqs="none", | ||||||||||||||||||
ssl_check_hostname=True, # This should work now because we handle the incompatibility | ||||||||||||||||||
) | ||||||||||||||||||
try: | ||||||||||||||||||
# Connection should be successful | ||||||||||||||||||
assert r.ping() | ||||||||||||||||||
# check_hostname should have been automatically set to False | ||||||||||||||||||
assert r.connection_pool.connection_kwargs["connection_class"] == redis.SSLConnection | ||||||||||||||||||
conn = r.connection_pool.make_connection() | ||||||||||||||||||
assert conn.check_hostname is False | ||||||||||||||||||
finally: | ||||||||||||||||||
r.close() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using urlparse(ssl_url).hostname and urlparse(ssl_url).port for clarity and better error-proofing of the URL parsing.
Copilot uses AI. Check for mistakes.