Skip to content

requests: Add ssl argument to pass a verifying SSLContext. - #1175

Open
joshdoesthis wants to merge 2 commits into
micropython:masterfrom
joshdoesthis:requests-ssl-context
Open

joshdoesthis wants to merge 2 commits into
micropython:masterfrom
joshdoesthis:requests-ssl-context

Conversation

@joshdoesthis

Copy link
Copy Markdown

requests builds its own tls.SSLContext and sets verify_mode = tls.CERT_NONE, with no way to change it. So HTTPS requests accept any server certificate, as reported in #838.

This adds an optional ssl argument to request() and the method helpers. It takes an SSLContext, as umqtt.simple does. When it's given, requests wraps the socket with that context and passes the host as server_hostname, so a context with CERT_REQUIRED and loaded CA certificates verifies the chain and the hostname. When it's None, nothing changes.

import tls, requests
ctx = tls.SSLContext(tls.PROTOCOL_TLS_CLIENT)
ctx.verify_mode = tls.CERT_REQUIRED
ctx.load_verify_locations(root_der)
requests.get("https://example.com", ssl=ctx)

Tested on the unix port and on an RPI_PICO2_W running 1.29.0, against a server with a valid chain and one with a self-signed certificate (rejected). A test is added under python-ecosys/requests/test_requests.py. Version bumped to 1.2.0.

Fixes #838.

Joshua Wilson and others added 2 commits November 12, 2026 15:09
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
keep using it. When None, behaviour is unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Joshua Wilson <joshua@Joshuas-Mac-mini.local>
tls.SSLContext.load_verify_locations is a C method that takes no
keyword arguments, so cadata=root_der raises TypeError on a board.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Joshua Wilson <joshua@Joshuas-Mac-mini.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SECURITY: Requests module HTTPS - no server certificate verification.

1 participant