From ce98e831db5372987c19876178a285b373416065 Mon Sep 17 00:00:00 2001 From: Pham Khanh Tam <84433497+AI-WFox@users.noreply.github.com> Date: Mon, 30 Nov 2026 08:39:27 +0700 Subject: [PATCH] metadata: verify HTTPS certificates unless https_allow_insecure is set BaseHTTPMetadataService passed https_allow_insecure straight to the verify argument of requests, so the default (False) turned certificate validation off and setting the option to True turned it on, the opposite of its documented meaning (CWE-295). Return the negated value instead; a configured https_ca_bundle still takes precedence. The existing test asserted on a sentinel rather than on the returned value, so it could not catch this. Replace it with tests for the default, for https_allow_insecure=True, for a CA bundle with and without the option, and for the verify value passed to requests.request. Fixes #219 Co-Authored-By: Claude Opus 5.5 Co-authored-by: gitranks[bot] <198371943+gitranks[bot]@users.noreply.github.com> Signed-off-by: Pham Khanh Tam <84433497+AI-WFox@users.noreply.github.com> --- cloudbaseinit/metadata/services/base.py | 13 +++--- .../tests/metadata/services/test_base.py | 41 +++++++++++++++---- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/cloudbaseinit/metadata/services/base.py b/cloudbaseinit/metadata/services/base.py index 4e8b64cc..91a245c2 100644 --- a/cloudbaseinit/metadata/services/base.py +++ b/cloudbaseinit/metadata/services/base.py @@ -315,16 +315,17 @@ def __init__(self, base_url, https_allow_insecure=False, self._base_url = base_url def _verify_https_request(self): - """Whether to disable the validation of HTTPS certificates. + """The `verify` argument for the HTTPS requests of this provider. - When this option is `True` the SSL certificate validation for the - current metadata provider will be disabled (please don't use it if - you don't know the implications of this behaviour). + Returns the CA bundle path when one is configured. Otherwise returns + `True`, so the certificate is validated, unless + `https_allow_insecure` is set, in which case the SSL certificate + validation is disabled (please don't use it if you don't know the + implications of this behaviour). """ if self._https_ca_bundle: return self._https_ca_bundle - else: - return self._https_allow_insecure + return not self._https_allow_insecure def _http_request(self, url, data=None, headers=None, method=None): """Get content for received url.""" diff --git a/cloudbaseinit/tests/metadata/services/test_base.py b/cloudbaseinit/tests/metadata/services/test_base.py index 17a9457e..2870f576 100644 --- a/cloudbaseinit/tests/metadata/services/test_base.py +++ b/cloudbaseinit/tests/metadata/services/test_base.py @@ -125,24 +125,49 @@ def setUp(self): self._mock_base_url = "http://metadata.mock/" self._service = base.BaseHTTPMetadataService("http://metadata.mock/") - def _test_verify_https_request(self, https_ca_bundle=None): + def _test_verify_https_request(self, expected, https_allow_insecure=False, + https_ca_bundle=None): mock_service = base.BaseHTTPMetadataService( base_url=mock.sentinel.url, - https_allow_insecure=mock.sentinel.allow_insecure, + https_allow_insecure=https_allow_insecure, https_ca_bundle=https_ca_bundle) response = mock_service._verify_https_request() - if not https_ca_bundle: - self.assertTrue(mock.sentinel.allow_insecure) - else: - self.assertEqual(response, https_ca_bundle) + self.assertIs(response, expected) def test_verify_https_request(self): - self._test_verify_https_request() + self._test_verify_https_request(expected=True) + + def test_verify_https_request_default_verifies(self): + mock_service = base.BaseHTTPMetadataService(base_url=mock.sentinel.url) + + self.assertIs(mock_service._verify_https_request(), True) + + def test_verify_https_request_allow_insecure(self): + self._test_verify_https_request(expected=False, + https_allow_insecure=True) def test_verify_https_request_with_ca_bundle(self): - self._test_verify_https_request(https_ca_bundle="/path/to/resource") + ca_bundle = "/path/to/resource" + self._test_verify_https_request(expected=ca_bundle, + https_ca_bundle=ca_bundle) + + def test_verify_https_request_with_ca_bundle_and_allow_insecure(self): + ca_bundle = "/path/to/resource" + self._test_verify_https_request(expected=ca_bundle, + https_allow_insecure=True, + https_ca_bundle=ca_bundle) + + @mock.patch('requests.request') + def test_http_request_verifies_certificate_by_default(self, + mock_request): + mock_service = base.BaseHTTPMetadataService( + base_url="https://metadata.mock/") + + mock_service._http_request("meta_data.json") + + self.assertIs(mock_request.call_args.kwargs["verify"], True) @mock.patch('requests.request') @mock.patch("cloudbaseinit.metadata.services.base.BaseHTTPMetadataService."