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(security): exempt only GET health probes from the proxy token (#3921)
Satya Bharadwajd99779dae69d
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.pyindex c9e8016bf..4d36dcbd3 100644--- a/headroom/proxy/server.py+++ b/headroom/proxy/server.py@@ -3989,7 +3989,12 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:_proxy_token_bytes = _proxy_token.encode("utf-8") if _proxy_token else b""# Health/readiness probes must stay reachable without a token so# orchestrators can check a container that binds non-loopback.- _AUTH_EXEMPT_PATHS = frozenset({"/health", "/healthz", "/livez", "/readyz"})+ #+ # The exemption covers only GET, and only paths with a GET handler below.+ # Anything else on these paths falls through to the catch-all passthrough+ # relay (FastAPI's @app.get does not register HEAD), so exempting it would+ # let an unauthenticated caller relay arbitrary requests upstream.+ _AUTH_EXEMPT_PATHS = frozenset({"/health", "/livez", "/readyz"})# A non-loopback bind with no token is the exact shape (``--host 0.0.0.0``# from any launcher) that exposes the unauthenticated /v1/* relay to the@@ -4050,7 +4055,8 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:path = request.url.pathclient = getattr(request, "client", None)client_host = getattr(client, "host", None) if client is not None else None- if path not in _AUTH_EXEMPT_PATHS and not is_loopback_host(client_host):+ exempt = request.method == "GET" and path in _AUTH_EXEMPT_PATHS+ if not exempt and not is_loopback_host(client_host):provided = _extract_proxy_token(request.headers)if provided is None or not hmac.compare_digest(provided.encode("utf-8", "replace"), _proxy_token_bytesdiff --git a/tests/test_proxy_hardening.py b/tests/test_proxy_hardening.pyindex 212efc638..1151c0185 100644--- a/tests/test_proxy_hardening.py+++ b/tests/test_proxy_hardening.py@@ -16,11 +16,13 @@ import pytestpytest.importorskip("fastapi")+from fastapi.responses import JSONResponsefrom fastapi.testclient import TestClientfrom headroom.cache.compression_store import reset_compression_storefrom headroom.offline import apply_offline_env, is_offlinefrom headroom.proxy.audit import is_auditable_path+from headroom.proxy.handlers.openai import OpenAIHandlerMixinfrom headroom.proxy.server import (ProxyConfig,WebSocketAuthMiddleware,@@ -127,6 +129,36 @@ class TestInboundAuthToken:assert c.get("/livez").status_code == 200assert c.get("/readyz").status_code in (200, 503) # ready/not-ready, never 401+ @pytest.mark.parametrize(+ ("method", "path"),+ [+ ("GET", "/healthz"), # no Python route: lands on the catch-all+ ("POST", "/health"),+ ("PUT", "/livez"),+ ("DELETE", "/readyz"),+ ("HEAD", "/livez"), # @app.get does not register HEAD+ ],+ )+ def test_health_exemption_does_not_reach_passthrough(self, monkeypatch, method, path):+ """Only GET probes are exempt from the token.++ Any other request on a health path is not served by the health handler+ but by the catch-all passthrough, which relays it upstream. Exempting+ those turned the proxy into an unauthenticated relay.+ """+ relayed: list[tuple[str, str]] = []++ async def _spy_passthrough(self, request, base_url, *args, **kwargs):+ relayed.append((request.method, request.url.path))+ return JSONResponse({"relayed_to": base_url})++ monkeypatch.setattr(OpenAIHandlerMixin, "handle_passthrough", _spy_passthrough)+ app = _make_app(proxy_token="s3cr3t-token")+ with TestClient(app, base_url="http://testserver", client=NONLOOPBACK) as c:+ resp = c.request(method, path)+ assert resp.status_code == 401+ assert relayed == []+# ──────────────────── 2.1b inbound auth token over WebSocket ──────────────