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): gate CCR retrieve/compress endpoints to loopback (#1338)

inixacafb2d0f668
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.py
index 6b9ef06ab..3c19fa95f 100644
--- a/headroom/proxy/server.py
+++ b/headroom/proxy/server.py
@@ -3278,7 +3278,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
return {"status": "cache disabled"}
# CCR (Compress-Cache-Retrieve) endpoints
- @app.post("/v1/retrieve")
+ @app.post("/v1/retrieve", dependencies=[Depends(_require_loopback)])
async def ccr_retrieve(request: Request):
"""Retrieve original content from CCR compression cache.
@@ -3341,7 +3341,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
),
)
- @app.get("/v1/retrieve/stats")
+ @app.get("/v1/retrieve/stats", dependencies=[Depends(_require_loopback)])
async def ccr_stats():
"""Get CCR compression store statistics."""
store = get_compression_store()
@@ -3620,7 +3620,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
status_code=404, detail=f"No TOIN pattern found with hash starting with: {hash_prefix}"
)
- @app.get("/v1/retrieve/{hash_key}")
+ @app.get("/v1/retrieve/{hash_key}", dependencies=[Depends(_require_loopback)])
async def ccr_retrieve_get(hash_key: str, query: str | None = None):
"""GET version of CCR retrieve for easier testing."""
store = get_compression_store()
@@ -3660,7 +3660,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
)
# CCR Tool Call Handler - for agent frameworks to call when LLM uses headroom_retrieve
- @app.post("/v1/retrieve/tool_call")
+ @app.post("/v1/retrieve/tool_call", dependencies=[Depends(_require_loopback)])
async def ccr_handle_tool_call(request: Request):
"""Handle a CCR tool call from an LLM response.
@@ -3777,7 +3777,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
}
# Compression-only endpoint (for TypeScript SDK and other HTTP clients)
- @app.post("/v1/compress")
+ @app.post("/v1/compress", dependencies=[Depends(_require_loopback)])
async def compress_messages(request: Request):
return await proxy.handle_compress(request)
diff --git a/tests/test_ccr_row_drop_store_bridge.py b/tests/test_ccr_row_drop_store_bridge.py
index 7c61a1494..9866ddceb 100644
--- a/tests/test_ccr_row_drop_store_bridge.py
+++ b/tests/test_ccr_row_drop_store_bridge.py
@@ -403,7 +403,7 @@ def test_v1_compress_then_v1_retrieve_resolves_marker_hash() -> None:
}
try:
- with TestClient(app) as client:
+ with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as client:
resp = client.post("/v1/compress", json=req)
assert resp.status_code == 200, resp.text
body = resp.json()
@@ -481,7 +481,7 @@ def test_v1_retrieve_unknown_hash_still_404() -> None:
app = create_app(config)
try:
- with TestClient(app) as client:
+ with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as client:
resp = client.post("/v1/retrieve", json={"hash": "deadbeef0000"})
assert resp.status_code == 404
finally:
diff --git a/tests/test_proxy_ccr.py b/tests/test_proxy_ccr.py
index a553cec52..d9892c148 100644
--- a/tests/test_proxy_ccr.py
+++ b/tests/test_proxy_ccr.py
@@ -28,7 +28,8 @@ def client():
cost_tracking_enabled=False,
)
app = create_app(config)
- with TestClient(app) as client:
+ # CCR endpoints are loopback-gated (#1227).
+ with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as client:
yield client
reset_compression_store()
@@ -453,7 +454,8 @@ class TestEndToEndTOINIntegration:
cost_tracking_enabled=False,
)
app = create_app(config)
- with TestClient(app) as client:
+ # CCR endpoints are loopback-gated (#1227).
+ with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as client:
yield client
reset_compression_store()
diff --git a/tests/test_proxy_compress_endpoint.py b/tests/test_proxy_compress_endpoint.py
index 780025d54..daa20e0d9 100644
--- a/tests/test_proxy_compress_endpoint.py
+++ b/tests/test_proxy_compress_endpoint.py
@@ -26,7 +26,8 @@ def client():
cost_tracking_enabled=False,
)
app = create_app(config)
- with TestClient(app) as c:
+ # /v1/compress is loopback-gated (#1227).
+ with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as c:
yield c
@@ -40,7 +41,8 @@ def client_no_optimize():
cost_tracking_enabled=False,
)
app = create_app(config)
- with TestClient(app) as c:
+ # /v1/compress is loopback-gated (#1227).
+ with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as c:
yield c
diff --git a/tests/test_proxy_loopback_gating.py b/tests/test_proxy_loopback_gating.py
index b01e7e1e0..a3781c546 100644
--- a/tests/test_proxy_loopback_gating.py
+++ b/tests/test_proxy_loopback_gating.py
@@ -60,6 +60,22 @@ def test_loopback_caller_allowed(method: str, path: str) -> None:
assert resp.status_code == 200, resp.text
+# CCR data endpoints — cached session content, gated to 404 off-loopback (#1227).
+CCR_GATED = [
+ ("post", "/v1/retrieve"),
+ ("get", "/v1/retrieve/stats"),
+ ("get", "/v1/retrieve/somehash"),
+ ("post", "/v1/retrieve/tool_call"),
+ ("post", "/v1/compress"),
+]
+
+
[email protected]("method,path", CCR_GATED)
+def test_ccr_non_loopback_gets_404(method: str, path: str) -> None:
+ resp = TestClient(_make_app()).request(method, path, json={})
+ assert resp.status_code == 404, 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.