Skip to content

The agent that grows with you

fix(ssl): requests gets the OS store as a bundle when no CA file exists

kshitijk4poor17200116d41c
diff --git a/agent/ssl_verify.py b/agent/ssl_verify.py
index acfb6a7c139..2d60a8b5a97 100644
--- a/agent/ssl_verify.py
+++ b/agent/ssl_verify.py
@@ -254,7 +254,32 @@ def requests_verify() -> bool | str:
for candidate in (defaults.cafile, defaults.capath):
if candidate and Path(candidate).exists():
return candidate
- return True
+ # Windows keeps its roots in the system store with no OpenSSL file, and ``True``
+ # would send requests back to the dead frozen path: hand it the store as a bundle.
+ return _platform_store_bundle() or True
+
+
+_PLATFORM_STORE_BUNDLE: Optional[str] = None
+
+
+def _platform_store_bundle() -> Optional[str]:
+ """The OS store's CA certificates written once to a PEM file under HERMES_HOME."""
+ global _PLATFORM_STORE_BUNDLE
+ if _PLATFORM_STORE_BUNDLE and Path(_PLATFORM_STORE_BUNDLE).is_file():
+ return _PLATFORM_STORE_BUNDLE
+ ctx = _stdlib_ssl_context_class()(ssl.PROTOCOL_TLS_CLIENT)
+ ctx.load_default_certs()
+ certs = ctx.get_ca_certs(binary_form=True)
+ if not certs:
+ return None
+ from hermes_constants import get_hermes_home
+ from utils import atomic_write_text
+
+ path = get_hermes_home() / "cache" / "platform-ca-bundle.pem"
+ path.parent.mkdir(parents=True, exist_ok=True)
+ atomic_write_text(path, "".join(ssl.DER_cert_to_PEM_cert(der) for der in certs))
+ _PLATFORM_STORE_BUNDLE = str(path)
+ return _PLATFORM_STORE_BUNDLE
def _default_verify() -> bool | ssl.SSLContext:
diff --git a/tests/agent/test_ssl_verify.py b/tests/agent/test_ssl_verify.py
index c64b9e2c8b8..6f0789a16a2 100644
--- a/tests/agent/test_ssl_verify.py
+++ b/tests/agent/test_ssl_verify.py
@@ -468,3 +468,30 @@ def test_requests_verify_keeps_requests_own_ca_overrides(monkeypatch, no_ca_env)
monkeypatch.setattr("requests.utils.DEFAULT_CA_BUNDLE_PATH", "/gone/cacert.pem")
assert ssl_verify.requests_verify() is True
+
+
+def test_requests_verify_hands_over_the_platform_store_without_openssl_files(tmp_path, monkeypatch, no_ca_env):
+ """Windows: requests' frozen bundle is gone, certifi cannot be re-derived and OpenSSL has no
+ cafile/capath. ``True`` would make requests raise on the dead path, so the OS store goes over
+ as a PEM bundle that requests accepts."""
+ import ssl
+ from types import SimpleNamespace
+
+ import requests
+
+ from agent import ssl_verify
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))
+ monkeypatch.delenv("REQUESTS_CA_BUNDLE", raising=False)
+ monkeypatch.delenv("CURL_CA_BUNDLE", raising=False)
+ monkeypatch.setattr(ssl_verify, "_PLATFORM_STORE_BUNDLE", None)
+ monkeypatch.setattr(ssl_verify, "certifi_bundle_path", lambda: None)
+ monkeypatch.setattr("requests.utils.DEFAULT_CA_BUNDLE_PATH", str(tmp_path / "gone" / "cacert.pem"))
+ monkeypatch.setattr(ssl, "get_default_verify_paths", lambda: SimpleNamespace(cafile=None, capath=None))
+
+ verify = ssl_verify.requests_verify()
+ assert isinstance(verify, str) and Path(verify).is_relative_to(tmp_path / "home")
+ assert "BEGIN CERTIFICATE" in Path(verify).read_text()
+ # requests takes the bundle: the request fails on the closed port, not on a missing CA file.
+ with pytest.raises(requests.exceptions.ConnectionError):
+ requests.get("https://127.0.0.1:9/", verify=verify, timeout=2)