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): admit container host gateway on compress routes (#3715)

Prithvi raja8ae0a6fd5d6
diff --git a/docs/content/docs/openai-sdk.mdx b/docs/content/docs/openai-sdk.mdx
index 7a3bbefc3..931d4f25c 100644
--- a/docs/content/docs/openai-sdk.mdx
+++ b/docs/content/docs/openai-sdk.mdx
@@ -46,7 +46,7 @@ That's it. Every call to `client.chat.completions.create()` compresses the messa
3. Replaces the original messages with the compressed result
4. Forwards the request to OpenAI as normal
-The SDK talks to a **local** proxy, which is why no extra configuration is needed: `/v1/compress` is loopback-only by default. If you move the proxy to another host, set `HEADROOM_COMPRESS_ALLOW_REMOTE=1` on it or requests come back `404`.
+The SDK talks to a **local** proxy, which is why no extra configuration is needed: `/v1/compress` is loopback-only by default (in container environments, host gateway traffic is admitted automatically). If you move the proxy to another host, set `HEADROOM_COMPRESS_ALLOW_REMOTE=1` on it or requests come back `404`.
All other client methods are untouched:
diff --git a/docs/content/docs/proxy.mdx b/docs/content/docs/proxy.mdx
index 352e5f280..7804f28d2 100644
--- a/docs/content/docs/proxy.mdx
+++ b/docs/content/docs/proxy.mdx
@@ -332,7 +332,7 @@ with a privileged job.
| Flag / env | Default | Effect |
|---|---|---|
| `HEADROOM_PROXY_TOKEN` | none | Require a bearer token (`X-Headroom-Proxy-Token`) from non-loopback callers. |
-| `HEADROOM_COMPRESS_ALLOW_REMOTE` | `false` | Allow non-loopback callers to reach [`POST /v1/compress`](#post-v1compress). Required to run Headroom as a gateway/sidecar; without it remote callers get `404`. |
+| `HEADROOM_COMPRESS_ALLOW_REMOTE` | `false` | Allow non-loopback callers to reach [`POST /v1/compress`](#post-v1compress) and `POST /v1/usage`. Required to run Headroom as a remote gateway/sidecar; without it remote callers get `404`. |
| `--offline` / `HEADROOM_OFFLINE` | `false` | Air-gap mode: hard-disable **all** egress (telemetry, update checks, license reporting, model downloads). |
| `--stateless` / `HEADROOM_STATELESS` | `false` | Keep all state in memory; no filesystem writes (disables logs, memory, TOIN). |
| `HEADROOM_STRIP_INTERNAL_HEADERS` | `enabled` | Strip internal `x-headroom-*` headers before forwarding upstream. |
@@ -460,7 +460,7 @@ If `HEADROOM_KOMPRESS_ENDPOINT` is set, Kompress inference is offloaded over HTT
<Callout type="warning" title="Loopback-only by default">
This route is restricted to loopback callers and answers everyone else with **`404`**, not `403` — deliberately, so it stays invisible to external scanners. A gateway calling it from another host or pod therefore sees what looks like a missing route.
-Both the client IP and the inbound `Host:` header must name loopback. To allow remote callers, set `HEADROOM_COMPRESS_ALLOW_REMOTE=1`. `HEADROOM_PROXY_TOKEN` still applies if set.
+Both the client IP and the inbound `Host:` header must name loopback. In container environments (Docker/Podman), requests forwarded from the container host gateway are also admitted under loopback `Host:` headers. To allow arbitrary remote callers, set `HEADROOM_COMPRESS_ALLOW_REMOTE=1`. `HEADROOM_PROXY_TOKEN` still applies if set. The same policy applies to [`POST /v1/usage`](#relaying-provider-usage-post-v1usage).
</Callout>
#### Message format
diff --git a/headroom/proxy/loopback_guard.py b/headroom/proxy/loopback_guard.py
index 0add8b2a4..d7283aaec 100644
--- a/headroom/proxy/loopback_guard.py
+++ b/headroom/proxy/loopback_guard.py
@@ -40,6 +40,9 @@ CSRF / DNS-rebinding guidance and the standard Starlette
from __future__ import annotations
import ipaddress
+import os
+import socket
+import struct
try:
from fastapi import HTTPException, Request
@@ -50,10 +53,14 @@ except ImportError: # pragma: no cover - fastapi is a hard dep in practice
__all__ = [
"LOOPBACK_HOSTS",
+ "get_container_host_gateway",
+ "is_container_environment",
+ "is_container_host_gateway",
"is_ip_literal_host_header",
"is_loopback_host",
"is_loopback_host_header",
"require_loopback",
+ "require_loopback_or_container_gateway",
"require_same_origin",
]
@@ -216,6 +223,125 @@ def require_loopback(request: Request) -> None: # type: ignore[valid-type]
raise HTTPException(status_code=404)
+def is_container_environment() -> bool:
+ """Return True if running inside a container or containerized deployment."""
+ if os.environ.get("HEADROOM_CONTAINER_HOST_GATEWAY"):
+ return True
+ if os.environ.get("HEADROOM_DEPLOYMENT_RUNTIME") in ("docker", "podman", "container"):
+ return True
+ if os.environ.get("HEADROOM_DEPLOYMENT_PRESET") == "persistent-docker":
+ return True
+ if os.path.exists("/.dockerenv") or os.path.exists("/run/.containerenv"):
+ return True
+ try:
+ with open("/proc/1/cgroup", encoding="utf-8", errors="ignore") as f:
+ content = f.read()
+ if any(marker in content for marker in ("docker", "containerd", "kubepods", "libpod")):
+ return True
+ except OSError:
+ pass
+ return False
+
+
+def _read_linux_default_gateway() -> str | None:
+ """Read the default IPv4 gateway address from /proc/net/route."""
+ try:
+ with open("/proc/net/route", encoding="ascii") as f:
+ for line in f:
+ fields = line.strip().split()
+ if len(fields) >= 3 and fields[1] == "00000000":
+ gw_hex = fields[2]
+ if len(gw_hex) == 8:
+ gw_int = int(gw_hex, 16)
+ if gw_int != 0:
+ return socket.inet_ntoa(struct.pack("<L", gw_int))
+ except (OSError, ValueError):
+ pass
+ return None
+
+
+def get_container_host_gateway() -> str | None:
+ """Return the container host gateway IP if running in a container.
+
+ Checks ``HEADROOM_CONTAINER_HOST_GATEWAY`` first, and falls back to
+ resolving the default gateway from ``/proc/net/route`` when running
+ inside a container environment. Returns None when not in a container or
+ if no gateway can be determined.
+ """
+ env_gw = os.environ.get("HEADROOM_CONTAINER_HOST_GATEWAY")
+ if env_gw:
+ env_gw = env_gw.strip()
+ if env_gw:
+ return env_gw
+ if not is_container_environment():
+ return None
+ return _read_linux_default_gateway()
+
+
+def _normalize_ip(host: str) -> ipaddress.IPv4Address | ipaddress.IPv6Address | None:
+ try:
+ ip = ipaddress.ip_address(host)
+ except ValueError:
+ return None
+ if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped is not None:
+ return ip.ipv4_mapped
+ return ip
+
+
+def is_container_host_gateway(host: str | None) -> bool:
+ """Return True if ``host`` matches the container host gateway IP.
+
+ Always returns False if not in a container environment or if ``host``
+ is empty. Correctly handles IPv4-mapped IPv6 addresses (e.g.
+ ``::ffff:172.17.0.1``).
+ """
+ if not host or not is_container_environment():
+ return False
+ gateway = get_container_host_gateway()
+ if not gateway:
+ return False
+ parsed_host = _normalize_ip(host)
+ parsed_gw = _normalize_ip(gateway)
+ if parsed_host is not None and parsed_gw is not None:
+ return parsed_host == parsed_gw
+ return host.strip() == gateway.strip()
+
+
+def require_loopback_or_container_gateway(request: Request) -> None: # type: ignore[valid-type]
+ """FastAPI dependency: allow loopback callers or container host gateway.
+
+ Used by routes like ``/v1/compress`` and ``/v1/usage``. When running inside
+ a container environment (Docker / Podman), incoming host traffic forwarded
+ over the container bridge arrives with the host gateway IP (e.g. 172.17.0.1)
+ rather than a loopback interface.
+
+ Enforces two gates:
+ 1. Client address must be either loopback (:func:`is_loopback_host`) or
+ the container host default gateway (:func:`is_container_host_gateway`).
+ Arbitrary peer containers on the same bridge network are rejected.
+ 2. The inbound ``Host:`` header must name a loopback host
+ (:func:`is_loopback_host_header`). This blocks DNS-rebinding attacks and
+ rejects callers addressing the container's bridge IP or hostname directly.
+ """
+ if HTTPException is None: # pragma: no cover - defensive
+ raise RuntimeError("FastAPI is required for the loopback guard")
+
+ client = getattr(request, "client", None)
+ host = getattr(client, "host", None) if client is not None else None
+ if not (is_loopback_host(host) or is_container_host_gateway(host)):
+ raise HTTPException(status_code=404)
+
+ headers = getattr(request, "headers", None)
+ if headers is None:
+ return
+ try:
+ host_header = headers.get("host")
+ except AttributeError:
+ host_header = None
+ if not is_loopback_host_header(host_header):
+ raise HTTPException(status_code=404)
+
+
def require_same_origin(request: Request) -> None: # type: ignore[valid-type]
"""FastAPI dependency: reject cross-origin browser requests on mutating routes.
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.py
index 7d9d5117b..b14d3048b 100644
--- a/headroom/proxy/server.py
+++ b/headroom/proxy/server.py
@@ -3844,8 +3844,15 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
from headroom.proxy.debug_introspection import (
collect_tasks as _collect_tasks,
)
- from headroom.proxy.loopback_guard import require_loopback as _require_loopback
- from headroom.proxy.loopback_guard import require_same_origin as _require_same_origin
+ from headroom.proxy.loopback_guard import (
+ require_loopback as _require_loopback,
+ )
+ from headroom.proxy.loopback_guard import (
+ require_loopback_or_container_gateway as _require_loopback_or_container_gateway,
+ )
+ from 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.
@@ -5613,7 +5620,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
_compress_dependencies = (
[]
if _get_env_bool("HEADROOM_COMPRESS_ALLOW_REMOTE", False)
- else [Depends(_require_loopback)]
+ else [Depends(_require_loopback_or_container_gateway)]
)
@app.post("/v1/compress", dependencies=_compress_dependencies)
diff --git a/tests/test_gateway_sidecar_ports.py b/tests/test_gateway_sidecar_ports.py
index 9267ef8e5..54c469fd0 100644
--- a/tests/test_gateway_sidecar_ports.py
+++ b/tests/test_gateway_sidecar_ports.py
@@ -132,3 +132,98 @@ def test_compress_allows_non_loopback_with_flag(monkeypatch):
client = TestClient(_fast_app())
resp = client.post("/v1/compress", json=_BODY)
assert resp.status_code == 200, resp.text
+
+
+def test_compress_allows_container_gateway_with_loopback_host(monkeypatch):
+ monkeypatch.delenv("HEADROOM_COMPRESS_ALLOW_REMOTE", raising=False)
+ monkeypatch.setenv("HEADROOM_CONTAINER_HOST_GATEWAY", "172.17.0.1")
+ client = TestClient(
+ _fast_app(),
+ base_url="http://127.0.0.1:8787",
+ client=("172.17.0.1", 12345),
+ )
+ resp = client.post("/v1/compress", json=_BODY)
+ assert resp.status_code == 200, resp.text
+
+
+def test_compress_blocks_peer_container_on_bridge_network(monkeypatch):
+ monkeypatch.delenv("HEADROOM_COMPRESS_ALLOW_REMOTE", raising=False)
+ monkeypatch.setenv("HEADROOM_CONTAINER_HOST_GATEWAY", "172.17.0.1")
+ # Peer container connecting directly with its own container IP
+ client = TestClient(
+ _fast_app(),
+ base_url="http://172.17.0.2:8787",
+ client=("172.17.0.5", 12345),
+ )
+ assert client.post("/v1/compress", json=_BODY).status_code == 404
+
+ # Peer container trying to spoof a loopback Host header
+ client_spoofed_host = TestClient(
+ _fast_app(),
+ base_url="http://127.0.0.1:8787",
+ client=("172.17.0.5", 12345),
+ )
+ assert client_spoofed_host.post("/v1/compress", json=_BODY).status_code == 404
+
+
+def test_compress_blocks_non_loopback_host_header_from_gateway(monkeypatch):
+ monkeypatch.delenv("HEADROOM_COMPRESS_ALLOW_REMOTE", raising=False)
+ monkeypatch.setenv("HEADROOM_CONTAINER_HOST_GATEWAY", "172.17.0.1")
+ # Gateway IP caller but with external domain Host header (e.g. DNS rebinding)
+ client = TestClient(
+ _fast_app(),
+ base_url="http://attacker.com",
+ client=("172.17.0.1", 12345),
+ )
+ assert client.post("/v1/compress", json=_BODY).status_code == 404
+
+
+def test_loopback_guard_container_gateway_helpers(monkeypatch, tmp_path):
+ from headroom.proxy.loopback_guard import (
+ get_container_host_gateway,
+ is_container_environment,
+ is_container_host_gateway,
+ )
+
+ # Clean state - non-container environment
+ monkeypatch.delenv("HEADROOM_CONTAINER_HOST_GATEWAY", raising=False)
+ monkeypatch.delenv("HEADROOM_DEPLOYMENT_RUNTIME", raising=False)
+ monkeypatch.delenv("HEADROOM_DEPLOYMENT_PRESET", raising=False)
+ monkeypatch.setattr("os.path.exists", lambda path: False)
+ assert not is_container_environment()
+ assert get_container_host_gateway() is None
+ assert not is_container_host_gateway("172.17.0.1")
+
+ # Explicit override
+ monkeypatch.setenv("HEADROOM_CONTAINER_HOST_GATEWAY", "172.17.0.1")
+ assert is_container_environment()
+ assert get_container_host_gateway() == "172.17.0.1"
+ assert is_container_host_gateway("172.17.0.1")
+ assert is_container_host_gateway("::ffff:172.17.0.1")
+ assert not is_container_host_gateway("172.17.0.5")
+
+ # Linux route file parsing
+ monkeypatch.delenv("HEADROOM_CONTAINER_HOST_GATEWAY", raising=False)
+ monkeypatch.setenv("HEADROOM_DEPLOYMENT_RUNTIME", "docker")
+ assert is_container_environment()
+
+ route_content = (
+ "Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT\n"
+ "eth0\t00000000\t0111A8C0\t0003\t0\t0\t0\t00000000\t0\t0\t0\n"
+ )
+ route_file = tmp_path / "route"
+ route_file.write_text(route_content, encoding="ascii")
+
+ import builtins
+
+ real_open = builtins.open
+
+ def fake_open(file, *args, **kwargs):
+ if file == "/proc/net/route":
+ return real_open(route_file, *args, **kwargs)
+ return real_open(file, *args, **kwargs)
+
+ monkeypatch.setattr(builtins, "open", fake_open)
+ assert get_container_host_gateway() == "192.168.17.1"
+ assert is_container_host_gateway("192.168.17.1")
+ assert not is_container_host_gateway("192.168.17.2")