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 fileTake it
Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 1–2why, one step back1When 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).2Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 3why, one step back3### Enabling and disabling verificationDocs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 4why, one step back4Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 5why, one step back5By default httpx will verify HTTPS connections, and raise an error for invalid SSL cases...Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 6why, one step back6Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 7–10why, one step back7```pycon8>>> 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 back11Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 12why, one step back12Verification 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 back13Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 14–19why, one step back14```pycon15>>> 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 back20```21Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 22why, one step back22You can use this to disable verification completely and allow insecure requests...Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 23–24why, one step back2324```pyconIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 25–28why, one step back25>>> 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 back29<Response [200 OK]>30```31Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 32why, one step back32### Configuring client instancesDocs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 33why, one step back33Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 34–35why, one step back34If you're using a `Client()` instance you should pass any SSL context when instantiating the client.35Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 36why, one step back36```pyconIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 37–38why, one step back37>>> context = httpx.SSLContext()38>>> client = httpx.Client(ssl_context=context)Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 39–40why, one step back39```40Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 41why, one step back41The `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 back42Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 43–50why, one step back43If 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:50Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 51why, one step back51```pyconIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 52–55why, one step back52>>> 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 back56<Response [200 OK]>57```58Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 59why, one step back59Or by providing an certificate directory:Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 60why, one step back60Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 61–67why, one step back61```pycon62>>> 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 back68Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 69–77why, one step back69### Client side certificates7071You 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 back78```79Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 80why, one step back80Or including a keyfile...Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 81why, one step back81Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 82–90why, one step back82```pycon83>>> 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 back91Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 92–103why, one step back92Or 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 back104```105Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 106why, one step back106### Using alternate SSL contextsDocs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 107why, one step back107Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 108why, one step back108You can also use an alternate `ssl.SSLContext` instances.Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 109why, one step back109Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 110why, one step back110For example, [using the `truststore` package](https://truststore.readthedocs.io/)...Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 111–112why, one step back111112```pythonIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 113–118why, one step back113import ssl114import 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 back119```120Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 121why, one step back121Or 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 back122123```pythonIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 124–128why, one step back124import ssl125import httpx126127ssl_context = ssl.create_default_context()128client = httpx.Client(ssl_context=ssl_context)Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 129–130why, one step back129```130Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 131why, one step back131### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR`Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 132why, one step back132Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 133–136why, one step back133Unlike `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...136Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 137why, one step back137```pythonIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 138–145why, one step back138context = 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 back146```147Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 148why, one step back148## `SSLKEYLOGFILE`Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 149why, one step back149Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 150–183why, one step back150Valid values: a filename151152If 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 server183Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 184–188why, one step back184When 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 back1892. 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 back191Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 192–198why, one step back192```pycon193>>> 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 back199```