Merge pull request #3927 from bagerard/raise_IOError_invalid_certificate_files
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Raise IOError when providing an invalid path to a CA bundle or …
authored by Cory Benfield · committed by GitHub · parents b60c5a5, 4207867 · Browse files · 4 files·+40 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
4 files+40 added−8 removed
- tests:+16 added−0 removed
- other:+24 added−8 removed
Provenance
- Commit
993cbf5- Tree
bd4b09f- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
b60c5a5- Parent 2
4207867
4 files changed+40 added−8 removed
modified:requests/adapters.py+18 added-5 removed
62 unmodified lines63 :param timeout: (optional) How long to wait for the server to send64 data before giving up, as a float, or a :ref:`(connect timeout,65 read timeout) <timeouts>` tuple.66 :type timeout: float or tuple67- :param verify: (optional) Whether to verify SSL certificates.67+ :param verify: (optional) Either a boolean, in which case it controls whether we verify68+ the server's TLS certificate, or a string, in which case it must be a path69+ to a CA bundle to use70 :param cert: (optional) Any user-provided SSL certificate to be trusted.71 :param proxies: (optional) The proxies dictionary to apply to the request.72 """73 raise NotImplementedError129 unmodified lines203 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.204 205 :param conn: The urllib3 connection object associated with the cert.206 :param url: The requested URL.205- :param verify: Whether we should actually verify the certificate.207+ :param verify: Either a boolean, in which case it controls whether we verify208+ the server's TLS certificate, or a string, in which case it must be a path209+ to a CA bundle to use210 :param cert: The SSL certificate to verify.211 """212 if url.lower().startswith('https') and verify:213 5 unmodified lines219 220 if not cert_loc:221 cert_loc = DEFAULT_CA_BUNDLE_PATH222 219- if not cert_loc:220- raise Exception("Could not find a suitable SSL CA certificate bundle.")223+ if not cert_loc or not os.path.exists(cert_loc):224+ raise IOError("Could not find a suitable TLS CA certificate bundle, "225+ "invalid path: {0}".format(cert_loc))226 227 conn.cert_reqs = 'CERT_REQUIRED'228 229 if not os.path.isdir(cert_loc):10 unmodified lines240 conn.cert_file = cert[0]241 conn.key_file = cert[1]242 else:243 conn.cert_file = cert244+ if conn.cert_file and not os.path.exists(conn.cert_file):245+ raise IOError("Could not find the TLS certificate file, "246+ "invalid path: {0}".format(conn.cert_file))247+ if conn.key_file and not os.path.exists(conn.key_file):248+ raise IOError("Could not find the TLS key file, "249+ "invalid path: {0}".format(conn.key_file))250 251 def build_response(self, req, resp):252 """Builds a :class:`Response <requests.Response>` object from a urllib3253 response. This should not be called from user code, and is only exposed137 unmodified lines391 :param timeout: (optional) How long to wait for the server to send392 data before giving up, as a float, or a :ref:`(connect timeout,393 read timeout) <timeouts>` tuple.394 :type timeout: float or tuple384- :param verify: (optional) Whether to verify SSL certificates.395+ :param verify: (optional) Either a boolean, in which case it controls whether396+ we verify the server's TLS certificate, or a string, in which case it397+ must be a path to a CA bundle to use398 :param cert: (optional) Any user-provided SSL certificate to be trusted.399 :param proxies: (optional) The proxies dictionary to apply to the request.400 :rtype: requests.Response401 """115 unmodified linesmodified:requests/api.py+3 added-1 removed
34 unmodified lines35 :type timeout: float or tuple36 :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.37 :type allow_redirects: bool38 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.39- :param verify: (optional) whether the SSL cert will be verified. A CA_BUNDLE path can also be provided. Defaults to ``True``.39+ :param verify: (optional) Either a boolean, in which case it controls whether we verify40+ the server's TLS certificate, or a string, in which case it must be a path41+ to a CA bundle to use. Defaults to ``True``.42 :param stream: (optional) if ``False``, the response content will be immediately downloaded.43 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.44 :return: :class:`Response <Response>` object45 :rtype: requests.Response107 unmodified linesmodified:requests/sessions.py+3 added-2 removed
458 unmodified lines459 :param proxies: (optional) Dictionary mapping protocol or protocol and460 hostname to the URL of the proxy.461 :param stream: (optional) whether to immediately download the response462 content. Defaults to ``False``.463- :param verify: (optional) whether the SSL cert will be verified.464- A CA_BUNDLE path can also be provided. Defaults to ``True``.463+ :param verify: (optional) Either a boolean, in which case it controls whether we verify464+ the server's TLS certificate, or a string, in which case it must be a path465+ to a CA bundle to use. Defaults to ``True``.466 :param cert: (optional) if String, path to ssl client cert file (.pem).467 If Tuple, ('cert', 'key') pair.468 :rtype: requests.Response469 """266 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, httpbin_secure):773+ INVALID_PATH = '/garbage'774+ with pytest.raises(IOError) as e:775+ requests.get(httpbin_secure(), verify=INVALID_PATH)776+ assert str(e.value) == 'Could not find a suitable TLS CA certificate bundle, invalid path: {0}'.format(INVALID_PATH)777+778+ def test_invalid_ssl_certificate_files(self, httpbin_secure):779+ INVALID_PATH = '/garbage'780+ with pytest.raises(IOError) as e:781+ requests.get(httpbin_secure(), cert=INVALID_PATH)782+ assert str(e.value) == 'Could not find the TLS certificate file, invalid path: {0}'.format(INVALID_PATH)783+784+ with pytest.raises(IOError) as e:785+ requests.get(httpbin_secure(), cert=('.', INVALID_PATH))786+ assert str(e.value) == 'Could not find the TLS 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