Skip to content
Myr

ssl.md

This file

Path

docs/advanced/ssl.md

199 lines · 6.6 KiB · Markdown

Last changed by

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

History of this file

Take it

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 1–2why, one step back
1When 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

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 3why, one step back
3### Enabling and disabling verification

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 4why, one step back
4

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 5why, one step back
5By default httpx will verify HTTPS connections, and raise an error for invalid SSL cases...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 6why, one step back
6

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 7–10why, one step back
7```pycon
8>>> httpx.get("https://expired.badssl.com/")9httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)10```

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 11why, one step back
11

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 12why, one step back
12Verification is configured through [the SSL Context API](https://docs.python.org/3/library/ssl.html#ssl-contexts).

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 13why, one step back
13

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 14–19why, one step back
14```pycon
15>>> context = httpx.SSLContext()16>>> context17<SSLContext(verify=True)>18>>> httpx.get("https://www.example.com", ssl_context=context)19httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 20–21why, one step back
20```
21

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 22why, one step back
22You can use this to disable verification completely and allow insecure requests...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 23–24why, one step back
23
24```pycon

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 25–28why, one step back
25>>> context = httpx.SSLContext(verify=False)
26>>> context27<SSLContext(verify=False)>28>>> httpx.get("https://expired.badssl.com/", ssl_context=context)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 29–31why, one step back
29<Response [200 OK]>
30```31

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 32why, one step back
32### Configuring client instances

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 33why, one step back
33

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 34–35why, one step back
34If you're using a `Client()` instance you should pass any SSL context when instantiating the client.
35

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 36why, one step back
36```pycon

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 37–38why, one step back
37>>> context = httpx.SSLContext()
38>>> client = httpx.Client(ssl_context=context)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 39–40why, one step back
39```
40

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 41why, one step back
41The `client.get(...)` method and other request methods on a `Client` instance *do not* support changing the SSL settings on a per-request basis.

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 42why, one step back
42

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 43–50why, one step back
43If you need different SSL settings in different cases you should use more than 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.
4445### Configuring certificate stores4647By default, HTTPX uses the CA bundle provided by [Certifi](https://pypi.org/project/certifi/).4849You can load additional certificate verification using the [`.load_verify_locations()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_verify_locations) API:50

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 51why, one step back
51```pycon

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 52–55why, one step back
52>>> context = httpx.SSLContext()
53>>> context.load_verify_locations(cafile="path/to/certs.pem")54>>> client = httpx.Client(ssl_context=context)55>>> client.get("https://www.example.com")

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 56–58why, one step back
56<Response [200 OK]>
57```58

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 59why, one step back
59Or by providing an certificate directory:

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 60why, one step back
60

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 61–67why, one step back
61```pycon
62>>> context = httpx.SSLContext()63>>> context.load_verify_locations(capath="path/to/certs")64>>> client = httpx.Client(ssl_context=context)65>>> client.get("https://www.example.com")66<Response [200 OK]>67```

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 68why, one step back
68

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 69–77why, one step back
69### Client side certificates
7071You can also specify a local cert to use as a client-side certificate, using the [`.load_cert_chain()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_cert_chain) API:7273```pycon74>>> context = httpx.SSLContext()75>>> context.load_cert_chain(certfile="path/to/client.pem")76>>> httpx.get("https://example.org", ssl_context=ssl_context)77<Response [200 OK]>

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 78–79why, one step back
78```
79

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 80why, one step back
80Or including a keyfile...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 81why, one step back
81

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 82–90why, one step back
82```pycon
83>>> context = httpx.SSLContext()84>>> context.load_cert_chain(85 certfile="path/to/client.pem",86 keyfile="path/to/client.key"87 )88>>> httpx.get("https://example.org", ssl_context=context)89<Response [200 OK]>90```

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 91why, one step back
91

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 92–103why, one step back
92Or including a keyfile and password...
9394```pycon95>>> context = httpx.SSLContext(cert=cert)96>>> context = httpx.SSLContext()97>>> context.load_cert_chain(98 certfile="path/to/client.pem",99 keyfile="path/to/client.key"100 password="password"101 )102>>> httpx.get("https://example.org", ssl_context=context)103<Response [200 OK]>

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 104–105why, one step back
104```
105

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 106why, one step back
106### Using alternate SSL contexts

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 107why, one step back
107

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 108why, one step back
108You can also use an alternate `ssl.SSLContext` instances.

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 109why, one step back
109

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 110why, one step back
110For example, [using the `truststore` package](https://truststore.readthedocs.io/)...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 111–112why, one step back
111
112```python

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 113–118why, one step back
113import ssl
114import truststore115import httpx116117ssl_context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)118client = httpx.Client(ssl_context=ssl_context)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 119–120why, one step back
119```
120

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 121why, one step back
121Or working [directly with Python's standard library](https://docs.python.org/3/library/ssl.html)...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 122–123why, one step back
122
123```python

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 124–128why, one step back
124import ssl
125import httpx126127ssl_context = ssl.create_default_context()128client = httpx.Client(ssl_context=ssl_context)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 129–130why, one step back
129```
130

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 131why, one step back
131### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR`

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 132why, one step back
132

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 133–136why, one step back
133Unlike `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.
134135For example...136

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 137why, one step back
137```python

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 138–145why, one step back
138context = httpx.SSLContext()
139140# Use `SSL_CERT_FILE` or `SSL_CERT_DIR` if configured.141if os.environ.get("SSL_CERT_FILE") or os.environ.get("SSL_CERT_DIR"):142 context.load_verify_locations(143 cafile=os.environ.get("SSL_CERT_FILE"),144 capath=os.environ.get("SSL_CERT_DIR"),145 )

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 146–147why, one step back
146```
147

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 148why, one step back
148## `SSLKEYLOGFILE`

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 149why, one step back
149

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 150–183why, one step back
150Valid values: a filename
151152If 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.153154Support for `SSLKEYLOGFILE` requires Python 3.8 and OpenSSL 1.1.1 or newer.155156Example:157158```python159# test_script.py160import httpx161162with httpx.Client() as client:163 r = client.get("https://google.com")164```165166```console167SSLKEYLOGFILE=test.log python test_script.py168cat test.log169# TLS secrets log file, generated by OpenSSL / Python170SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX171EXPORTER_SECRET XXXX172SERVER_TRAFFIC_SECRET_0 XXXX173CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX174CLIENT_TRAFFIC_SECRET_0 XXXX175SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX176EXPORTER_SECRET XXXX177SERVER_TRAFFIC_SECRET_0 XXXX178CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX179CLIENT_TRAFFIC_SECRET_0 XXXX180```181182### Making HTTPS requests to a local server183

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 184–188why, one step back
184When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.
185186If 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:1871881. Use [trustme](https://github.com/python-trio/trustme) to generate a pair of server key/cert files, and a client cert file.

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 189–190why, one step back
1892. 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.)
1903. Tell HTTPX to use the certificates stored in `client.pem`:

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 191why, one step back
191

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 192–198why, one step back
192```pycon
193>>> import httpx194>>> context = httpx.SSLContext()195>>> context.load_verify_locations(cafile="/tmp/client.pem")196>>> r = httpx.get("https://localhost:8000", ssl_context=context)197>>> r198Response <200 OK>

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 199why, one step back
199```