Skip to content
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

Fix environment CA Bundle resolution #6074

Merged
merged 1 commit into from Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions requests/sessions.py
Expand Up @@ -702,20 +702,27 @@ def merge_environment_settings(self, url, proxies, stream, verify, cert):
for (k, v) in env_proxies.items():
proxies.setdefault(k, v)

# Look for requests environment configuration and be compatible
# with cURL.
# Look for requests environment configuration
# and be compatible with cURL.
if verify is True or verify is None:
verify = (os.environ.get('REQUESTS_CA_BUNDLE') or
os.environ.get('CURL_CA_BUNDLE'))
verify = (
os.environ.get('REQUESTS_CA_BUNDLE')
or os.environ.get('CURL_CA_BUNDLE')
or verify
)

# Merge all the kwargs.
proxies = merge_setting(proxies, self.proxies)
stream = merge_setting(stream, self.stream)
verify = merge_setting(verify, self.verify)
cert = merge_setting(cert, self.cert)

return {'verify': verify, 'proxies': proxies, 'stream': stream,
'cert': cert}
return {
'proxies': proxies,
'stream': stream,
'verify': verify,
'cert': cert
}

def get_adapter(self, url):
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/test_requests.py
Expand Up @@ -898,6 +898,42 @@ def test_invalid_ssl_certificate_files(self, httpbin_secure):
requests.get(httpbin_secure(), cert=('.', INVALID_PATH))
assert str(e.value) == 'Could not find the TLS key file, invalid path: {}'.format(INVALID_PATH)

@pytest.mark.parametrize(
'env, expected', (
({}, True),
({'REQUESTS_CA_BUNDLE': '/some/path'}, '/some/path'),
({'REQUESTS_CA_BUNDLE': ''}, True),
({'CURL_CA_BUNDLE': '/some/path'}, '/some/path'),
({'CURL_CA_BUNDLE': ''}, True),
({'REQUESTS_CA_BUNDLE': '', 'CURL_CA_BUNDLE': ''}, True),
(
{
'REQUESTS_CA_BUNDLE': '/some/path',
'CURL_CA_BUNDLE': '/curl/path',
},
'/some/path',
),
(
{
'REQUESTS_CA_BUNDLE': '',
'CURL_CA_BUNDLE': '/curl/path',
},
'/curl/path',
),
)
)
def test_env_cert_bundles(self, httpbin, mocker, env, expected):
s = requests.Session()
mocker.patch('os.environ', env)
settings = s.merge_environment_settings(
url=httpbin('get'),
proxies={},
stream=False,
verify=True,
cert=None
)
assert settings['verify'] == expected

def test_http_with_certificate(self, httpbin):
r = requests.get(httpbin(), cert='.')
assert r.status_code == 200
Expand Down