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): allow settings routes for trusted gateway/dashboard clients (#2491)
Parideboya5b0a8f4cc54
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.pyindex 21d9fe26c..ea9615fcc 100644--- a/headroom/proxy/server.py+++ b/headroom/proxy/server.py@@ -3342,6 +3342,40 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:from headroom.proxy.loopback_guard import require_loopback as _require_loopbackfrom headroom.proxy.loopback_guard import require_same_origin as _require_same_origin+ def _require_loopback_or_trusted_dashboard_client(request: Request) -> None:+ """Allow loopback callers, or gateway-forwarded dashboard clients.++ Mirrors the trust chain already used by /stats and /stats-lifetime+ (see _request_can_view_dashboard_metadata) so the settings UI works+ the same way behind a reverse-proxy/gateway (issue #2466).+ """+ if not _request_can_view_dashboard_metadata(request, trusted_dashboard_client_cidrs):+ raise HTTPException(status_code=404)++ def _require_same_origin_or_trusted_dashboard_client(request: Request) -> None:+ """Same-origin CSRF guard for settings writes, trusted-dashboard aware.++ ``require_same_origin`` only accepts an ``Origin`` that itself names a+ loopback host, so a browser POST from a trusted-gateway dashboard+ client was rejected even though the paired GET routes allow that same+ caller (issue #2466). For non-loopback callers, accept an ``Origin``+ that matches this request's own Host header, provided the caller is+ already an IP-literal-Host, CIDR-trusted dashboard client. Loopback+ callers keep the stricter loopback-only origin check unchanged.+ """+ if not _request_is_loopback(request):+ origin = request.headers.get("origin")+ host_header = request.headers.get("host")+ if (+ origin+ and origin != "null"+ and host_header+ and _request_has_same_origin_or_no_provenance(request, host_header)+ and _request_can_view_dashboard_metadata(request, trusted_dashboard_client_cidrs)+ ):+ return+ _require_same_origin(request)+@app.get("/admin/upstream", dependencies=[Depends(_require_loopback)])async def get_upstream():"""Current Anthropic upstream + cc-switch reconciler state (loopback-only).@@ -3462,7 +3496,9 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:# (Phase 3's /settings/apply drives that).from headroom import settings_store- @app.get("/settings/schema", dependencies=[Depends(_require_loopback)])+ @app.get(+ "/settings/schema", dependencies=[Depends(_require_loopback_or_trusted_dashboard_client)]+ )async def settings_schema(_request: Request):"""Registry + grouped fields + effective values for the settings form."""schema = settings_store.to_schema()@@ -3479,12 +3515,18 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:schema["supervised"] = Falsereturn JSONResponse(status_code=200, content=schema)- @app.get("/settings", dependencies=[Depends(_require_loopback)])+ @app.get("/settings", dependencies=[Depends(_require_loopback_or_trusted_dashboard_client)])async def settings_get(_request: Request):"""Return stored (file) values only; secret fields masked."""return JSONResponse(status_code=200, content=settings_store.stored_values())- @app.post("/settings", dependencies=[Depends(_require_loopback), Depends(_require_same_origin)])+ @app.post(+ "/settings",+ dependencies=[+ Depends(_require_loopback_or_trusted_dashboard_client),+ Depends(_require_same_origin_or_trusted_dashboard_client),+ ],+ )async def settings_post(request: Request):"""Persist settings. Unknown key -> 400; bad type/enum/range -> 422.@@ -3529,7 +3571,11 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:)@app.post(- "/settings/apply", dependencies=[Depends(_require_loopback), Depends(_require_same_origin)]+ "/settings/apply",+ dependencies=[+ Depends(_require_loopback_or_trusted_dashboard_client),+ Depends(_require_same_origin_or_trusted_dashboard_client),+ ],)async def settings_apply(request: Request):"""Persist settings (optional body) then restart the proxy to apply them.@@ -3592,7 +3638,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:@app.get("/dashboard/settings",response_class=HTMLResponse,- dependencies=[Depends(_require_loopback)],+ dependencies=[Depends(_require_loopback_or_trusted_dashboard_client)],)async def dashboard_settings():"""Serve the Headroom settings GUI."""diff --git a/tests/test_proxy_loopback_gating.py b/tests/test_proxy_loopback_gating.pyindex 7e48dabf6..5e9a1c9cb 100644--- a/tests/test_proxy_loopback_gating.py+++ b/tests/test_proxy_loopback_gating.py@@ -200,6 +200,104 @@ def test_ccr_retrieve_hash_route_blocks_valid_hash_for_non_loopback() -> None:reset_compression_store()+SETTINGS_GATED = [+ ("get", "/settings/schema"),+ ("get", "/settings"),+ ("get", "/dashboard/settings"),+]++[email protected]("method,path", SETTINGS_GATED)+def test_settings_non_loopback_gets_404_without_trusted_cidr(method: str, path: str) -> None:+ resp = TestClient(_make_app()).request(method, path)+ assert resp.status_code == 404, resp.text++[email protected]("method,path", SETTINGS_GATED)+def test_settings_loopback_caller_allowed(method: str, path: str) -> None:+ resp = _loopback_client().request(method, path)+ assert resp.status_code == 200, resp.text++[email protected]("method,path", SETTINGS_GATED)+def test_settings_trusted_gateway_dashboard_client_allowed(+ monkeypatch: pytest.MonkeyPatch, method: str, path: str+) -> None:+ """Settings routes must follow the same trust chain as /stats so the+ dashboard works behind a reverse-proxy/gateway (#2466)."""+ monkeypatch.setenv("HEADROOM_PROXY_TRUSTED_DASHBOARD_CLIENT_CIDRS", "100.90.0.5/32")+ client = TestClient(+ _make_app(),+ base_url="http://100.82.0.2:8787",+ client=("100.90.0.5", 12345),+ )+ resp = client.request(method, path)+ assert resp.status_code == 200, resp.text+++def test_settings_trusted_gateway_cidr_mismatch_still_404s(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setenv("HEADROOM_PROXY_TRUSTED_DASHBOARD_CLIENT_CIDRS", "100.90.0.5/32")+ client = TestClient(+ _make_app(),+ base_url="http://100.82.0.2:8787",+ client=("100.90.0.9", 12345),+ )+ assert client.get("/settings").status_code == 404+++ "path,body",+ [("/settings", {"values": {}}), ("/settings/apply", None)],+)+def test_settings_post_trusted_gateway_client_same_origin_allowed(+ monkeypatch: pytest.MonkeyPatch, path: str, body: dict | None+) -> None:+ """Regression for #2491 review: a trusted-gateway dashboard client's real+ same-origin browser POST (Origin matching this Host) must not be rejected+ by the loopback-only same-origin guard."""+ monkeypatch.setenv("HEADROOM_PROXY_TRUSTED_DASHBOARD_CLIENT_CIDRS", "100.90.0.5/32")+ client = TestClient(+ _make_app(),+ base_url="http://100.82.0.2:8787",+ client=("100.90.0.5", 12345),+ )+ resp = client.post(path, json=body, headers={"origin": "http://100.82.0.2:8787"})+ assert resp.status_code != 403, resp.text+++ "path,body",+ [("/settings", {"values": {}}), ("/settings/apply", None)],+)+def test_settings_post_trusted_gateway_client_mismatched_origin_rejected(+ monkeypatch: pytest.MonkeyPatch, path: str, body: dict | None+) -> None:+ """A trusted-gateway peer with a foreign Origin is still CSRF-rejected.++ The mismatched Origin also fails the first (loopback-or-trusted-client)+ gate's own same-origin check, so this surfaces as 404, not 403 -- either+ way the write must not go through."""+ monkeypatch.setenv("HEADROOM_PROXY_TRUSTED_DASHBOARD_CLIENT_CIDRS", "100.90.0.5/32")+ client = TestClient(+ _make_app(),+ base_url="http://100.82.0.2:8787",+ client=("100.90.0.5", 12345),+ )+ resp = client.post(path, json=body, headers={"origin": "http://attacker.example"})+ assert resp.status_code in (403, 404), resp.text+++def test_settings_post_loopback_null_origin_still_rejected() -> None:+ """Loopback callers keep the stricter loopback-only origin check: a+ sandboxed-iframe/file:// "null" Origin must still 403, unaffected by the+ trusted-dashboard-client carve-out."""+ client = _loopback_client()+ resp = client.post("/settings", json={"values": {}}, headers={"origin": "null"})+ assert resp.status_code == 403, resp.text++def test_dns_rebinding_host_header_rejected() -> None:# Loopback peer IP but an attacker-controlled Host header (the DNS-rebinding# shape) must still be rejected by the second gate.