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): redact upstream error detail and add opt-in /metrics loopback gate (#2589)

Laura Moneya05717f6ec2b
diff --git a/RUST_DEV.md b/RUST_DEV.md
index 2b5e76c19..9ec484b3e 100644
--- a/RUST_DEV.md
+++ b/RUST_DEV.md
@@ -84,6 +84,7 @@ curl -si http://127.0.0.1:8787/v1/models
| Flag | Env var | Default | Notes |
| --- | --- | --- | --- |
| `--listen` | `HEADROOM_PROXY_LISTEN` | `0.0.0.0:8787` | bind address |
+| `--metrics-require-loopback` | `HEADROOM_PROXY_METRICS_REQUIRE_LOOPBACK` | `false` | when set, `/metrics` is served only to loopback peers (403 otherwise); recommended on non-loopback binds |
| `--upstream` | `HEADROOM_PROXY_UPSTREAM` | (required) | base URL the proxy forwards to |
| `--upstream-timeout` | | `600s` | end-to-end request timeout (long for streams) |
| `--upstream-connect-timeout` | | `10s` | TCP/TLS connect timeout |
diff --git a/crates/headroom-proxy/src/config.rs b/crates/headroom-proxy/src/config.rs
index 0ea8703a2..5210dee55 100644
--- a/crates/headroom-proxy/src/config.rs
+++ b/crates/headroom-proxy/src/config.rs
@@ -276,6 +276,25 @@ pub struct CliArgs {
#[arg(long, env = "HEADROOM_PROXY_LISTEN", default_value = "0.0.0.0:8787")]
pub listen: SocketAddr,
+ /// Restrict the Prometheus `/metrics` scrape endpoint to loopback
+ /// clients. Default `false` to preserve existing behaviour (the
+ /// endpoint is reachable from wherever the proxy is bound). Set to
+ /// `true` — recommended whenever `--listen` binds a non-loopback
+ /// address — so `/metrics` (token counts, per-session cache-hit
+ /// rates, rate-limit gauges) is served only to `127.0.0.1` / `::1`
+ /// and returns 403 otherwise. This is defense-in-depth alongside
+ /// firewalling the path.
+ ///
+ /// Source priority: CLI flag → `HEADROOM_PROXY_METRICS_REQUIRE_LOOPBACK`
+ /// env var → default (`false`).
+ #[arg(
+ long = "metrics-require-loopback",
+ env = "HEADROOM_PROXY_METRICS_REQUIRE_LOOPBACK",
+ default_value_t = false,
+ action = clap::ArgAction::Set,
+ )]
+ pub metrics_require_loopback: bool,
+
/// Upstream base URL the proxy forwards to (e.g. http://127.0.0.1:8788).
/// REQUIRED — there is no default; we want operators to be explicit.
#[arg(long, env = "HEADROOM_PROXY_UPSTREAM")]
@@ -617,6 +636,9 @@ pub struct Config {
/// Runtime rollout state resolved from CLI/env.
pub rollout: RolloutSnapshot,
pub listen: SocketAddr,
+ /// Gate the Prometheus `/metrics` endpoint to loopback clients.
+ /// Default `false`; recommend `true` when `listen` is non-loopback.
+ pub metrics_require_loopback: bool,
pub upstream: Url,
pub upstream_timeout: Duration,
pub upstream_connect_timeout: Duration,
@@ -725,6 +747,7 @@ impl Config {
Self {
rollout: rollout.clone(),
listen: args.listen,
+ metrics_require_loopback: args.metrics_require_loopback,
upstream: args.upstream,
upstream_timeout: args.upstream_timeout,
upstream_connect_timeout: args.upstream_connect_timeout,
@@ -761,6 +784,11 @@ impl Config {
Self {
rollout: RolloutSnapshot::default(),
listen: "127.0.0.1:0".parse().unwrap(),
+ // Off by default in tests: the metrics integration tests
+ // scrape `/metrics` and assert 200, and oneshot-style tests
+ // carry no `ConnectInfo`. Tests that exercise the gate set
+ // this to `true` explicitly.
+ metrics_require_loopback: false,
upstream,
upstream_timeout: Duration::from_secs(60),
upstream_connect_timeout: Duration::from_secs(5),
diff --git a/crates/headroom-proxy/src/error.rs b/crates/headroom-proxy/src/error.rs
index 61cf8a2bc..91905bb82 100644
--- a/crates/headroom-proxy/src/error.rs
+++ b/crates/headroom-proxy/src/error.rs
@@ -55,30 +55,93 @@ impl From<crate::upstream_path::PathError> for ProxyError {
impl IntoResponse for ProxyError {
fn into_response(self) -> Response {
- let (status, msg) = match &self {
- ProxyError::Upstream(e) if e.is_timeout() => (
- StatusCode::GATEWAY_TIMEOUT,
- format!("upstream timeout: {e}"),
- ),
- ProxyError::Upstream(e) if e.is_connect() => (
- StatusCode::BAD_GATEWAY,
- format!("upstream connect error: {e}"),
+ // Full detail is for operators only. The `Display` impls above
+ // interpolate the source error (e.g. reqwest's chain), which for
+ // upstream failures can embed the upstream host/port and other
+ // internal wiring. We log that detail but return a generic,
+ // stable body to the client so an error response can't be used
+ // to fingerprint the upstream. The HTTP status still carries the
+ // actionable signal for well-behaved clients.
+ let detail = self.to_string();
+ let (status, client_msg): (StatusCode, &'static str) = match &self {
+ ProxyError::Upstream(e) if e.is_timeout() => {
+ (StatusCode::GATEWAY_TIMEOUT, "upstream timeout")
+ }
+ ProxyError::Upstream(e) if e.is_connect() => {
+ (StatusCode::BAD_GATEWAY, "upstream connection failed")
+ }
+ ProxyError::Upstream(_) => (StatusCode::BAD_GATEWAY, "upstream request failed"),
+ ProxyError::InvalidUpstream(_) => (StatusCode::BAD_GATEWAY, "upstream request failed"),
+ ProxyError::InvalidHeader(_) => (StatusCode::BAD_REQUEST, "invalid request"),
+ // PayloadTooLarge stays descriptive: it leaks no internal
+ // detail (the interpolated value is the client's own size vs
+ // the configured cap) and clients with retry-on-413 logic
+ // rely on recognizing it. See the PayloadTooLarge doc comment.
+ ProxyError::PayloadTooLarge(_) => (
+ StatusCode::PAYLOAD_TOO_LARGE,
+ "request body exceeds configured limit",
),
- ProxyError::Upstream(_) => (StatusCode::BAD_GATEWAY, self.to_string()),
- ProxyError::InvalidUpstream(_) => (StatusCode::BAD_GATEWAY, self.to_string()),
- ProxyError::InvalidHeader(_) => (StatusCode::BAD_REQUEST, self.to_string()),
- ProxyError::PayloadTooLarge(_) => (StatusCode::PAYLOAD_TOO_LARGE, self.to_string()),
- ProxyError::InvalidPath(_) => (StatusCode::BAD_REQUEST, self.to_string()),
- ProxyError::WebSocket(_) => (StatusCode::BAD_GATEWAY, self.to_string()),
- ProxyError::Io(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
+ ProxyError::InvalidPath(_) => (StatusCode::BAD_REQUEST, "invalid request"),
+ ProxyError::WebSocket(_) => (StatusCode::BAD_GATEWAY, "upstream request failed"),
+ ProxyError::Io(_) => (StatusCode::INTERNAL_SERVER_ERROR, "internal error"),
+
// CompressionStartup is a startup-time error, not a
// per-request one — but if it ever surfaces in the
// handler path, surface as 500 rather than panic.
ProxyError::CompressionStartup(_) => {
- (StatusCode::INTERNAL_SERVER_ERROR, self.to_string())
+ (StatusCode::INTERNAL_SERVER_ERROR, "internal error")
}
};
- tracing::warn!(error = %msg, "proxy error");
- (status, msg).into_response()
+ tracing::warn!(status = %status.as_u16(), error = %detail, "proxy error");
+ (status, client_msg).into_response()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use axum::body::to_bytes;
+
+ async fn status_and_body(err: ProxyError) -> (StatusCode, String) {
+ let resp = err.into_response();
+ let status = resp.status();
+ let bytes = to_bytes(resp.into_body(), 1024).await.unwrap();
+ (status, String::from_utf8_lossy(&bytes).to_string())
+ }
+
+ // The interpolated detail (which for real upstream errors can embed
+ // the upstream host/port) must NEVER reach the client body. Before
+ // the hardening these bodies were `self.to_string()` and DID contain
+ // the detail, so these assertions fail on old code.
+ #[tokio::test]
+ async fn invalid_header_detail_is_not_leaked_to_client() {
+ let (status, body) = status_and_body(ProxyError::InvalidHeader(
+ "upstream-secret-host:9443".into(),
+ ))
+ .await;
+ assert_eq!(status, StatusCode::BAD_REQUEST);
+ assert_eq!(body, "invalid request");
+ assert!(!body.contains("upstream-secret-host"));
+ }
+
+ #[tokio::test]
+ async fn invalid_upstream_detail_is_not_leaked_to_client() {
+ let (status, body) = status_and_body(ProxyError::InvalidUpstream(
+ "http://10.0.0.5:8788/internal".into(),
+ ))
+ .await;
+ assert_eq!(status, StatusCode::BAD_GATEWAY);
+ assert_eq!(body, "upstream request failed");
+ assert!(!body.contains("10.0.0.5"));
+ }
+
+ #[tokio::test]
+ async fn payload_too_large_stays_descriptive() {
+ // Deliberately kept descriptive: no internal detail, and
+ // retry-on-413 clients rely on recognizing it.
+ let (status, body) =
+ status_and_body(ProxyError::PayloadTooLarge("body exceeds cap".into())).await;
+ assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE);
+ assert_eq!(body, "request body exceeds configured limit");
}
}
diff --git a/crates/headroom-proxy/src/proxy.rs b/crates/headroom-proxy/src/proxy.rs
index 725f455fd..84414f8df 100644
--- a/crates/headroom-proxy/src/proxy.rs
+++ b/crates/headroom-proxy/src/proxy.rs
@@ -161,6 +161,36 @@ impl AppState {
}
}
+/// Per-route middleware for `/metrics`, attached only when
+/// `--metrics-require-loopback` is enabled. Rejects any scrape whose
+/// peer address is not loopback (`127.0.0.0/8` / `::1`) with
+/// `403 Forbidden`, keeping operational metrics off the wire on a
+/// non-loopback bind. Relies on the server being served with
+/// `ConnectInfo<SocketAddr>` (see `main.rs`); requests synthesised
+/// without connect info (e.g. `oneshot` in unit tests) would fail the
+/// `ConnectInfo` extractor — which is one reason the gate defaults off.
+async fn require_metrics_loopback(
+ ConnectInfo(peer): ConnectInfo<SocketAddr>,
+ req: Request<Body>,
+ next: axum::middleware::Next,
+) -> axum::response::Response {
+ if peer.ip().is_loopback() {
+ next.run(req).await
+ } else {
+ tracing::warn!(
+ event = "metrics_scrape_rejected_non_loopback",
+ peer = %peer,
+ "rejected /metrics scrape from non-loopback client \
+ (--metrics-require-loopback is enabled)"
+ );
+ (
+ StatusCode::FORBIDDEN,
+ "metrics endpoint restricted to loopback",
+ )
+ .into_response()
+ }
+}
+
/// Build the axum app. `/healthz` and `/healthz/upstream` are intercepted;
/// everything else hits the catch-all forwarder. WebSocket upgrades are
/// handled inside the catch-all handler when an `Upgrade: websocket` header
@@ -170,14 +200,6 @@ pub fn build_app(state: AppState) -> Router {
.route("/healthz", get(healthz))
.route("/healthz/upstream", get(healthz_upstream))
.route("/rollout/status", get(rollout_status))
- // PR-D3: Prometheus scrape endpoint. Renders the global
- // registry in text format. The handler is stateless — no
- // `AppState` needed — and idempotent across concurrent
- // scrapes (`prometheus`'s registry uses internal locking).
- // Mounted unconditionally because it has no dependencies on
- // any feature flag; an operator who doesn't want it scraped
- // simply firewalls the path.
- .route("/metrics", get(crate::observability::handle_metrics))
// PR-C2: explicit POST route for /v1/chat/completions. The
// handler buffers the body and re-injects it into
// `forward_http`, which runs the OpenAI live-zone gate
@@ -212,6 +234,27 @@ pub fn build_app(state: AppState) -> Router {
post(crate::vertex::handle_vertex_predict_dispatch),
);
+ // PR-D3: Prometheus scrape endpoint. Renders the global registry in
+ // text format. The handler is stateless — no `AppState` needed — and
+ // idempotent across concurrent scrapes (`prometheus`'s registry uses
+ // internal locking). Mounted as its own sub-router so the optional
+ // loopback gate can wrap ONLY this route. `/metrics` exposes
+ // operational detail (token counts, per-session cache-hit rates,
+ // rate-limit gauges); when `--metrics-require-loopback` is set,
+ // `require_metrics_loopback` rejects non-loopback peers with 403 —
+ // defense-in-depth alongside firewalling the path. Default off,
+ // preserving the previous "mounted unconditionally; operator
+ // firewalls the path" behaviour.
+ let metrics_router: Router<AppState> = {
+ let base = Router::new().route("/metrics", get(crate::observability::handle_metrics));
+ if state.config.metrics_require_loopback {
+ base.route_layer(axum::middleware::from_fn(require_metrics_loopback))
+ } else {
+ base
+ }
+ };
+ router = router.merge(metrics_router);
+
// PR-D1: native AWS Bedrock InvokeModel route. Mounts only when
// `enable_bedrock_native` is on (default). The handler runs the
// live-zone compressor over Anthropic-shape bodies, signs with
@@ -1697,6 +1740,47 @@ mod tests {
assert_eq!(out.as_str(), "http://up:8080/");
}
+ // `require_metrics_loopback` gate, driven directly via `oneshot`
+ // with a manually-injected `ConnectInfo` so BOTH branches are
+ // covered — including the non-loopback 403 path the real-server
+ // integration test can't reach from a loopback-only client.
+ fn metrics_gate_router() -> Router {
+ Router::new()
+ .route("/metrics", get(|| async { "metrics-body" }))
+ .route_layer(axum::middleware::from_fn(require_metrics_loopback))
+ }
+
+ async fn scrape_status_from(peer: &str) -> StatusCode {
+ use tower::util::ServiceExt;
+ let mut req = Request::builder()
+ .method("GET")
+ .uri("/metrics")
+ .body(Body::empty())
+ .unwrap();
+ let addr: SocketAddr = peer.parse().unwrap();
+ req.extensions_mut().insert(ConnectInfo(addr));
+ metrics_gate_router().oneshot(req).await.unwrap().status()
+ }
+
+ #[tokio::test]
+ async fn metrics_gate_allows_ipv4_loopback() {
+ assert_eq!(scrape_status_from("127.0.0.1:5555").await, StatusCode::OK);
+ }
+
+ #[tokio::test]
+ async fn metrics_gate_allows_ipv6_loopback() {
+ assert_eq!(scrape_status_from("[::1]:5555").await, StatusCode::OK);
+ }
+
+ #[tokio::test]
+ async fn metrics_gate_rejects_non_loopback_with_403() {
+ // 203.0.113.0/24 is TEST-NET-3 (RFC 5737) — never loopback.
+ assert_eq!(
+ scrape_status_from("203.0.113.7:5555").await,
+ StatusCode::FORBIDDEN
+ );
+ }
+
#[test]
fn url_build_refuses_to_escape_base_path() {
// `url::Url::set_path` would resolve this to `/tenant-b/v1/messages`.
diff --git a/crates/headroom-proxy/tests/integration_metrics_loopback.rs b/crates/headroom-proxy/tests/integration_metrics_loopback.rs
new file mode 100644
index 000000000..57d927441
--- /dev/null
+++ b/crates/headroom-proxy/tests/integration_metrics_loopback.rs
@@ -0,0 +1,59 @@
+//! `/metrics` loopback-gate integration coverage.
+//!
+//! `--metrics-require-loopback` (`Config::metrics_require_loopback`)
+//! restricts the Prometheus scrape endpoint to loopback peers as
+//! defense-in-depth for non-loopback binds. The shared harness serves
+//! the app over a real `127.0.0.1` listener with
+//! `ConnectInfo<SocketAddr>`, so a reqwest client here is itself a
+//! loopback peer: with the gate ON the scrape must STILL succeed (200),
+//! proving the middleware is wired correctly and never breaks the
+//! legitimate local-scrape path. The non-loopback rejection (403) path
+//! can't be driven from a loopback-only test client; it is enforced by
+//! the middleware's `peer.ip().is_loopback()` check in
+//! `crate::proxy::require_metrics_loopback`.
+
+mod common;
+
+use common::start_proxy_with;
+
+/// Gate ON: a loopback client is still allowed through, so the scrape
+/// returns 200 with the usual Prometheus descriptor lines.
+#[tokio::test]
+async fn metrics_gate_allows_loopback_scrape() {
+ // Upstream is never contacted — `/metrics` is served locally.
+ let proxy = start_proxy_with("http://127.0.0.1:1", |cfg| {
+ cfg.metrics_require_loopback = true;
+ })
+ .await;
+
+ let resp = reqwest::Client::new()
+ .get(format!("{}/metrics", proxy.url()))
+ .send()
+ .await
+ .expect("metrics scrape");
+
+ assert_eq!(
+ resp.status(),
+ 200,
+ "loopback client must still pass the metrics loopback gate"
+ );
+ let body = resp.text().await.unwrap();
+ assert!(
+ body.contains("# HELP") || body.contains("# TYPE"),
+ "scrape body should carry Prometheus metric descriptors"
+ );
+}
+
+/// Gate OFF (default): unchanged behaviour — the scrape is served.
+#[tokio::test]
+async fn metrics_default_open_serves_scrape() {
+ let proxy = start_proxy_with("http://127.0.0.1:1", |_| {}).await;
+
+ let resp = reqwest::Client::new()
+ .get(format!("{}/metrics", proxy.url()))
+ .send()
+ .await
+ .expect("metrics scrape");
+
+ assert_eq!(resp.status(), 200);
+}