The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(ssl): trust SSL_CERT_FILE on top of the platform store, not instead of it
kshitijk4poorf95f54314a94
diff --git a/agent/ssl_verify.py b/agent/ssl_verify.pyindex 9cc0d13b152..1fc289aad19 100644--- a/agent/ssl_verify.py+++ b/agent/ssl_verify.py@@ -70,7 +70,7 @@ def _coerce_insecure(ssl_verify: Any) -> bool:return False-_CA_CONTEXTS: dict[str | None, ssl.SSLContext] = {}+_CA_CONTEXTS: dict[tuple[str | None, bool], ssl.SSLContext] = {}_CA_CONTEXTS_LOCK = threading.Lock()@@ -92,12 +92,21 @@ def _stdlib_ssl_context_class() -> type[ssl.SSLContext]:return _original_SSLContext-def _shared_context(ca_path: str | None) -> ssl.SSLContext:- """A stable context identity lets clients reuse the existing transport pool."""+def _shared_context(ca_path: str | None, union: bool = False) -> ssl.SSLContext:+ """A stable context identity lets clients reuse the existing transport pool.++ ``union`` adds ``ca_path`` to the platform store (the ``SSL_CERT_FILE``+ contract) instead of replacing it (a provider ``ssl_ca_cert``).+ """with _CA_CONTEXTS_LOCK:- ctx = _CA_CONTEXTS.get(ca_path)+ ctx = _CA_CONTEXTS.get((ca_path, union))if ctx is None:- if ca_path is not None:+ if union:+ ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)+ if not _installed:+ ctx.load_default_certs()+ ctx.load_verify_locations(cafile=ca_path)+ elif ca_path is not None:# PROTOCOL_TLS_CLIENT sets hostname checking and CERT_REQUIRED;# assigning the original class's properties after injection recurses.ctx = _stdlib_ssl_context_class()(ssl.PROTOCOL_TLS_CLIENT)@@ -106,20 +115,21 @@ def _shared_context(ca_path: str | None) -> ssl.SSLContext:ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)if not _installed:ctx.load_default_certs()- _CA_CONTEXTS[ca_path] = ctx+ _CA_CONTEXTS[(ca_path, union)] = ctxreturn ctxdef platform_ssl_context() -> ssl.SSLContext:"""The process-wide client context (cheap after the first call).- An existing ``SSL_CERT_FILE`` bundle wins, as it did for httpx ``verify=True``;- a missing or stale path falls back to the platform store.+ An existing ``SSL_CERT_FILE`` bundle is trusted ON TOP of the platform store,+ as it was for httpx ``verify=True``; a missing or stale path falls back to+ the platform store alone."""install_truststore()ca = os.environ.get("SSL_CERT_FILE", "").strip()if ca and Path(ca).is_file():- return _shared_context(str(Path(ca).resolve()))+ return _shared_context(str(Path(ca).resolve()), union=True)return _shared_context(None)diff --git a/tests/gateway/test_telegram_closewait_limits.py b/tests/gateway/test_telegram_closewait_limits.pyindex 511aef4bf31..adbf8d5205f 100644--- a/tests/gateway/test_telegram_closewait_limits.py+++ b/tests/gateway/test_telegram_closewait_limits.py@@ -176,7 +176,8 @@ def test_all_ptb_clients_share_the_platform_ssl_context(monkeypatch):passassert seen.get("verify") is shared- # An SSL_CERT_FILE bundle is honoured (as httpx verify=True did) and still cached once.+ # An SSL_CERT_FILE bundle is honoured ON TOP of the platform store (as httpx+ # verify=True did) and still cached once.import certififrom agent import ssl_verifymonkeypatch.setattr(ssl_verify, "_CA_CONTEXTS", {})@@ -184,7 +185,8 @@ def test_all_ptb_clients_share_the_platform_ssl_context(monkeypatch):bundle_ctx = ssl_verify.platform_ssl_context()assert bundle_ctx is ssl_verify.platform_ssl_context()assert bundle_ctx is not ssl_verify._shared_context(None)- assert bundle_ctx.cert_store_stats()["x509_ca"] > 0+ assert type(bundle_ctx) is ssl.SSLContext # truststore-injected: bundle + OS store+ assert bundle_ctx._ctx.cert_store_stats()["x509_ca"] > 0def test_proxy_branch_general_pool_has_tight_keepalive(monkeypatch):