Skip to content

The agent that grows with you

fix(urllib): never memoise a CA context built from a fallback bundle

kshitijk4poor5fc0af7050b9
diff --git a/hermes_cli/urllib_security.py b/hermes_cli/urllib_security.py
index 0329ae56331..e6b9b1ca231 100644
--- a/hermes_cli/urllib_security.py
+++ b/hermes_cli/urllib_security.py
@@ -142,22 +142,25 @@ def _resolved_https_context() -> ssl.SSLContext | None:
cached = _HTTPS_CONTEXT_CACHE
if cached is not None and cached[0] == key:
return cached[1]
- context = _build_https_context(candidates)
- if context is not None:
- # A failed (possibly transient) load must not pin default certs until the file changes.
+ context, used_path = _build_https_context(candidates)
+ if context is not None and candidates and used_path == candidates[0]:
+ # Only a context built from the preferred bundle is memoised. A failed (possibly transient)
+ # load — whether it left us with no context or with a fallback bundle (certifi on macOS) —
+ # must be retried on the next request, not pinned until the file's mtime/size changes.
_HTTPS_CONTEXT_CACHE = (key, context)
return context
-def _build_https_context(candidates: tuple[str, ...]) -> ssl.SSLContext | None:
+def _build_https_context(candidates: tuple[str, ...]) -> tuple[ssl.SSLContext | None, str | None]:
+ """Return ``(context, path)`` for the first loadable candidate, or ``(None, None)``."""
for path in candidates:
try:
- return ssl.create_default_context(cafile=path)
+ return ssl.create_default_context(cafile=path), path
except (OSError, ssl.SSLError) as exc:
logger.warning(
"CA bundle could not be loaded from %s: %s — falling back to default certificates", path, exc
)
- return None
+ return None, None
def _secure_opener_from_installed_policy(original_url: str, *, ssl_context=None):
diff --git a/tests/hermes_cli/test_urllib_security.py b/tests/hermes_cli/test_urllib_security.py
index ecf853bed59..eabd5ed69e3 100644
--- a/tests/hermes_cli/test_urllib_security.py
+++ b/tests/hermes_cli/test_urllib_security.py
@@ -609,3 +609,35 @@ def test_a_failed_bundle_load_is_not_memoised(monkeypatch, tmp_path):
assert state["loads"] == 2
assert urllib_security._resolved_https_context() is recovered
assert state["loads"] == 2
+
+
+def test_a_fallback_context_is_not_memoised(monkeypatch, tmp_path):
+ """A context built from a fallback bundle (certifi on macOS) must not pin the failed primary."""
+ import hermes_cli.urllib_security as urllib_security
+
+ _clear_ca_bundle_env(monkeypatch)
+ corporate = str(tmp_path / "corporate-ca.pem")
+ fallback = str(tmp_path / "cacert.pem")
+ for path in (corporate, fallback):
+ (tmp_path / path).write_text("pem")
+ monkeypatch.setattr(urllib_security, "_ca_bundle_candidates", lambda: (corporate, fallback))
+
+ state = {"failing": True, "loads": []}
+
+ def load_verify_locations(self, cafile=None, capath=None, cadata=None):
+ state["loads"].append(cafile)
+ if state["failing"] and cafile == corporate:
+ raise ssl.SSLError("transient read failure")
+
+ monkeypatch.setattr(ssl.SSLContext, "load_verify_locations", load_verify_locations)
+
+ assert urllib_security._resolved_https_context() is not None
+ assert state["loads"] == [corporate, fallback]
+
+ state["failing"] = False
+ recovered = urllib_security._resolved_https_context()
+
+ assert recovered is not None
+ assert state["loads"] == [corporate, fallback, corporate]
+ assert urllib_security._resolved_https_context() is recovered
+ assert state["loads"] == [corporate, fallback, corporate]