From beeb23df749ad34b0c0852db5d52944e1851be57 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 15:00:50 -0800 Subject: [PATCH] fix(relay): backport HTTP response and ops-listener hardening Backport of #7210 to release. - /fetch serves bytes as application/octet-stream; /fetch and /announced send X-Content-Type-Options: nosniff and Cache-Control: private, no-store, refusals and extraction errors included. - The internal (ops) listener refuses any request carrying Origin, and any request whose Host or URI authority is not the accepted socket's local IP and port (localhost only on loopback), before dispatch so merged routes inherit it. Co-Authored-By: Claude Opus 5.5 --- doc/bin/relay/http.md | 10 ++ rs/moq-relay/Cargo.toml | 2 +- rs/moq-relay/src/internal.rs | 252 ++++++++++++++++++++++++++++++++++- rs/moq-relay/src/web.rs | 117 +++++++++++++++- 4 files changed, 369 insertions(+), 12 deletions(-) diff --git a/doc/bin/relay/http.md b/doc/bin/relay/http.md index 72be1bbd1ee..978a3987bfc 100644 --- a/doc/bin/relay/http.md +++ b/doc/bin/relay/http.md @@ -26,6 +26,10 @@ The announcement listing names each announced route by the prefix it covers; by convention a publisher announces each broadcast's exact path, so the list reads as broadcast names. +Both `/fetch` and `/announced` return private, non-cacheable responses. +`/fetch` serves binary data, so save it to a file or parse it as the track's +format. + A relay configured with more than one certificate has no single fingerprint to publish, and this endpoint answers for the first. The others are reachable over `https://`, which selects a certificate by SNI at the handshake. @@ -45,6 +49,12 @@ production. listen = "127.0.0.1:9101" ``` +The listener is unauthenticated and must stay on loopback or a trusted private +network. Requests carrying an `Origin` header are refused. Ops tools must use +the listener's literal IP and port, or `localhost` on loopback; other hostnames +are refused to prevent DNS rebinding. A proxy must preserve that authority +rather than forward the caller's `Host`. + ### GET /metrics Prometheus text: bytes, frames, groups, announces, subscriptions, viewers, and diff --git a/rs/moq-relay/Cargo.toml b/rs/moq-relay/Cargo.toml index 8f5ce8f8983..f35b3989054 100644 --- a/rs/moq-relay/Cargo.toml +++ b/rs/moq-relay/Cargo.toml @@ -93,7 +93,7 @@ sd-notify = { workspace = true } [dev-dependencies] aws-lc-rs = { workspace = true } criterion = { workspace = true } -# The http_auth bench drives the web listener over HTTP/2. +# An HTTP/2 client for the http_auth bench and the internal listener's tests. h2 = "0.4" hex = { workspace = true } moq-auth = { path = "../moq-auth", features = ["client", "serve"] } diff --git a/rs/moq-relay/src/internal.rs b/rs/moq-relay/src/internal.rs index b67fa08115b..543b2196874 100644 --- a/rs/moq-relay/src/internal.rs +++ b/rs/moq-relay/src/internal.rs @@ -25,7 +25,10 @@ //! //! Everything here is unauthenticated, so bind it only to a trusted plane - //! loopback for a co-located scraper/agent, or a private overlay address; see -//! [`Config::listen`]. Unset by default (opt-in). A push can only cause +//! [`Config::listen`]. Browser requests carrying `Origin` are refused. The +//! request authority must name the accepted socket's local IP and port, or +//! `localhost` for a loopback connection. DNS names and forwarded hosts are not +//! trusted. Unset by default (opt-in). A push can only cause //! re-checks, never close a session on its own, so it belongs on this plane. use std::net; @@ -38,7 +41,7 @@ use axum::{ response::{IntoResponse, Response}, routing::{get, post}, }; -use axum_server::accept::DefaultAcceptor; +use axum_server::accept::Accept; /// One io_uring worker's counters, as [`with_uring`](Internal::with_uring) takes /// them. @@ -224,7 +227,8 @@ impl Internal { /// [`serve`](Self::serve). /// /// Anything merged in inherits this listener's "unauthenticated, - /// trusted-plane-only" contract; see the module docs. + /// trusted-plane-only" contract; see the module docs. [`serve`](Self::serve) + /// applies the Origin and authority policy to the completed router. pub fn routes(&self) -> Router { Router::new() .route("/metrics", get(serve_metrics)) @@ -246,7 +250,9 @@ impl Internal { /// /// The mirror of [`Web::serve`](crate::web::Web::serve): the caller builds `app` /// from [`routes`](Self::routes) plus whatever extra ops routes it merged in, - /// and this owns the listener. An embedder that binds the socket itself + /// and this owns the listener. Every route refuses `Origin` and requires the + /// request authority to match the connection's local IP and port (`localhost` + /// is also accepted on loopback). An embedder that binds the socket itself /// instead would fork both the socket options and the disabled-listener /// contract below. /// @@ -262,9 +268,10 @@ impl Internal { // No blanket "…server failed" context here: the caller (main.rs) adds // that single top-level layer, matching `Web::serve` / `Cluster::run`. - // No accept-time work: the ops router never hands a connection to qmux, so - // capturing a descriptor per health check would spend one for nothing. - crate::listener::server(listener, health, DefaultAcceptor::new())? + // The acceptor only reads the local address: the ops router never hands a + // connection to qmux, so capturing a descriptor per health check would + // spend one for nothing. + crate::listener::server(listener, health, InternalAcceptor)? .serve(app.into_make_service()) .await?; Ok(()) @@ -279,6 +286,95 @@ impl Internal { } } +/// Apply the policy after an embedder has finished assembling its router, using +/// the accepted socket's destination even when the listener binds a wildcard. +#[derive(Clone)] +struct InternalAcceptor; + +impl Accept for InternalAcceptor { + type Stream = tokio::net::TcpStream; + type Service = InternalService; + type Future = std::future::Ready>; + + fn accept(&self, stream: Self::Stream, inner: S) -> Self::Future { + std::future::ready( + stream + .local_addr() + .map(|local| (stream, InternalService { inner, local })), + ) + } +} + +#[derive(Clone)] +struct InternalService { + inner: S, + local: net::SocketAddr, +} + +impl tower_service::Service> for InternalService +where + S: tower_service::Service, Response = Response>, +{ + type Response = Response; + type Error = S::Error; + type Future = futures::future::Either>>; + + fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll> { + self.inner.poll_ready(cx) + } + + fn call(&mut self, request: http::Request) -> Self::Future { + if allows_request(&request, self.local) { + futures::future::Either::Left(self.inner.call(request)) + } else { + futures::future::Either::Right(std::future::ready(Ok(StatusCode::FORBIDDEN.into_response()))) + } + } +} + +fn allows_request(request: &http::Request, local: net::SocketAddr) -> bool { + // Origin alone blocks a cross-site form POST to the literal loopback IP. + // Authority validation also blocks same-origin reads through DNS rebinding. + if request.headers().contains_key(http::header::ORIGIN) { + return false; + } + // HTTP/1.1 carries one Host header, HTTP/2 and absolute-form requests a URI + // authority. At least one must be present, and each present must name this socket. + let mut hosts = request.headers().get_all(http::header::HOST).iter(); + let host = match (hosts.next(), hosts.next()) { + (None, _) => None, + (Some(host), None) => match http::uri::Authority::try_from(host.as_bytes()) { + Ok(host) => Some(host), + Err(_) => return false, + }, + (Some(_), Some(_)) => return false, + }; + let mut authorities = host.iter().chain(request.uri().authority()).peekable(); + authorities.peek().is_some() && authorities.all(|authority| owns_authority(authority, local)) +} + +fn owns_authority(authority: &http::uri::Authority, local: net::SocketAddr) -> bool { + let port = match authority.port_u16() { + Some(port) => port, + None if authority.as_str() == authority.host() => 80, + None => return false, + }; + if port != local.port() || authority.as_str().contains('@') { + return false; + } + let host = authority.host(); + if host.eq_ignore_ascii_case("localhost") { + // A dual-stack wildcard bind reports an IPv4 peer as `::ffff:127.0.0.1`. + return local.ip().to_canonical().is_loopback(); + } + let host = host + .strip_prefix('[') + .and_then(|host| host.strip_suffix(']')) + .unwrap_or(host); + host.parse::() + .is_ok_and(|ip| ip.to_canonical() == local.ip().to_canonical()) +} + /// Liveness probe mirror for the internal listener. Always `200 ok`. The /// customer-facing [`Web`](crate::web::Web) server serves its own public `/health`; /// this one lets an internal prober check the process over the trusted plane @@ -702,6 +798,148 @@ fn render_uring(_out: &mut String, _workers: &[UringWorker]) {} mod tests { use super::*; + #[test] + fn authority_matches_only_the_local_address_and_port() { + for (local, authority, expected) in [ + ("127.0.0.1:80", "127.0.0.1", true), + ("127.0.0.1:80", "LOCALHOST", true), + ("127.0.0.1:80", "localhost:", false), + ("127.0.0.1:80", "localhost:invalid", false), + ("127.0.0.1:80", "127.0.0.1:65536", false), + ("127.0.0.1:80", "user@127.0.0.1", false), + ("127.0.0.1:9101", "127.0.0.2:9101", false), + ("192.0.2.1:9101", "localhost:9101", false), + ("[::1]:80", "[::1]", true), + ("[::1]:9101", "[0:0:0:0:0:0:0:1]:9101", true), + ("[::ffff:127.0.0.1]:9101", "127.0.0.1:9101", true), + ("[::ffff:127.0.0.1]:9101", "localhost:9101", true), + ("[::ffff:192.0.2.1]:9101", "localhost:9101", false), + ] { + assert_eq!( + owns_authority(&authority.parse().unwrap(), local.parse().unwrap()), + expected, + "{local}, {authority}" + ); + } + } + + #[tokio::test] + async fn internal_http2_checks_authority_without_a_host_header() { + let internal = Internal::new( + Config { + listen: Some("127.0.0.1:0".parse().unwrap()), + }, + moq_net::stats::Registry::disabled(), + ) + .bind() + .unwrap(); + let addr = internal.addr().unwrap(); + let app = internal.routes(); + let server = tokio::spawn(internal.serve(app)); + let socket = tokio::net::TcpStream::connect(addr).await.unwrap(); + let (mut client, connection) = h2::client::handshake(socket).await.unwrap(); + let connection = tokio::spawn(connection); + for (authority, host, origin, expected) in [ + (addr.to_string(), None, None, StatusCode::OK), + ("evil.example".into(), None, None, StatusCode::FORBIDDEN), + (addr.to_string(), Some("evil.example"), None, StatusCode::FORBIDDEN), + (addr.to_string(), None, Some("null"), StatusCode::FORBIDDEN), + ] { + let mut request = http::Request::builder().uri(format!("http://{authority}/sessions")); + if let Some(host) = host { + request = request.header(http::header::HOST, host); + } + if let Some(origin) = origin { + request = request.header(http::header::ORIGIN, origin); + } + let (response, _) = client.send_request(request.body(()).unwrap(), true).unwrap(); + assert_eq!( + response.await.unwrap().status(), + expected, + "{authority}, host={host:?}, origin={origin:?}" + ); + } + connection.abort(); + assert!(connection.await.unwrap_err().is_cancelled()); + server.abort(); + assert!(server.await.unwrap_err().is_cancelled()); + } + + #[tokio::test] + async fn internal_requests_require_the_listener_authority_and_no_origin() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + for listen in ["127.0.0.1:0", "0.0.0.0:0", "[::1]:0", "[::]:0"] { + let internal = Internal::new( + Config { + listen: Some(listen.parse().unwrap()), + }, + moq_net::stats::Registry::disabled(), + ) + .bind() + .unwrap(); + let bound = internal.addr().unwrap(); + let ip = if bound.is_ipv4() { "127.0.0.1" } else { "::1" }; + let addr = net::SocketAddr::new(ip.parse().unwrap(), bound.port()); + let app = internal + .routes() + .merge(Router::new().route("/embedder", get(async || "embedded"))); + let server = tokio::spawn(internal.serve(app)); + for (method, path, headers, expected) in [ + ("GET", "/sessions", format!("Host: {addr}\r\n"), 200), + ("POST", "/sessions/revalidate", format!("Host: {addr}\r\n"), 200), + ("GET", "/embedder", format!("Host: localhost:{}\r\n", addr.port()), 200), + ( + "POST", + "/sessions/revalidate", + format!("Host: {addr}\r\nOrigin: https://evil.example\r\n"), + 403, + ), + ( + "POST", + "/sessions/revalidate", + format!("Host: {addr}\r\nOrigin: null\r\n"), + 403, + ), + ( + "GET", + "/sessions", + format!("Host: evil.example:{}\r\n", addr.port()), + 403, + ), + ( + "GET", + "/embedder", + format!("Host: evil.example:{}\r\n", addr.port()), + 403, + ), + ("GET", "/health", format!("Host: {addr}\r\nOrigin: \r\n"), 403), + ("GET", "/sessions", "Host: 192.0.2.1:9101\r\n".into(), 403), + ("GET", "/sessions", format!("Host: {ip}\r\n"), 403), + ("GET", "/sessions", String::new(), 403), + ("GET", "/sessions", format!("Host: {addr}\r\nHost: {addr}\r\n"), 403), + ("GET", "http://evil.example/sessions", format!("Host: {addr}\r\n"), 403), + ] { + let mut socket = tokio::net::TcpStream::connect(addr).await.unwrap(); + socket + .write_all( + format!("{method} {path} HTTP/1.1\r\n{headers}Connection: close\r\nContent-Length: 0\r\n\r\n") + .as_bytes(), + ) + .await + .unwrap(); + let mut response = String::new(); + socket.read_to_string(&mut response).await.unwrap(); + assert!( + response.starts_with(&format!("HTTP/1.1 {expected} ")), + "listen={listen}, {method} {path}, {headers:?}: {response}" + ); + } + server.abort(); + assert!(server.await.unwrap_err().is_cancelled()); + } + } + /// An `Config` whose listener is enabled, so `Internal` registers its own. fn listening() -> Config { Config { diff --git a/rs/moq-relay/src/web.rs b/rs/moq-relay/src/web.rs index 8c8b6dd8db9..953e576a098 100644 --- a/rs/moq-relay/src/web.rs +++ b/rs/moq-relay/src/web.rs @@ -340,12 +340,28 @@ impl Web { /// once across the merged router). GET CORS is applied to the complete router by [`Self::serve`], /// including any routes the embedder merges in without their own policy. pub fn routes(&self) -> Router { + // These responses depend on the caller's grant, including when auth or + // extraction fails. Publisher bytes must never become active content. + let granted = Router::new() + .route("/announced", get(serve_announced)) + .route("/announced/{*prefix}", get(serve_announced)) + .route("/fetch/{*path}", get(serve_fetch)) + .layer(axum::middleware::map_response(async |mut response: Response| { + let headers = response.headers_mut(); + headers.insert( + http::header::CACHE_CONTROL, + http::HeaderValue::from_static("private, no-store"), + ); + headers.insert( + http::header::X_CONTENT_TYPE_OPTIONS, + http::HeaderValue::from_static("nosniff"), + ); + response + })); let app = Router::new() .route("/health", get(serve_health)) .route("/certificate.sha256", get(serve_fingerprint)) - .route("/announced", get(serve_announced)) - .route("/announced/{*prefix}", get(serve_announced)) - .route("/fetch/{*path}", get(serve_fetch)); + .merge(granted); // If WebSocket is enabled, add the WebSocket route. Both `/` and // `/{*path}` map to the same handler so a client that dials a bare @@ -1042,7 +1058,11 @@ impl IntoResponse for ServeGroup { } } }); - Response::new(Body::from_stream(frames)) + ( + [(http::header::CONTENT_TYPE, "application/octet-stream")], + Body::from_stream(frames), + ) + .into_response() } } @@ -1053,6 +1073,95 @@ mod tests { use std::io::Write; use tempfile::TempDir; + #[tokio::test(start_paused = true)] + async fn authenticated_http_responses_are_private_and_cannot_be_sniffed() { + let dir = TempDir::new().unwrap(); + let (_, cert, _) = make_certs(&dir); + let certificates = moq_tokio::tls::Certificates::from_pem(&std::fs::read(cert).unwrap()).unwrap(); + let auth = crate::auth::Config { + public_subscribe: vec![moq_auth::Pattern::all()], + ..Default::default() + } + .init("test", &moq_tokio::tls::Connect::default()) + .unwrap(); + let cluster = cluster::Cluster::new(crate::cluster::Options::default()).unwrap(); + let broadcast = cluster.origin.create_broadcast("room").unwrap(); + broadcast.announce(Default::default()).unwrap(); + let track = broadcast.create_track("video", None).unwrap(); + let mut group = track.append_group().unwrap(); + const PAYLOAD: &[u8] = b""; + group.write_frame(moq_net::Timestamp::ZERO, PAYLOAD).unwrap(); + group.finish().unwrap(); + fn header(response: &Response, name: http::HeaderName) -> Option<&str> { + response.headers().get(name).and_then(|value| value.to_str().ok()) + } + let peer = ConnectInfo(crate::listener::Peer("127.0.0.1:1234".parse().unwrap())); + let mut refused = Web::new( + auth::Auth::refuse("test"), + cluster.clone(), + certificates.clone(), + Config::default(), + ) + .routes(); + for path in ["/fetch/room/video", "/announced/room"] { + let mut request = http::Request::builder().uri(path).body(Body::empty()).unwrap(); + request.extensions_mut().insert(peer); + let response = refused.call(request).await.unwrap(); + assert_eq!(response.status(), StatusCode::UNAUTHORIZED, "{path}"); + assert_eq!( + header(&response, http::header::CACHE_CONTROL), + Some("private, no-store"), + "{path}" + ); + assert_eq!( + header(&response, http::header::X_CONTENT_TYPE_OPTIONS), + Some("nosniff"), + "{path}" + ); + } + let web = Web::new(auth, cluster, certificates, Config::default()); + let mut routes = web.routes(); + for (path, expected, content_type) in [ + ( + "/fetch/room/video?group=0", + StatusCode::OK, + Some("application/octet-stream"), + ), + ("/fetch/room/video?group=1", StatusCode::NOT_FOUND, None), + ("/fetch/room/video?group=invalid", StatusCode::BAD_REQUEST, None), + ("/announced/room", StatusCode::OK, Some("text/plain; charset=utf-8")), + ("/announced", StatusCode::OK, Some("text/plain; charset=utf-8")), + ] { + let mut request = http::Request::builder().uri(path).body(Body::empty()).unwrap(); + request.extensions_mut().insert(peer); + let response = routes.call(request).await.unwrap(); + assert_eq!(response.status(), expected, "{path}"); + assert_eq!( + header(&response, http::header::CACHE_CONTROL), + Some("private, no-store"), + "{path}" + ); + assert_eq!( + header(&response, http::header::X_CONTENT_TYPE_OPTIONS), + Some("nosniff"), + "{path}" + ); + if let Some(content_type) = content_type { + assert_eq!( + header(&response, http::header::CONTENT_TYPE), + Some(content_type), + "{path}" + ); + } + if path == "/fetch/room/video?group=0" { + assert_eq!( + axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap(), + PAYLOAD + ); + } + } + } + fn make_certs(dir: &TempDir) -> (PathBuf, PathBuf, PathBuf) { make_named_certs(dir, "server", "localhost") }