diff --git a/python-ecosys/requests/README.md b/python-ecosys/requests/README.md index 7d888e240..879c55cc5 100644 --- a/python-ecosys/requests/README.md +++ b/python-ecosys/requests/README.md @@ -10,14 +10,29 @@ bodies with Content-Length or Transfer-Encoding: chunked via streaming ### Limitations -* Certificate validation is not currently supported. +* Certificates are not validated unless an ``ssl`` context is passed (see below). * A dictionary passed as post data will not do automatic JSON or multipart-form encoding of post data (this can be done manually). * Compressed requests/responses are not currently supported. * File upload is not supported. * HTTP keep-alive connection reuse is not supported (Connection: close by default). +### Verifying certificates + +By default HTTPS requests accept any server certificate. To verify the +certificate chain and the hostname, pass an ``SSLContext`` as ``ssl``: + +```py +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) +``` + +The context is used for redirects too. + ### Follow-up work -* TLS certificate verification (see micropython-lib issue #838). * ``stream=True`` incremental body reads (see issue #777). diff --git a/python-ecosys/requests/manifest.py b/python-ecosys/requests/manifest.py index cbb311fbd..31535950e 100644 --- a/python-ecosys/requests/manifest.py +++ b/python-ecosys/requests/manifest.py @@ -1,3 +1,3 @@ -metadata(version="1.1.1", pypi="requests") +metadata(version="1.2.0", pypi="requests") package("requests") diff --git a/python-ecosys/requests/requests/__init__.py b/python-ecosys/requests/requests/__init__.py index b0e479ead..5a0863b9c 100644 --- a/python-ecosys/requests/requests/__init__.py +++ b/python-ecosys/requests/requests/__init__.py @@ -93,6 +93,7 @@ def request( auth=None, timeout=None, parse_headers=True, + ssl=None, ): if headers is None: headers = {} @@ -153,8 +154,10 @@ def request( try: s.connect(ai[-1]) if proto == "https:": - context = tls.SSLContext(tls.PROTOCOL_TLS_CLIENT) - context.verify_mode = tls.CERT_NONE + context = ssl + if context is None: + context = tls.SSLContext(tls.PROTOCOL_TLS_CLIENT) + context.verify_mode = tls.CERT_NONE s = context.wrap_socket(s, server_hostname=host) s.write(b"%s /%s HTTP/1.1\r\n" % (method, path)) @@ -254,9 +257,9 @@ def request( # Use the host specified in the redirect URL, as it may not be the same as the original URL. headers.pop("Host", None) if status in [301, 302, 303]: - return request("GET", redirect, None, None, headers, stream) + return request("GET", redirect, None, None, headers, stream, ssl=ssl) else: - return request(method, redirect, data, json, headers, stream) + return request(method, redirect, data, json, headers, stream, ssl=ssl) else: if chunked: resp = Response(BodyStream(s, -1)) diff --git a/python-ecosys/requests/test_requests.py b/python-ecosys/requests/test_requests.py index c12589c02..c8b5c348c 100644 --- a/python-ecosys/requests/test_requests.py +++ b/python-ecosys/requests/test_requests.py @@ -42,6 +42,33 @@ def socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP): sys.modules["socket"] = socket + + +class SSLContext: + def __init__(self, protocol=None): + self.protocol = protocol + self.verify_mode = None + self.wrapped = [] + + def wrap_socket(self, sock, server_hostname=None): + self.wrapped.append(server_hostname) + return sock + + +class tls: + PROTOCOL_TLS_CLIENT = 0 + CERT_NONE = 0 + CERT_REQUIRED = 2 + contexts = [] + + @staticmethod + def SSLContext(protocol): + context = SSLContext(protocol) + tls.contexts.append(context) + return context + + +sys.modules["tls"] = tls # ruff: noqa: E402 import requests @@ -296,6 +323,45 @@ def test_raw_readinto_content_length(): socket.socket = lambda *a, **k: Socket() +def test_https_without_ssl_skips_verification(): + tls.contexts.clear() + + requests.request("GET", "https://example.com") + + assert len(tls.contexts) == 1 + assert tls.contexts[0].verify_mode == tls.CERT_NONE + assert tls.contexts[0].wrapped == ["example.com"] + + +def test_https_wraps_with_the_given_ssl_context(): + tls.contexts.clear() + context = SSLContext() + context.verify_mode = tls.CERT_REQUIRED + + requests.get("https://example.com:8443/x", ssl=context) + + assert tls.contexts == [] + assert context.wrapped == ["example.com"] + assert context.verify_mode == tls.CERT_REQUIRED + + +def test_redirect_keeps_the_ssl_context(): + server = iter( + [ + b"HTTP/1.1 302 Found\r\nLocation: https://other.example.com/next\r\n\r\n", + SERVER_RESPONSE_200_OK, + ] + ) + socket.socket = lambda *a, **k: Socket(next(server)) + context = SSLContext() + + requests.get("https://example.com", ssl=context) + + assert context.wrapped == ["example.com", "other.example.com"] + + socket.socket = lambda *a, **k: Socket() + + def test_redirect_absolute(): server = iter( [ @@ -379,6 +445,9 @@ def test_content_length_lowercase_header(): test_post_data_str_unicode() test_post_data_bytes() test_post_chunked_data() +test_https_without_ssl_skips_verification() +test_https_wraps_with_the_given_ssl_context() +test_redirect_keeps_the_ssl_context() test_overwrite_get_headers() test_overwrite_post_json_headers() test_overwrite_post_chunked_data_headers()