#3926 raise IOError when providing an invalid path to a CA bundle or certificate files
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Bastien Gérard · committed by Bastien Gérard · parent b60c5a5 · Browse files · 2 files·+31 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+31 added−8 removed
- tests:+16 added−0 removed
- other:+15 added−8 removed
Provenance
- Commit
7d8b87c- Tree
2e15513- Author
Bastien Gérard <bastien.gerard@cluepoints.com>- Committer
Bastien Gérard <bastien.gerard@cluepoints.com>- Parent
b60c5a5
2 files changed+31 added−8 removed
modified:requests/adapters.py+15 added-8 removed
200 unmodified lines201 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.202 203 :param conn: The urllib3 connection object associated with the cert.204 :param url: The requested URL.205- :param verify: Whether we should actually verify the certificate.205+ :param verify: Whether we should actually verify the certificate206+ or it could be a path to a ca bundle207 :param cert: The SSL certificate to verify.208 """209 if url.lower().startswith('https') and verify:210 5 unmodified lines216 217 if not cert_loc:218 cert_loc = DEFAULT_CA_BUNDLE_PATH219 219- if not cert_loc:220- raise Exception("Could not find a suitable SSL CA certificate bundle.")220+ if not cert_loc or not os.path.exists(cert_loc):221+ raise IOError("Could not find a suitable SSL CA certificate bundle, "222+ "invalid path: {0}".format(cert_loc))223 224 conn.cert_reqs = 'CERT_REQUIRED'225 224- if not os.path.isdir(cert_loc):225- conn.ca_certs = cert_loc226- else:226+ if os.path.isdir(cert_loc):227 conn.ca_cert_dir = cert_loc228+ else:229+ conn.ca_certs = cert_loc230 else:231 conn.cert_reqs = 'CERT_NONE'232 conn.ca_certs = None233 conn.ca_cert_dir = None234 235 if cert:236 if not isinstance(cert, basestring):235- conn.cert_file = cert[0]236- conn.key_file = cert[1]237+ conn.cert_file, conn.key_file = cert238 else:239 conn.cert_file = cert240+ if conn.cert_file and not os.path.exists(conn.cert_file):241+ raise IOError("Could not find the SSL certificate file, "242+ "invalid path: {0}".format(conn.cert_file))243+ if conn.key_file and not os.path.exists(conn.key_file):244+ raise IOError("Could not find the SSL key file, "245+ "invalid path: {0}".format(conn.key_file))246 247 def build_response(self, req, resp):248 """Builds a :class:`Response <requests.Response>` object from a urllib3249 response. This should not be called from user code, and is only exposed261 unmodified linesmodified:tests/test_requests.py+16 added
767 unmodified lines768 769 def test_pyopenssl_redirect(self, httpbin_secure, httpbin_ca_bundle):770 requests.get(httpbin_secure('status', '301'), verify=httpbin_ca_bundle)771 772+ def test_invalid_ca_certificate_path(self):773+ INVALID_PATH = '/garbage'774+ with pytest.raises(IOError) as e:775+ requests.get('http://github.com', verify=INVALID_PATH)776+ assert str(e.value) == 'Could not find a suitable SSL CA certificate bundle, invalid path: {0}'.format(INVALID_PATH)777+778+ def test_invalid_ssl_certificate_files(self):779+ INVALID_PATH = '/garbage'780+ with pytest.raises(IOError) as e:781+ requests.get('http://github.com', cert=INVALID_PATH)782+ assert str(e.value) == 'Could not find the SSL certificate file, invalid path: {0}'.format(INVALID_PATH)783+784+ with pytest.raises(IOError) as e:785+ requests.get('http://github.com', cert=('.', INVALID_PATH))786+ assert str(e.value) == 'Could not find the SSL key file, invalid path: {0}'.format(INVALID_PATH)787+788 def test_https_warnings(self, httpbin_secure, httpbin_ca_bundle):789 """warnings are emitted with requests.get"""790 if HAS_MODERN_SSL or HAS_PYOPENSSL:791 warnings_expected = ('SubjectAltNameWarning', )1563 unmodified lines