Skip to content

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.

fix(proxy): restrict urlopen SSL context ALPN to http/1.1 (#3567)

Waqas Ahmed5baa439ebf4a
diff --git a/headroom/proxy/ssl_context.py b/headroom/proxy/ssl_context.py
index 058c70d1f..9e8396e31 100644
--- a/headroom/proxy/ssl_context.py
+++ b/headroom/proxy/ssl_context.py
@@ -198,7 +198,12 @@ def build_urlopen_context() -> ssl.SSLContext | None:
"""
verify = build_httpx_verify()
- return verify if isinstance(verify, ssl.SSLContext) else None
+ if not isinstance(verify, ssl.SSLContext):
+ return None
+ # urllib.request/http.client only implements HTTP/1.1 framing. Offering h2
+ # can make a TLS-inspecting MITM negotiate a protocol it cannot parse.
+ verify.set_alpn_protocols(["http/1.1"])
+ return verify
def apply_global_tls_relaxation() -> bool:
diff --git a/tests/test_ssl_context.py b/tests/test_ssl_context.py
index 9b78ae874..5d8ee7706 100644
--- a/tests/test_ssl_context.py
+++ b/tests/test_ssl_context.py
@@ -21,6 +21,7 @@ from headroom.proxy import ssl_context
from headroom.proxy.ssl_context import (
apply_global_tls_relaxation,
build_httpx_verify,
+ build_urlopen_context,
find_ca_bundle,
tls_strict_disabled,
)
@@ -286,6 +287,43 @@ class TestBuildHttpxVerify:
assert ctx.cert_store_stats()["x509_ca"] == 1
+class TestBuildUrlopenContext:
+ def test_custom_ca_context_only_offers_http_1_1(self, monkeypatch, ca_pem_file):
+ _clean_env(monkeypatch)
+ monkeypatch.setenv("SSL_CERT_FILE", ca_pem_file)
+ created_context = FakeSSLContext()
+
+ def fake_create_default_context(*, cafile: str | None = None):
+ assert cafile == ca_pem_file
+ return created_context
+
+ monkeypatch.setattr(ssl_context.ssl, "SSLContext", FakeSSLContext)
+ monkeypatch.setattr(ssl_context.ssl, "create_default_context", fake_create_default_context)
+
+ ctx = build_urlopen_context()
+
+ assert ctx is created_context
+ assert created_context.alpn_protocols == ["http/1.1"]
+
+ def test_default_returns_none(self, monkeypatch):
+ """No CA bundle, strict on → build_httpx_verify() is True, nothing to restrict."""
+ _clean_env(monkeypatch)
+ assert build_urlopen_context() is None
+
+ def test_toggle_off_context_only_offers_http_1_1(self, monkeypatch):
+ """No CA bundle, strict OFF → still a real context, still restricted to http/1.1."""
+ _clean_env(monkeypatch)
+ monkeypatch.setenv("HEADROOM_TLS_STRICT", "0")
+ created_context = FakeSSLContext()
+ monkeypatch.setattr(ssl_context.ssl, "SSLContext", FakeSSLContext)
+ monkeypatch.setattr(ssl_context.ssl, "create_default_context", lambda: created_context)
+
+ ctx = build_urlopen_context()
+
+ assert ctx is created_context
+ assert created_context.alpn_protocols == ["http/1.1"]
+
+
class TestApplyGlobalTlsRelaxation:
def test_noop_when_strict_on(self, monkeypatch):
_clean_env(monkeypatch)