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): guard feedback endpoints and add CSRF checks to loopback writes (#3060)
Tejas Chopraa6ab359a5d8d
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.pyindex c3a137b5d..990504597 100644--- a/headroom/proxy/server.py+++ b/headroom/proxy/server.py@@ -2438,6 +2438,35 @@ def _normalized_http_origin(value: str) -> tuple[str, str, int] | None:return scheme, parsed.hostname.lower(), port+#: Feedback-pattern keys built verbatim from agent query text. They are useful+#: in-process for compression decisions but must never reach an HTTP response —+#: same privacy contract the TOIN endpoints were brought under in #2926/#2927.+_FEEDBACK_QUERY_TEXT_KEYS = ("common_queries", "queried_fields")+++def _feedback_stats_without_query_text(stats: dict[str, Any]) -> dict[str, Any]:+ """Return ``stats`` with per-tool query text stripped from ``tool_patterns``.++ Copies only the levels it edits; the aggregate counters are shared with the+ caller's dict, which is fine because they are scalars.+ """++ patterns = stats.get("tool_patterns")+ if not isinstance(patterns, dict):+ return stats++ scrubbed: dict[str, Any] = {}+ for name, pattern in patterns.items():+ if isinstance(pattern, dict):+ scrubbed[name] = {+ key: value for key, value in pattern.items() if key not in _FEEDBACK_QUERY_TEXT_KEYS+ }+ else:+ scrubbed[name] = pattern++ return {**stats, "tool_patterns": scrubbed}++_is_known_websocket_callback_failure = is_known_websocket_callback_failure@@ -3506,7 +3535,10 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:payload["runtime"] = _runtime_payload()return JSONResponse(status_code=200, content=payload)- @app.post("/admin/runtime-env", dependencies=[Depends(_require_loopback)])+ @app.post(+ "/admin/runtime-env",+ dependencies=[Depends(_require_loopback), Depends(_require_same_origin)],+ )async def admin_runtime_env(request: Request):"""Hot-reload live env knobs (the output-shaper family, the ast-grepread threshold) without restarting the proxy.@@ -4447,7 +4479,10 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:payload["persistence"] = {**persistence, "error": None}return payload- @app.post("/stats/reset", dependencies=[Depends(_require_loopback)])+ @app.post(+ "/stats/reset",+ dependencies=[Depends(_require_loopback), Depends(_require_same_origin)],+ )async def stats_reset():"""Reset in-memory proxy stats for local test/debug isolation."""await proxy.metrics.reset_runtime()@@ -4584,7 +4619,10 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:report = tracker.get_report()return report.to_dict()- @app.post("/cache/clear", dependencies=[Depends(_require_loopback)])+ @app.post(+ "/cache/clear",+ dependencies=[Depends(_require_loopback), Depends(_require_same_origin)],+ )async def clear_cache():"""Clear the response cache.@@ -4600,7 +4638,10 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:return {"status": "cache disabled"}# CCR (Compress-Cache-Retrieve) endpoints- @app.post("/v1/retrieve", dependencies=[Depends(_require_loopback)])+ @app.post(+ "/v1/retrieve",+ dependencies=[Depends(_require_loopback), Depends(_require_same_origin)],+ )async def ccr_retrieve(request: Request):"""Retrieve original content from CCR compression cache.@@ -4669,21 +4710,25 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:],}- @app.get("/v1/feedback")+ @app.get("/v1/feedback", dependencies=[Depends(_require_loopback)])async def ccr_feedback():"""Get CCR feedback loop statistics and learned patterns.This endpoint exposes the feedback loop's learned patterns for monitoringand debugging. It shows:- Per-tool retrieval rates (high = compress less aggressively)- - Common search queries per tool- - Queried fields (suggest what to preserve)+ - Aggregate compression/retrieval counters per toolUse this to understand how well compression is working and whetherthe feedback loop is adjusting appropriately.++ Loopback-guarded and query-text free for the same reason as the+ telemetry and TOIN endpoints (#2926/#2927): ``common_queries`` and+ ``queried_fields`` are built verbatim from agent search queries, so+ they stay out of the response even on the guarded path."""feedback = get_compression_feedback()- stats = feedback.get_stats()+ stats = _feedback_stats_without_query_text(feedback.get_stats())return {"feedback": stats,"hints_example": {@@ -4702,12 +4747,15 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:},}- @app.get("/v1/feedback/{tool_name}")+ @app.get("/v1/feedback/{tool_name}", dependencies=[Depends(_require_loopback)])async def ccr_feedback_for_tool(tool_name: str):"""Get compression hints for a specific tool.Returns feedback-based hints that would be used for compressingthis tool's output.++ Loopback-guarded, and the pattern block excludes ``common_queries``+ and ``queried_fields`` — both are raw agent query text (#2926/#2927)."""feedback = get_compression_feedback()hints = feedback.get_compression_hints(tool_name)@@ -4730,8 +4778,6 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:"retrieval_rate": patterns.retrieval_rate if patterns else 0.0,"full_retrieval_rate": patterns.full_retrieval_rate if patterns else 0.0,"search_rate": patterns.search_rate if patterns else 0.0,- "common_queries": list(patterns.common_queries.keys())[:10] if patterns else [],- "queried_fields": list(patterns.queried_fields.keys())[:10] if patterns else [],}if patternselse None,@@ -4777,7 +4823,10 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:telemetry = get_telemetry_collector()return telemetry.export_stats()- @app.post("/v1/telemetry/import", dependencies=[Depends(_require_loopback)])+ @app.post(+ "/v1/telemetry/import",+ dependencies=[Depends(_require_loopback), Depends(_require_same_origin)],+ )async def telemetry_import(request: Request):"""Import telemetry data from another source.diff --git a/tests/test_proxy_loopback_gating.py b/tests/test_proxy_loopback_gating.pyindex 5e9a1c9cb..49381d1ef 100644--- a/tests/test_proxy_loopback_gating.py+++ b/tests/test_proxy_loopback_gating.py@@ -15,6 +15,7 @@ from fastapi import FastAPIfrom fastapi.testclient import TestClientfrom headroom.cache.backends import InMemoryBackend+from headroom.cache.compression_feedback import CompressionHintsfrom headroom.cache.compression_store import get_compression_store, reset_compression_storefrom headroom.proxy.loopback_guard import is_ip_literal_host_headerfrom headroom.proxy.server import ProxyConfig, create_app@@ -30,6 +31,11 @@ GATED = [("get", "/v1/toin/stats"),("get", "/v1/toin/patterns"),("get", "/v1/toin/pattern/example"),+ # #2927 guarded the eight telemetry/TOIN routes the issue enumerated but+ # left these two siblings open, and their payload carries the same raw+ # agent query text (``common_queries``, built from ``event.query``).+ ("get", "/v1/feedback"),+ ("get", "/v1/feedback/example"),]@@ -119,6 +125,135 @@ def test_toin_pattern_detail_whitelists_learned_payload(monkeypatch: pytest.Monk}+# Mutating routes reachable from loopback. `require_loopback` cannot stop a+# remote page from POSTing to a known 127.0.0.1 URL: a "simple" cross-origin+# request (Content-Type: text/plain carrying JSON) skips preflight, and the+# browser still sends the real loopback Host header. Only `Origin` betrays the+# attacker, and only `require_same_origin` inspects it.+CSRF_GUARDED = [+ "/stats/reset",+ "/cache/clear",+ "/v1/retrieve",+ "/v1/telemetry/import",+ "/admin/runtime-env",+]++[email protected]("path", CSRF_GUARDED)+def test_cross_origin_post_rejected(path: str) -> None:+ resp = _loopback_client().post(+ path,+ headers={"Origin": "https://attacker.example", "Content-Type": "text/plain"},+ content="{}",+ )+ assert resp.status_code == 403, resp.text++[email protected]("path", CSRF_GUARDED)+def test_sandboxed_null_origin_post_rejected(path: str) -> None:+ # A sandboxed iframe or file:// page sends the opaque literal "null".+ resp = _loopback_client().post(+ path,+ headers={"Origin": "null", "Content-Type": "text/plain"},+ content="{}",+ )+ assert resp.status_code == 403, resp.text++[email protected]("path", CSRF_GUARDED)+def test_loopback_origin_post_allowed(path: str) -> None:+ # The local dashboard is same-origin on loopback and must keep working.+ resp = _loopback_client().post(+ path,+ headers={"Origin": "http://127.0.0.1"},+ json={},+ )+ assert resp.status_code != 403, resp.text++[email protected]("path", CSRF_GUARDED)+def test_originless_post_allowed(path: str) -> None:+ # CLI tools and the TypeScript SDK send no Origin header at all; the guard+ # must pass them through or it breaks every non-browser client.+ resp = _loopback_client().post(path, json={})+ assert resp.status_code != 403, resp.text+++def _feedback_with_query_text():+ """A feedback singleton whose patterns carry raw agent query text."""++ class FakePattern:+ total_compressions = 8+ total_retrievals = 2+ retrieval_rate = 0.25+ full_retrieval_rate = 0.1+ search_rate = 0.5+ common_queries = {"find the customer api key rotation runbook": 3}+ queried_fields = {"internal_field_name": 2}++ class FakeFeedback:+ def get_stats(self):+ return {+ "total_compressions": 8,+ "total_retrievals": 2,+ "global_retrieval_rate": 0.25,+ "tools_tracked": 1,+ "tool_patterns": {+ "Grep": {+ "compressions": 8,+ "retrievals": 2,+ "retrieval_rate": 0.25,+ "full_rate": 0.1,+ "search_rate": 0.5,+ "common_queries": ["find the customer api key rotation runbook"],+ "queried_fields": ["internal_field_name"],+ }+ },+ }++ def get_compression_hints(self, tool_name):+ # The real implementation is annotated ``-> CompressionHints`` and+ # always returns one, so the double must too.+ return CompressionHints()++ def get_all_patterns(self):+ return {"Grep": FakePattern()}++ return FakeFeedback()+++def test_feedback_stats_exclude_agent_query_text(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(+ "headroom.proxy.server.get_compression_feedback",+ _feedback_with_query_text,+ )+ response = _loopback_client().get("/v1/feedback")++ assert response.status_code == 200+ pattern = response.json()["feedback"]["tool_patterns"]["Grep"]+ assert "common_queries" not in pattern+ assert "queried_fields" not in pattern+ # The aggregate counters the endpoint exists to expose still survive.+ assert pattern["retrieval_rate"] == 0.25+ assert "customer api key rotation" not in response.text+++def test_feedback_tool_detail_excludes_agent_query_text(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(+ "headroom.proxy.server.get_compression_feedback",+ _feedback_with_query_text,+ )+ response = _loopback_client().get("/v1/feedback/Grep")++ assert response.status_code == 200+ pattern = response.json()["pattern"]+ assert "common_queries" not in pattern+ assert "queried_fields" not in pattern+ assert pattern["retrieval_rate"] == 0.25+ assert "customer api key rotation" not in response.text+ assert "internal_field_name" not in response.text++# CCR data endpoints — cached session content, gated to 404 off-loopback (#1227).def test_stats_lifetime_route_uses_dashboard_metadata_access_policy(monkeypatch: pytest.MonkeyPatch,