|
1 | 1 | When making a request over HTTPS, HTTPX needs to verify the identity of the requested host. To do this, it uses a bundle of SSL certificates (a.k.a. CA bundle) delivered by a trusted certificate authority (CA). |
2 | 2 |
|
3 | | -## Changing the verification defaults |
| 3 | +### Enabling and disabling verification |
4 | 4 |
|
5 | | -By default, HTTPX uses the CA bundle provided by [Certifi](https://pypi.org/project/certifi/). This is what you want in most cases, even though some advanced situations may require you to use a different set of certificates. |
| 5 | +By default httpx will verify HTTPS connections, and raise an error for invalid SSL cases... |
6 | 6 |
|
7 | | -If you'd like to use a custom CA bundle, you can use the `verify` parameter. |
| 7 | +```pycon |
| 8 | +>>> httpx.get("https://expired.badssl.com/") |
| 9 | +httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997) |
| 10 | +``` |
8 | 11 |
|
9 | | -```python |
10 | | -import httpx |
| 12 | +You can configure the verification using `httpx.SSLContext()`. |
11 | 13 |
|
12 | | -r = httpx.get("https://example.org", verify="path/to/client.pem") |
| 14 | +```pycon |
| 15 | +>>> ssl_context = httpx.SSLContext() |
| 16 | +>>> ssl_context |
| 17 | +SSLContext(verify=True) |
| 18 | +>>> httpx.get("https://www.example.com", ssl_context=ssl_context) |
| 19 | +httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997) |
13 | 20 | ``` |
14 | 21 |
|
15 | | -Alternatively, you can pass a standard library `ssl.SSLContext`. |
| 22 | +For example, you can use this to disable verification completely and allow insecure requests... |
16 | 23 |
|
17 | 24 | ```pycon |
18 | | ->>> import ssl |
19 | | ->>> import httpx |
20 | | ->>> context = ssl.create_default_context() |
21 | | ->>> context.load_verify_locations(cafile="/tmp/client.pem") |
22 | | ->>> httpx.get('https://example.org', verify=context) |
| 25 | +>>> no_verify = httpx.SSLContext(verify=False) |
| 26 | +>>> no_verify |
| 27 | +SSLContext(verify=False) |
| 28 | +>>> httpx.get("https://expired.badssl.com/", ssl_context=no_verify) |
23 | 29 | <Response [200 OK]> |
24 | 30 | ``` |
25 | 31 |
|
26 | | -We also include a helper function for creating properly configured `SSLContext` instances. |
| 32 | +### Configuring client instances |
| 33 | + |
| 34 | +If you're using a `Client()` instance, then you should pass any SSL settings when instantiating the client. |
27 | 35 |
|
28 | 36 | ```pycon |
29 | | ->>> context = httpx.create_ssl_context() |
| 37 | +>>> ssl_context = httpx.SSLContext() |
| 38 | +>>> client = httpx.Client(ssl_context=ssl_context) |
30 | 39 | ``` |
31 | 40 |
|
32 | | -The `create_ssl_context` function accepts the same set of SSL configuration arguments |
33 | | -(`trust_env`, `verify`, `cert` and `http2` arguments) |
34 | | -as `httpx.Client` or `httpx.AsyncClient` |
| 41 | +The `client.get(...)` method and other request methods on a `Client` instance *do not* support changing the SSL settings on a per-request basis. |
| 42 | + |
| 43 | +If you need different SSL settings in different cases you should use more that one client instance, with different settings on each. Each client will then be using an isolated connection pool with a specific fixed SSL configuration on all connections within that pool. |
| 44 | + |
| 45 | +### Changing the verification defaults |
| 46 | + |
| 47 | +By default, HTTPX uses the CA bundle provided by [Certifi](https://pypi.org/project/certifi/). |
| 48 | + |
| 49 | +The following all have the same behaviour... |
| 50 | + |
| 51 | +Using the default SSL context. |
35 | 52 |
|
36 | 53 | ```pycon |
37 | | ->>> import httpx |
38 | | ->>> context = httpx.create_ssl_context(verify="/tmp/client.pem") |
39 | | ->>> httpx.get('https://example.org', verify=context) |
| 54 | +>>> client = httpx.Client() |
| 55 | +>>> client.get("https://www.example.com") |
40 | 56 | <Response [200 OK]> |
41 | 57 | ``` |
42 | 58 |
|
43 | | -Or you can also disable the SSL verification entirely, which is _not_ recommended. |
| 59 | +Using the default SSL context, but specified explicitly. |
44 | 60 |
|
45 | | -```python |
46 | | -import httpx |
| 61 | +```pycon |
| 62 | +>>> default = httpx.SSLContext() |
| 63 | +>>> client = httpx.Client(ssl_context=default) |
| 64 | +>>> client.get("https://www.example.com") |
| 65 | +<Response [200 OK]> |
| 66 | +``` |
47 | 67 |
|
48 | | -r = httpx.get("https://example.org", verify=False) |
| 68 | +Using the default SSL context, with `verify=True` specified explicitly. |
| 69 | + |
| 70 | +```pycon |
| 71 | +>>> default = httpx.SSLContext(verify=True) |
| 72 | +>>> client = httpx.Client(ssl_context=default) |
| 73 | +>>> client.get("https://www.example.com") |
| 74 | +<Response [200 OK]> |
49 | 75 | ``` |
50 | 76 |
|
51 | | -## SSL configuration on client instances |
| 77 | +Using an SSL context, with `certifi.where()` explicitly specified. |
52 | 78 |
|
53 | | -If you're using a `Client()` instance, then you should pass any SSL settings when instantiating the client. |
| 79 | +```pycon |
| 80 | +>>> default = httpx.SSLContext(verify=certifi.where()) |
| 81 | +>>> client = httpx.Client(ssl_context=default) |
| 82 | +>>> client.get("https://www.example.com") |
| 83 | +<Response [200 OK]> |
| 84 | +``` |
54 | 85 |
|
55 | | -```python |
56 | | -client = httpx.Client(verify=False) |
| 86 | +For some advanced situations may require you to use a different set of certificates, either by specifying a PEM file: |
| 87 | + |
| 88 | +```pycon |
| 89 | +>>> custom_cafile = httpx.SSLContext(verify="path/to/certs.pem") |
| 90 | +>>> client = httpx.Client(ssl_context=custom_cafile) |
| 91 | +>>> client.get("https://www.example.com") |
| 92 | +<Response [200 OK]> |
| 93 | +``` |
| 94 | + |
| 95 | +Or by providing an certificate directory: |
| 96 | + |
| 97 | +```pycon |
| 98 | +>>> custom_capath = httpx.SSLContext(verify="path/to/certs") |
| 99 | +>>> client = httpx.Client(ssl_context=custom_capath) |
| 100 | +>>> client.get("https://www.example.com") |
| 101 | +<Response [200 OK]> |
| 102 | +``` |
| 103 | + |
| 104 | +These usages are equivelent to using [`.load_verify_locations()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_verify_locations) with either `cafile=...` or `capath=...`. |
| 105 | + |
| 106 | +### Client side certificates |
| 107 | + |
| 108 | +You can also specify a local cert to use as a client-side certificate, either a path to an SSL certificate file... |
| 109 | + |
| 110 | +```pycon |
| 111 | +>>> cert = "path/to/client.pem" |
| 112 | +>>> ssl_context = httpx.SSLContext(cert=cert) |
| 113 | +>>> httpx.get("https://example.org", ssl_context=ssl_context) |
| 114 | +<Response [200 OK]> |
57 | 115 | ``` |
58 | 116 |
|
59 | | -The `client.get(...)` method and other request methods *do not* support changing the SSL settings on a per-request basis. If you need different SSL settings in different cases you should use more that one client instance, with different settings on each. Each client will then be using an isolated connection pool with a specific fixed SSL configuration on all connections within that pool. |
| 117 | +Or two-tuple of (certificate file, key file)... |
60 | 118 |
|
61 | | -## Client Side Certificates |
| 119 | +```pycon |
| 120 | +>>> cert = ("path/to/client.pem", "path/to/client.key") |
| 121 | +>>> ssl_context = httpx.SSLContext(cert=cert) |
| 122 | +>>> httpx.get("https://example.org", ssl_context=ssl_context) |
| 123 | +<Response [200 OK]> |
| 124 | +``` |
62 | 125 |
|
63 | | -You can also specify a local cert to use as a client-side certificate, either a path to an SSL certificate file, or two-tuple of (certificate file, key file), or a three-tuple of (certificate file, key file, password) |
| 126 | +Or a three-tuple of (certificate file, key file, password)... |
| 127 | + |
| 128 | +```pycon |
| 129 | +>>> cert = ("path/to/client.pem", "path/to/client.key", "password") |
| 130 | +>>> ssl_context = httpx.SSLContext(cert=cert) |
| 131 | +>>> httpx.get("https://example.org", ssl_context=ssl_context) |
| 132 | +<Response [200 OK]> |
| 133 | +``` |
| 134 | + |
| 135 | +These configurations are equivalent to using [`.load_cert_chain()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_cert_chain). |
| 136 | + |
| 137 | +### Using alternate SSL contexts |
| 138 | + |
| 139 | +You can also use an alternate `ssl.SSLContext` instances. |
| 140 | + |
| 141 | +For example, [using the `truststore` package](https://truststore.readthedocs.io/)... |
64 | 142 |
|
65 | 143 | ```python |
66 | | -cert = "path/to/client.pem" |
67 | | -client = httpx.Client(cert=cert) |
68 | | -response = client.get("https://example.org") |
| 144 | +import ssl |
| 145 | +import truststore |
| 146 | +import httpx |
| 147 | + |
| 148 | +ssl_context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 149 | +client = httpx.Client(ssl_context=ssl_context) |
69 | 150 | ``` |
70 | 151 |
|
71 | | -Alternatively... |
| 152 | +Or working [directly with Python's standard library](https://docs.python.org/3/library/ssl.html)... |
72 | 153 |
|
73 | 154 | ```python |
74 | | -cert = ("path/to/client.pem", "path/to/client.key") |
75 | | -client = httpx.Client(cert=cert) |
76 | | -response = client.get("https://example.org") |
| 155 | +import ssl |
| 156 | +import httpx |
| 157 | + |
| 158 | +ssl_context = ssl.create_default_context() |
| 159 | +client = httpx.Client(ssl_context=ssl_context) |
77 | 160 | ``` |
78 | 161 |
|
79 | | -Or... |
| 162 | +### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR` |
| 163 | + |
| 164 | +Unlike `requests`, the `httpx` package does not automatically pull in [the environment variables `SSL_CERT_FILE` or `SSL_CERT_DIR`](https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_default_verify_paths.html). If you want to use these they need to be enabled explicitly. |
| 165 | + |
| 166 | +For example... |
| 167 | + |
| 168 | +```python |
| 169 | +# Use `SSL_CERT_FILE` or `SSL_CERT_DIR` if configured, otherwise use certifi. |
| 170 | +verify = os.environ.get("SSL_CERT_FILE", os.environ.get("SSL_CERT_DIR", True)) |
| 171 | +ssl_context = httpx.SSLContext(verify=verify) |
| 172 | +``` |
| 173 | + |
| 174 | +## `SSLKEYLOGFILE` |
| 175 | + |
| 176 | +Valid values: a filename |
| 177 | + |
| 178 | +If this environment variable is set, TLS keys will be appended to the specified file, creating it if it doesn't exist, whenever key material is generated or received. The keylog file is designed for debugging purposes only. |
| 179 | + |
| 180 | +Support for `SSLKEYLOGFILE` requires Python 3.8 and OpenSSL 1.1.1 or newer. |
| 181 | + |
| 182 | +Example: |
80 | 183 |
|
81 | 184 | ```python |
82 | | -cert = ("path/to/client.pem", "path/to/client.key", "password") |
83 | | -client = httpx.Client(cert=cert) |
84 | | -response = client.get("https://example.org") |
| 185 | +# test_script.py |
| 186 | +import httpx |
| 187 | + |
| 188 | +with httpx.Client() as client: |
| 189 | + r = client.get("https://google.com") |
85 | 190 | ``` |
86 | 191 |
|
87 | | -## Making HTTPS requests to a local server |
| 192 | +```console |
| 193 | +SSLKEYLOGFILE=test.log python test_script.py |
| 194 | +cat test.log |
| 195 | +# TLS secrets log file, generated by OpenSSL / Python |
| 196 | +SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX |
| 197 | +EXPORTER_SECRET XXXX |
| 198 | +SERVER_TRAFFIC_SECRET_0 XXXX |
| 199 | +CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX |
| 200 | +CLIENT_TRAFFIC_SECRET_0 XXXX |
| 201 | +SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX |
| 202 | +EXPORTER_SECRET XXXX |
| 203 | +SERVER_TRAFFIC_SECRET_0 XXXX |
| 204 | +CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX |
| 205 | +CLIENT_TRAFFIC_SECRET_0 XXXX |
| 206 | +``` |
| 207 | + |
| 208 | +### Making HTTPS requests to a local server |
88 | 209 |
|
89 | 210 | When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections. |
90 | 211 |
|
91 | 212 | If you do need to make HTTPS connections to a local server, for example to test an HTTPS-only service, you will need to create and use your own certificates. Here's one way to do it: |
92 | 213 |
|
93 | 214 | 1. Use [trustme](https://github.com/python-trio/trustme) to generate a pair of server key/cert files, and a client cert file. |
94 | | -1. Pass the server key/cert files when starting your local server. (This depends on the particular web server you're using. For example, [Uvicorn](https://www.uvicorn.org) provides the `--ssl-keyfile` and `--ssl-certfile` options.) |
95 | | -1. Tell HTTPX to use the certificates stored in `client.pem`: |
| 215 | +2. Pass the server key/cert files when starting your local server. (This depends on the particular web server you're using. For example, [Uvicorn](https://www.uvicorn.org) provides the `--ssl-keyfile` and `--ssl-certfile` options.) |
| 216 | +3. Tell HTTPX to use the certificates stored in `client.pem`: |
96 | 217 |
|
97 | | -```python |
98 | | -client = httpx.Client(verify="/tmp/client.pem") |
99 | | -response = client.get("https://localhost:8000") |
| 218 | +```pycon |
| 219 | +>>> import httpx |
| 220 | +>>> ssl_context = httpx.SSLContext(verify="/tmp/client.pem") |
| 221 | +>>> r = httpx.get("https://localhost:8000", ssl_context=ssl_context) |
| 222 | +>>> r |
| 223 | +Response <200 OK> |
100 | 224 | ``` |
0 commit comments