Repository navigation
requests: Add ssl argument to pass a verifying SSLContext. - #1175
Open
joshdoesthis wants to merge 1 commit into
Open
joshdoesthis wants to merge 1 commit into
joshdoesthis wants to merge 1 commit into
Conversation
joshdoesthis
force-pushed
the
requests-ssl-context
branch
4 times, most recently
from
November 17, 2026 02:23
f55bb25 to
90deb4e
Compare
requests builds its own tls.SSLContext with verify_mode CERT_NONE and offers no way to change it, so HTTPS requests accept any certificate. Add an optional ssl argument to request() and the method helpers that takes an SSLContext, as umqtt.simple does. When given, the socket is wrapped with it and the host passed as server_hostname, and redirects to https keep using it. An http URL, or a redirect to one, raises ValueError instead of sending the request unencrypted. When ssl is None, requests are sent as before. Close the socket on any exception, not only OSError: a certificate that fails verification raises ValueError, and running out of memory raises MemoryError. This also stops the socket leaking on other errors, such as a non-redirect status with a Location header. Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Joshua Wilson <joshua@qualithm.com>
joshdoesthis
force-pushed
the
requests-ssl-context
branch
from
November 17, 2026 05:16
90deb4e to
aa9326d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
requestsbuilds its owntls.SSLContextand setsverify_mode = tls.CERT_NONE, and a caller can't change that. So HTTPS requests accept any server certificate (#838).This adds an optional
sslargument torequest()and the method helpers. It takes anSSLContext, asumqtt.simpledoes.server_hostname. ACERT_REQUIREDcontext with a root CA loaded then verifies both the chain and the hostname.https://are wrapped with the same context, against the redirect's own host.sslgiven, anhttp://URL, or a redirect to one, raisesValueErrorinstead of sending the request unencrypted.sslisNone, requests are sent as before.The socket is now closed on any exception, not only
OSError. A certificate that fails verification raisesValueError, and running out of memory raisesMemoryError; both left the socket open. The same change also stops leaks that already existed, for example on a 401 that has aLocationheader.The README covers what a verifying context needs (
CERT_REQUIRED, a root, a set clock, an mbedTLS port) and what it raises. The version goes to 1.2.0.Part of #838 is still open after this. Verifying by default needs a source of root certificates on the device (for example #633, or the ESP-IDF certificate bundle discussed there), so this PR refers to the issue rather than closing it.
Testing
test_requests.pygains six tests. The last three fail without this change.ssl, the request still usesCERT_NONE.server_hostname.ValueError,MemoryErrorandAttributeErrorfromwrap_socket()close the socket.sslwith anhttp://URL raises before any socket is opened.http://raises, and the first socket is closed.I also ran a path test against real TLS servers. It runs on a board through
mpremote, or on the unix port, with test servers using only CPython's standard library and certificates made byopenssl: https://gist.github.com/joshdoesthis/03410378b6b2a9b837abb11510661892. It covers:http://.timeout), a bad status line, and exceptions fromwrap_socket().Results on stock MicroPython 1.29.0 release images:
ssl: V2-V5CERT_REQUIRED, no root loadedsslwith anhttp://URLhttp://ValueErrorfromwrap_socket()MemoryErrorfromwrap_socket()OSErrorfromwrap_socket()ssl=Truetimeout=2Locationdata=andjson=togetherEvery case ran on every target with the PR's previous revision. The cases this revision changes (
sslwithhttp://, the redirect tohttp://, and the socket closing on other exceptions) were rerun on this revision on the unix port, the ESP32-S3 and the ESP32-C6. On the Pico 2 W and the ESP32-C3 they show "not rerun".Memory on an ESP32-C3 without PSRAM (ESP32_GENERIC_C3 1.29.0), from a reset, 3 runs each:
CERT_REQUIRED, nothing else loadedCERT_NONE, nothing else loadedCERT_REQUIRED, 50 KB of Python heap heldCERT_NONE, 50 KB of Python heap heldVerification itself costs about 0.8 KB of ESP-IDF heap for the context with one root loaded, and about 0.7 KB more during the session. All 12 handshakes succeeded. With 85 KB held, both verifying handshakes I tried failed with
OSError: ENOMEM once and ETIMEDOUT once, as in micropython/micropython#19946. I didn't runCERT_NONEat 85 KB.Not tested: esp8266 and other axTLS builds, which can't verify certificates (their
verify_modesetter is a no-op). I haven't tested other ports either.Trade-offs and Alternatives
.mpyforrequestsgoes from 3178 to 3247 bytes (+69), measured with mpy-cross 1.29.0.sslstill accept any certificate, so existing code keeps working but isn't any safer.requestsfollows an https→http redirect. Here, withsslgiven, it raises: a caller who asked for verification would otherwise get a plain-HTTP response, with itsauth=header sent in the clear, and no way to tell. Withoutssl, redirects behave as before.requestshas nosslargument; the equivalent there isverify, which takesTrue,Falseor a path to a CA bundle. I usedssl=to matchumqtt.simple, and because a context can also hold roots that are only in memory. If you'd rather keeprequestsa strict subset of CPython, I can switch toverify="ca.pem"andverify=False.verify=Truewould have to raise, since there's no default CA store. Which would you prefer?ssl=True.asyncio.open_connection()andaiohttpalso acceptssl=True. Heresslmust be a context orNone; anything else raisesAttributeError, with the socket closed.timeoutandparse_headers.Content-LengthandContent-Type.json=followed by a 307/308 raisesAssertionError.Authorizationfromauth=is sent to any redirect target.head(...).contentwaits for a body that never comes.Generative AI
I used generative AI tools when creating this PR, but a human has checked the code and is responsible for the code and the description above.
Refs #838.