Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(proxy): treat NODE_EXTRA_CA_CERTS as additive, not replacement (#998) (#1031)
Shengbo_Wangc98728363a10
diff --git a/headroom/proxy/ssl_context.py b/headroom/proxy/ssl_context.pyindex 780e7a35a..55215932b 100644--- a/headroom/proxy/ssl_context.py+++ b/headroom/proxy/ssl_context.py@@ -7,37 +7,41 @@ deployments with custom certificate authorities work without extraconfiguration.Priority order (first match wins):-1. ``SSL_CERT_FILE``-2. ``REQUESTS_CA_BUNDLE``-3. ``NODE_EXTRA_CA_CERTS``+1. ``SSL_CERT_FILE`` — replacement semantics (only these CAs are trusted)+2. ``REQUESTS_CA_BUNDLE`` — replacement semantics+3. ``NODE_EXTRA_CA_CERTS`` — **additive** semantics (extra roots loaded+ on top of the default/system trust store, matching Node.js behavior)"""from __future__ import annotationsimport loggingimport os+import ssllogger = logging.getLogger("headroom.proxy")-_CA_BUNDLE_ENV_VARS = (+_REPLACEMENT_CA_VARS = ("SSL_CERT_FILE","REQUESTS_CA_BUNDLE",- "NODE_EXTRA_CA_CERTS",)-def find_ca_bundle() -> str | None:- """Return the CA bundle path if any CA-bundle env var points to a file.+def find_ca_bundle() -> str | ssl.SSLContext | None:+ """Return a CA verification target for httpx's ``verify=`` parameter.- Iterates ``SSL_CERT_FILE``, ``REQUESTS_CA_BUNDLE``, and- ``NODE_EXTRA_CA_CERTS`` in that order. The first variable that is set- *and* points to an existing file is returned as a string path so that- httpx can build its own SSL context (with correct ALPN setup for HTTP/2).+ ``SSL_CERT_FILE`` and ``REQUESTS_CA_BUNDLE`` use **replacement**+ semantics: the returned path becomes the *only* trust store.++ ``NODE_EXTRA_CA_CERTS`` uses **additive** semantics (matching Node.js):+ an ``ssl.SSLContext`` is returned that contains the default/system+ roots *plus* the extra certificate, so public upstreams stay reachable+ when the extra bundle contains only a private/internal root.Returns ``None`` when no env var is set (or all paths are missing),which signals to the caller to use httpx's default TLS verification."""- for var in _CA_BUNDLE_ENV_VARS:+ for var in _REPLACEMENT_CA_VARS:path = os.environ.get(var)if path and os.path.isfile(path):logger.info(@@ -52,4 +56,21 @@ def find_ca_bundle() -> str | None:var,path,)++ node_path = os.environ.get("NODE_EXTRA_CA_CERTS")+ if node_path and os.path.isfile(node_path):+ ctx = ssl.create_default_context()+ ctx.load_verify_locations(cafile=node_path)+ ctx.set_alpn_protocols(["h2", "http/1.1"])+ logger.info(+ "event=ssl_ca_bundle_loaded env_var=NODE_EXTRA_CA_CERTS path=%s additive=true",+ node_path,+ )+ return ctx+ if node_path and not os.path.isfile(node_path):+ logger.warning(+ "event=ssl_ca_bundle_missing env_var=NODE_EXTRA_CA_CERTS path=%r (skipped)",+ node_path,+ )+return Nonediff --git a/tests/test_ssl_context.py b/tests/test_ssl_context.pyindex 23c178881..619827697 100644--- a/tests/test_ssl_context.py+++ b/tests/test_ssl_context.py@@ -4,7 +4,8 @@ Covers:- Returns None when no env var is set- Returns a path string when SSL_CERT_FILE points to a valid PEM file- Returns a path string when REQUESTS_CA_BUNDLE points to a valid PEM file-- Returns a path string when NODE_EXTRA_CA_CERTS points to a valid PEM file+- Returns an ssl.SSLContext when NODE_EXTRA_CA_CERTS points to a valid PEM file+- The SSLContext is additive: default/system roots are preserved (#998)- Priority order: SSL_CERT_FILE beats REQUESTS_CA_BUNDLE beats NODE_EXTRA_CA_CERTS- Nonexistent paths are skipped (returns None if all paths are missing)"""@@ -12,6 +13,7 @@ Covers:from __future__ import annotationsimport os+import sslimport pytest@@ -78,12 +80,23 @@ class TestFindCaBundleWithValidPem:assert isinstance(ctx, str)assert os.path.isfile(ctx)- def test_node_extra_ca_certs_returns_path(self, monkeypatch, ca_pem_file):+ def test_node_extra_ca_certs_returns_ssl_context(self, monkeypatch, ca_pem_file):+ """NODE_EXTRA_CA_CERTS returns an SSLContext, not a bare path (#998)."""_clean_env(monkeypatch)monkeypatch.setenv("NODE_EXTRA_CA_CERTS", ca_pem_file)ctx = find_ca_bundle()- assert isinstance(ctx, str)- assert os.path.isfile(ctx)+ assert isinstance(ctx, ssl.SSLContext)++ def test_node_extra_ca_certs_is_additive(self, monkeypatch, ca_pem_file):+ """The SSLContext must contain default/system roots plus the extra cert (#998)."""+ _clean_env(monkeypatch)+ monkeypatch.setenv("NODE_EXTRA_CA_CERTS", ca_pem_file)+ ctx = find_ca_bundle()+ assert isinstance(ctx, ssl.SSLContext)+ stats = ctx.cert_store_stats()+ # The default trust store has dozens of CAs; if only the test cert+ # were loaded (replacement), x509_ca would be 1.+ assert stats["x509_ca"] > 1class TestFindCaBundlePriority: