diff --git a/docs/api.md b/docs/api.md index 3f7dc1d..c48dfd3 100644 --- a/docs/api.md +++ b/docs/api.md @@ -19,6 +19,7 @@ Base: `http://127.0.0.1:1984`. Errors are `{"error":{"code","message"}}`. | `GET /api/v1/docker/containers/{name}/logs?tail=&since=&follow=0\|1` | JSON, or SSE with events `stdout`/`stderr` | | `GET /api/v1/peers` | consolidator only; sorted status list; absent when peers are disabled | | `GET /api/v1/peers/{peer}` | one peer's status object; unknown name is 404 | +| `GET /api/v1/peers/{peer}/...` | GET-only pass-through of that peer's documented routes | ```sh TOKEN=slinuxm_... @@ -42,6 +43,15 @@ Present only when `[peers] enabled = true`. The same inbound token as the rest o `server` is the TCP check and `api` is the authenticated ping. `auth` is `ok`, `rejected` or `unknown`. Before the first check, `up` is false, `auth` is `unknown` and the timestamps are null. An unknown name is `404` `{"error":{"code":"not_found","message":"peer : unknown peer"}}`. +`GET /api/v1/peers/{peer}/{path}` forwards a GET to that peer and returns the peer's status and body. The path has to be one of the documented read routes: `systemd/units`, `systemd/units/{name}`, `systemd/timers`, `journal`, `journal/follow`, `docker/ps`, `docker/containers/{name}`, `docker/containers/{name}/logs`. `ping` and `peers/...` are not forwarded. `..`, encoded slashes and any other path are `400` `peer : path not allowed`. Query keys are the same ones the local route accepts (`state`, `unit`, `priority`, `lines`, `since`, `all`, `tail`, `follow`); anything else is `400` `peer : unsupported query parameter`. The caller's `Authorization` and `Cookie` are not forwarded; the consolidator sends the peer's own token. + +A request that already has `x-slinuxm-hop` is `400` `peer : chained requests are not allowed`. Other methods are `405`. `journal/follow` and docker logs with `follow=1` are `text/event-stream`, cut off at `limits.max_follow_secs` or shutdown. Bodies larger than `peers.max_response_bytes` are `503` `peer : oversize`. + + +```sh +curl -H "Authorization: Bearer $TOKEN" 'http://127.0.0.1:1984/api/v1/peers/beta/systemd/units?state=failed' +``` + ## MCP Endpoint: `http://127.0.0.1:1984/mcp` (Streamable HTTP). Eight tools (`list_systemd_units`, `get_systemd_unit`, `list_systemd_timers`, `get_journal_logs`, `tail_journal`, `list_containers`, `get_container`, `get_container_logs`), two prompts (`diagnose_unit`, `container_health_report`) two resources (`systemd://units/failed`, `docker://containers`) and two resource templates (`systemd://unit/{name}`, `docker://container/{name}`, same JSON as `get_systemd_unit` and `get_container`; percent-encode unusual characters in the name). Log content is untrusted data and is labelled as such in tool results. diff --git a/src/api/behaviour_tests.rs b/src/api/behaviour_tests.rs index 0c3e6c0..61141d9 100644 --- a/src/api/behaviour_tests.rs +++ b/src/api/behaviour_tests.rs @@ -6,6 +6,7 @@ use std::time::Duration; use async_trait::async_trait; use axum::body::{to_bytes, Body}; use axum::http::{Request, StatusCode}; +use bytes::Bytes; use tokio::sync::{mpsc, Notify}; use tokio_util::sync::CancellationToken; use tower::ServiceExt; @@ -14,8 +15,13 @@ use super::router; use super::testutil::{get, json}; use crate::config::Config; use crate::error::AppError; -use crate::peers::testing::{fake_state_with_peers, static_registry}; -use crate::peers::types::{ApiStatus, AuthState, PeerName, PeerStatus, ServerStatus}; +use crate::peers::error::PeerError; +use crate::peers::testing::{ + fake_peers_config, fake_state_with_peers, registry_with, static_registry, FakeTransport, +}; +use crate::peers::transport::{PeerBody, PeerRequest, PeerResponse, PeerTransport}; +use crate::peers::types::{ApiStatus, AuthState, PeerConfig, PeerName, PeerStatus, ServerStatus}; +use crate::peers::PeerRegistry; use crate::sources::fake::fake_state; use crate::sources::{ ContainerStatus, ContainerSummary, DockerSource, JournalEntry, JournalQuery, JournalSource, @@ -275,3 +281,277 @@ async fn peer_status_routes_follow_the_registry_snapshot() { let (s, _) = get(&bare, "/peers").await; assert_eq!(s, StatusCode::NOT_FOUND); } + +async fn call( + app: &axum::Router, + method: &str, + uri: &str, + headers: &[(&str, &str)], +) -> (StatusCode, axum::http::HeaderMap, String) { + let mut builder = Request::builder().method(method).uri(uri); + for (name, value) in headers { + builder = builder.header(*name, *value); + } + let resp = app + .clone() + .oneshot(builder.body(Body::empty()).unwrap()) + .await + .unwrap(); + let status = resp.status(); + let headers = resp.headers().clone(); + let body = to_bytes(resp.into_body(), usize::MAX).await.unwrap(); + (status, headers, String::from_utf8(body.to_vec()).unwrap()) +} + +fn proxy_app(fake: Arc) -> axum::Router { + router(fake_state_with_peers( + Config::default(), + registry_with(fake, fake_peers_config(&["beta"])), + )) +} + +#[tokio::test] +async fn proxy_forwards_allowlisted_get_and_filters_the_query() { + let fake = FakeTransport::new(); + fake.peer("beta").respond( + "/systemd/units", + 200, + "application/json", + r#"[{"name":"nginx.service"}]"#, + ); + let app = proxy_app(fake.clone()); + let (status, _headers, body) = call( + &app, + "GET", + "/peers/beta/systemd/units?state=failed", + &[ + ("authorization", "Bearer caller-token"), + ("cookie", "session=secret"), + ( + "traceparent", + "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01", + ), + ], + ) + .await; + assert_eq!(status, StatusCode::OK); + assert_eq!(body, r#"[{"name":"nginx.service"}]"#); + let calls = fake.calls(); + assert_eq!(calls.len(), 1); + assert_eq!(calls[0].1.path, "/systemd/units"); + assert_eq!(calls[0].1.query, vec![("state".into(), "failed".into())]); + assert!(calls[0] + .1 + .headers + .iter() + .any(|(k, v)| k.as_str() == "x-slinuxm-hop" && v == "1")); + assert!(calls[0] + .1 + .headers + .iter() + .any(|(k, v)| k.as_str() == "traceparent" && v.to_str().unwrap().starts_with("00-"))); + assert!(!calls[0] + .1 + .headers + .iter() + .any(|(k, _)| k.as_str() == "authorization" || k.as_str() == "cookie")); +} + +#[tokio::test] +async fn proxy_rejects_non_get_traversal_hop_and_foreign_query() { + let fake = FakeTransport::new(); + fake.peer("beta") + .respond("/systemd/units", 200, "application/json", "{}"); + let app = proxy_app(fake.clone()); + for method in ["POST", "DELETE", "PUT"] { + let (status, _, body) = call(&app, method, "/peers/beta/systemd/units", &[]).await; + assert_eq!(status, StatusCode::METHOD_NOT_ALLOWED, "{method}"); + assert_eq!(json(&body)["error"]["code"], "method_not_allowed"); + } + for uri in [ + "/peers/beta/ping", + "/peers/beta/peers/gamma/systemd/units", + "/peers/beta/systemd/units/../timers", + "/peers/beta/systemd/units/%2e%2e", + "/peers/beta/journal%2ffollow", + "/peers/beta/systemd/units/foo%2Fbar", + "/peers/beta/docker/containers/a/b", + ] { + let (status, _, body) = call(&app, "GET", uri, &[]).await; + assert_eq!(status, StatusCode::BAD_REQUEST, "{uri}: {body}"); + assert_eq!(json(&body)["error"]["code"], "bad_request"); + assert_eq!( + json(&body)["error"]["message"], + "peer beta: path not allowed" + ); + } + let (status, _, body) = call( + &app, + "GET", + "/peers/beta/systemd/units?state=failed&token=abc", + &[], + ) + .await; + assert_eq!(status, StatusCode::BAD_REQUEST); + assert_eq!( + json(&body)["error"]["message"], + "peer beta: unsupported query parameter" + ); + let (status, _, body) = call( + &app, + "GET", + "/peers/beta/systemd/units", + &[("x-slinuxm-hop", "1")], + ) + .await; + assert_eq!(status, StatusCode::BAD_REQUEST); + assert_eq!( + json(&body)["error"]["message"], + "peer beta: chained requests are not allowed" + ); + let (status, _, body) = call( + &app, + "GET", + "/peers/beta/journal/follow", + &[("x-slinuxm-hop", "")], + ) + .await; + assert_eq!(status, StatusCode::BAD_REQUEST, "{body}"); + assert!(fake.calls().is_empty()); +} + +#[tokio::test] +async fn proxy_streams_follow_and_stops_on_shutdown() { + let reg = Arc::new(PeerRegistry::new( + fake_peers_config(&["beta"]), + Arc::new(GatedSse), + )); + let mut cfg = Config::default(); + cfg.limits.max_follow_secs = 30; + let st = fake_state_with_peers(cfg, reg); + let shutdown = st.shutdown.clone(); + let app = router(st); + tokio::spawn(async move { + tokio::time::sleep(Duration::from_millis(200)).await; + shutdown.cancel(); + }); + let started = std::time::Instant::now(); + let (status, _, body) = call(&app, "GET", "/peers/beta/journal/follow", &[]).await; + assert_eq!(status, StatusCode::OK); + assert!(body.contains("data: 0"), "{body}"); + assert!(!body.contains("data: 1"), "{body}"); + assert!(started.elapsed() < Duration::from_secs(5)); +} + +#[tokio::test] +async fn proxy_stream_stops_at_follow_deadline() { + let reg = Arc::new(PeerRegistry::new( + fake_peers_config(&["beta"]), + Arc::new(GatedSse), + )); + let mut cfg = Config::default(); + cfg.limits.max_follow_secs = 1; + let app = router(fake_state_with_peers(cfg, reg)); + let started = std::time::Instant::now(); + let (status, _, body) = call(&app, "GET", "/peers/beta/journal/follow", &[]).await; + assert_eq!(status, StatusCode::OK); + assert!(body.contains("data: 0"), "{body}"); + assert!(!body.contains("data: 1"), "{body}"); + let elapsed = started.elapsed(); + assert!(elapsed >= Duration::from_millis(900), "{elapsed:?}"); + assert!(elapsed < Duration::from_secs(5), "{elapsed:?}"); +} + +#[tokio::test] +async fn proxy_caps_buffered_bodies_and_passes_sse_bytes() { + let fake = FakeTransport::new(); + fake.peer("beta") + .respond("/systemd/units", 200, "application/json", "0123456789") + .stream( + "/journal/follow", + vec![Bytes::from_static(b"abcd"), Bytes::from_static(b"efgh")], + ); + let mut peers = fake_peers_config(&["beta"]); + peers.max_response_bytes = 4; + let app = router(fake_state_with_peers( + Config::default(), + registry_with(fake, peers), + )); + let (status, _, body) = call(&app, "GET", "/peers/beta/systemd/units", &[]).await; + assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); + assert_eq!(json(&body)["error"]["message"], "peer beta: oversize"); + assert!(!body.contains("0123456789")); + + let (status, headers, body) = call(&app, "GET", "/peers/beta/journal/follow", &[]).await; + assert_eq!(status, StatusCode::OK); + assert_eq!( + headers + .get(axum::http::header::CONTENT_TYPE) + .and_then(|v| v.to_str().ok()), + Some("text/event-stream") + ); + // The cap stops the stream once four bytes have been forwarded. + assert_eq!(body, "abcd"); +} + +#[tokio::test] +async fn proxy_unknown_peer_and_smuggled_content_type() { + let fake = FakeTransport::new(); + fake.peer("beta").respond( + "/systemd/units", + 200, + "application/json\r\nX-Injected: 1", + "{}", + ); + let app = proxy_app(fake); + let (status, _, body) = call(&app, "GET", "/peers/nope/systemd/units", &[]).await; + assert_eq!(status, StatusCode::NOT_FOUND); + assert_eq!(json(&body)["error"]["message"], "peer nope: unknown peer"); + let (status, headers, body) = call(&app, "GET", "/peers/beta/systemd/units", &[]).await; + assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR); + assert_eq!( + json(&body)["error"]["message"], + "peer beta: malformed response" + ); + assert!(headers.get("x-injected").is_none()); + + let bare = router(fake_state(Config::default())); + let (status, _, _) = call(&bare, "GET", "/peers/beta/systemd/units", &[]).await; + assert_eq!(status, StatusCode::NOT_FOUND); +} + +/// First chunk is immediate; the next waits long enough that shutdown or the +/// follow deadline ends the pass-through. `FakeTransport` only has finite +/// scripts, so it cannot hold a stream open. +struct GatedSse; + +#[async_trait] +impl PeerTransport for GatedSse { + async fn tcp_check(&self, _: &PeerConfig, _: Duration) -> Result { + Ok(Duration::from_millis(1)) + } + + async fn get( + &self, + _: &PeerConfig, + req: PeerRequest, + _: Duration, + ) -> Result { + assert_eq!(req.path, "/journal/follow"); + assert_eq!(req.accept.as_deref(), Some("text/event-stream")); + let stream = futures::stream::unfold(0u32, |n| async move { + if n > 0 { + tokio::time::sleep(Duration::from_secs(30)).await; + } + Some(( + Ok::(Bytes::from(format!("data: {n}\n\n"))), + n + 1, + )) + }); + Ok(PeerResponse { + status: 200, + content_type: Some("text/event-stream".into()), + body: PeerBody::Stream(Box::pin(stream)), + }) + } +} diff --git a/src/api/peers.rs b/src/api/peers.rs index 6c3b807..0361f92 100644 --- a/src/api/peers.rs +++ b/src/api/peers.rs @@ -4,19 +4,29 @@ //! peers routes at all. Pass-through lives in `crate::peers::proxy` so MCP //! calls the same code. -use axum::extract::{Path, State}; +use std::time::Duration; + +use axum::body::Body; +use axum::extract::{Path, Request, State}; +use axum::http::{header, HeaderValue, StatusCode}; +use axum::response::{IntoResponse, Response}; use axum::routing::get; use axum::{Json, Router}; use serde::Serialize; use crate::error::AppError; -use crate::peers::types::PeerStatus; +use crate::peers::error::peer_error_to_app; +use crate::peers::proxy::{self, PreparedBody}; +use crate::peers::transport::PeerResponse; +use crate::peers::types::{PeerName, PeerStatus}; +use crate::peers::ProxyOpts; use crate::state::AppState; pub fn routes() -> Router { Router::new() .route("/peers", get(list)) .route("/peers/{peer}", get(one)) + .route("/peers/{peer}/{*path}", get(pass)) } #[derive(Serialize)] @@ -42,6 +52,122 @@ async fn one( Ok(Json(registry(&st)?.status(&peer)?)) } +async fn pass( + State(st): State, + Path((peer, _path)): Path<(String, String)>, + request: Request, +) -> Response { + let Some(reg) = st.peers.clone() else { + return AppError::NotFound("no such route".into()).into_response(); + }; + if let Err(err) = proxy::reject_inbound_hop(&peer, request.headers()) { + return err.into_response(); + } + let Some(raw_path) = raw_peer_path(request.uri().path()) else { + return AppError::BadRequest(format!("peer {peer}: path not allowed")).into_response(); + }; + let query = match proxy::query_from_raw(&peer, request.uri().query()) { + Ok(query) => query, + Err(err) => return err.into_response(), + }; + let opts = ProxyOpts { + traceparent: inbound_traceparent(request.headers()), + accept_sse: accepts_event_stream(request.headers()), + }; + match reg.get(&peer, raw_path, &query, opts).await { + Ok(resp) => proxy_response(&st, &peer, resp), + Err(err) => err.into_response(), + } +} + +/// Path after `/peers/{name}/`, still percent-encoded. Nesting strips +/// `/api/v1` before this runs, so both the mounted server and the bare API +/// router yield `/peers/...`. +fn raw_peer_path(uri_path: &str) -> Option<&str> { + let mut rest = uri_path; + while let Some(stripped) = rest.strip_prefix('/') { + if let Some(after) = stripped.strip_prefix("peers/") { + let (_name, path) = after.split_once('/')?; + return Some(path); + } + rest = &stripped[stripped.find('/')?..]; + } + None +} + +fn inbound_traceparent(headers: &axum::http::HeaderMap) -> Option { + let value = headers.get("traceparent")?.to_str().ok()?; + if (1..=128).contains(&value.len()) && value.bytes().all(|b| b.is_ascii_graphic()) { + Some(value.to_owned()) + } else { + None + } +} + +fn accepts_event_stream(headers: &axum::http::HeaderMap) -> bool { + headers + .get(header::ACCEPT) + .and_then(|value| value.to_str().ok()) + .is_some_and(|value| { + value + .split(',') + .any(|part| part.trim().starts_with("text/event-stream")) + }) +} + +fn proxy_response(st: &AppState, peer: &str, resp: PeerResponse) -> Response { + let name = match PeerName::parse(peer) { + Ok(name) => name, + Err(_) => { + return AppError::NotFound(format!("peer {peer}: unknown peer")).into_response(); + } + }; + let max_bytes = st + .peers + .as_ref() + .map(|reg| reg.config().max_response_bytes) + .unwrap_or(0); + let deadline = + tokio::time::Instant::now() + Duration::from_secs(st.config.limits.max_follow_secs); + let status = match StatusCode::from_u16(resp.status) { + Ok(status) => status, + Err(_) => { + return peer_error_to_app(&name, &crate::peers::PeerError::Http(resp.status)) + .into_response(); + } + }; + let content_type = match content_type_header(&name, resp.content_type.as_deref()) { + Ok(value) => value, + Err(err) => return err.into_response(), + }; + let body = match proxy::prepare_body(&name, resp.body, max_bytes, deadline, st.shutdown.clone()) + { + Ok(PreparedBody::Bytes(bytes)) => Body::from(bytes), + Ok(PreparedBody::Stream(stream)) => Body::from_stream(stream), + Err(err) => return err.into_response(), + }; + let mut response = Response::new(body); + *response.status_mut() = status; + if let Some(content_type) = content_type { + response + .headers_mut() + .insert(header::CONTENT_TYPE, content_type); + } + response +} + +fn content_type_header( + peer: &PeerName, + value: Option<&str>, +) -> Result, AppError> { + match value { + None => Ok(None), + Some(value) => HeaderValue::from_str(value).map(Some).map_err(|_| { + peer_error_to_app(peer, &crate::peers::PeerError::Malformed(String::new())) + }), + } +} + #[cfg(test)] mod tests { use axum::http::StatusCode; diff --git a/src/peers/proxy.rs b/src/peers/proxy.rs index 63d93cf..20c1822 100644 --- a/src/peers/proxy.rs +++ b/src/peers/proxy.rs @@ -1,24 +1,70 @@ //! Proxy one GET to a peer. //! -//! The path allowlist, rejection of `..` and encoded slashes, and the query -//! allowlist belong in this module. They are not implemented yet: this stub -//! forwards the request and maps peer failures. REST and MCP share this -//! function, so those checks must not grow a second copy. Caller credentials -//! are never forwarded. +//! REST and MCP both call [`get`]. The allowlist is the peer's documented +//! read routes; `ping` and `peers` are excluded so this cannot chain or +//! become an open proxy. Caller credentials are never copied: the transport +//! attaches the peer token itself. +//! +//! Paths are the route under `/api/v1` (`/systemd/units`). A caller that +//! includes the `/api/v1` prefix is accepted and that prefix is stripped, so +//! the transport always sees one form. use std::time::Duration; -use axum::http::{HeaderName, HeaderValue}; +use tokio::time::Instant; -use super::error::peer_error_to_app; +use axum::http::{HeaderMap, HeaderName, HeaderValue}; +use bytes::Bytes; +use futures::stream::{BoxStream, Stream, StreamExt}; +use tokio_util::sync::CancellationToken; + +use super::error::{peer_error_to_app, PeerError}; use super::registry::ProxyOpts; -use super::transport::{PeerRequest, PeerResponse, PeerTransport}; -use super::types::PeerConfig; +use super::transport::{PeerBody, PeerRequest, PeerResponse, PeerTransport}; +use super::types::{PeerConfig, PeerName}; use crate::error::AppError; const HOP_HEADER: &str = "x-slinuxm-hop"; const TRACEPARENT: &str = "traceparent"; +/// Reject an inbound hop marker. Any value counts: a consolidator must not +/// forward a request that has already been proxied. +pub(crate) fn reject_inbound_hop(peer: &str, headers: &HeaderMap) -> Result<(), AppError> { + if headers.contains_key(HOP_HEADER) { + return Err(chained(peer)); + } + Ok(()) +} + +fn chained(peer: &str) -> AppError { + AppError::BadRequest(format!("peer {peer}: chained requests are not allowed")) +} + +fn path_rejected(peer: &PeerName) -> AppError { + AppError::BadRequest(format!("peer {peer}: path not allowed")) +} + +fn query_rejected(peer: &str) -> AppError { + AppError::BadRequest(format!("peer {peer}: unsupported query parameter")) +} + +/// Parse a raw query string. Bad encodings are rejected before the pairs are +/// handed to [`get`], which checks the allowlist again. +pub(crate) fn query_from_raw( + peer: &str, + raw: Option<&str>, +) -> Result, AppError> { + let Some(raw) = raw.filter(|s| !s.is_empty()) else { + return Ok(Vec::new()); + }; + if raw.len() > 4096 || encoding_forbidden(raw) || raw.chars().any(|c| c.is_control()) { + return Err(query_rejected(peer)); + } + Ok(url::form_urlencoded::parse(raw.as_bytes()) + .map(|(k, v)| (k.into_owned(), v.into_owned())) + .collect()) +} + fn header(name: &'static str, value: &str) -> (HeaderName, HeaderValue) { let name = HeaderName::from_static(name); let value = HeaderValue::from_str(value).expect("static header value"); @@ -33,41 +79,275 @@ pub(crate) async fn get( opts: ProxyOpts, timeout: Duration, ) -> Result { + let Validated { path, query, sse } = validate(&peer.name, path, query, opts.accept_sse)?; // Marks the hop so a peer refuses chained consolidation. let mut headers = vec![header(HOP_HEADER, "1")]; if let Some(traceparent) = opts.traceparent.as_deref() { if let Ok(value) = HeaderValue::from_str(traceparent) { - headers.push((HeaderName::from_static(TRACEPARENT), value)); + if !value.as_bytes().contains(&b' ') { + headers.push((HeaderName::from_static(TRACEPARENT), value)); + } } } let req = PeerRequest { - path: path.to_owned(), - query: query.to_vec(), + path, + query, headers, - accept: opts.accept_sse.then(|| "text/event-stream".to_owned()), + accept: sse.then(|| "text/event-stream".to_owned()), }; - transport - .get(peer, req, timeout) - .await - .map_err(|e| peer_error_to_app(&peer.name, &e)) + let result = tokio::time::timeout(timeout, transport.get(peer, req, timeout)).await; + match result { + Ok(Ok(resp)) => Ok(resp), + Ok(Err(e)) => Err(peer_error_to_app(&peer.name, &e)), + Err(_elapsed) => Err(peer_error_to_app(&peer.name, &PeerError::Timeout)), + } +} + +/// Buffered bodies over the cap become `Oversize`. Streams are cut off at the +/// cap, at `deadline`, or when `shutdown` fires, because a follow holds a +/// connection until the peer closes it. +pub(crate) fn prepare_body( + peer: &PeerName, + body: PeerBody, + max_bytes: u64, + deadline: Instant, + shutdown: CancellationToken, +) -> Result { + match body { + PeerBody::Bytes(bytes) if bytes.len() as u64 > max_bytes => { + Err(peer_error_to_app(peer, &PeerError::Oversize)) + } + PeerBody::Bytes(bytes) => Ok(PreparedBody::Bytes(bytes)), + PeerBody::Stream(stream) => Ok(PreparedBody::Stream(Box::pin(bound_stream( + stream, max_bytes, deadline, shutdown, + )))), + } +} + +pub(crate) enum PreparedBody { + Bytes(Bytes), + Stream(BoxStream<'static, Result>), +} + +fn bound_stream( + stream: BoxStream<'static, Result>, + max_bytes: u64, + deadline: Instant, + shutdown: CancellationToken, +) -> impl Stream> + Send { + futures::stream::unfold((stream, 0u64), move |(mut stream, sent)| { + let shutdown = shutdown.clone(); + async move { + let next = tokio::select! { + biased; + () = shutdown.cancelled() => return None, + () = tokio::time::sleep_until(deadline) => return None, + item = stream.next() => item, + }; + let bytes = match next? { + Ok(bytes) => bytes, + // Headers are already sent. Dropping the chunk keeps a + // token-file path or peer URL out of the body. + Err(_) => return None, + }; + if sent.saturating_add(bytes.len() as u64) > max_bytes { + return None; + } + let sent = sent + bytes.len() as u64; + Some((Ok(bytes), (stream, sent))) + } + }) +} + +struct Validated { + path: String, + query: Vec<(String, String)>, + sse: bool, +} + +fn validate( + peer: &PeerName, + path: &str, + query: &[(String, String)], + accept_sse: bool, +) -> Result { + let route = canonical_route(path).ok_or_else(|| path_rejected(peer))?; + let query = allow_query(peer, &route, query)?; + let sse = accept_sse || route_is_follow(&route, &query); + Ok(Validated { + path: format!("/{route}"), + query, + sse, + }) +} + +fn canonical_route(path: &str) -> Option { + if path.len() > 512 || encoding_forbidden(path) { + return None; + } + let decoded = percent_encoding::percent_decode_str(path) + .decode_utf8() + .ok()?; + if !decoded_chars_ok(&decoded) { + return None; + } + let mut rel = decoded.trim_start_matches('/'); + if let Some(rest) = rel.strip_prefix("api/v1/") { + rel = rest; + } + if rel.is_empty() || rel.split('/').any(|s| !segment_chars_ok(s)) { + return None; + } + if !route_allowed(rel) { + return None; + } + Some(rel.to_owned()) +} + +fn segment_chars_ok(segment: &str) -> bool { + !segment.is_empty() && segment != "." && segment != ".." && !segment.contains("..") +} + +fn decoded_chars_ok(s: &str) -> bool { + !s.chars() + .any(|c| c.is_control() || c == '\\' || c == '%' || c == '?' || c == '#') +} + +/// Encoded slash, backslash, dot-dot, NUL and CRLF, including a second layer +/// of encoding (`%252f`). Checked case-insensitively on the raw text. +fn encoding_forbidden(raw: &str) -> bool { + let lower = raw.to_ascii_lowercase(); + [ + "%2f", "%5c", "%00", "%0a", "%0d", "%2e%2e", "%252f", "%255c", + ] + .iter() + .any(|needle| lower.contains(needle)) +} + +fn route_allowed(route: &str) -> bool { + matches!( + route, + "systemd/units" | "systemd/timers" | "journal" | "journal/follow" | "docker/ps" + ) || unit_path(route) + || container_path(route) +} + +fn unit_path(route: &str) -> bool { + route + .strip_prefix("systemd/units/") + .is_some_and(resource_segment) +} + +fn container_path(route: &str) -> bool { + let Some(rest) = route.strip_prefix("docker/containers/") else { + return false; + }; + match rest.strip_suffix("/logs") { + Some(name) => resource_segment(name), + None => resource_segment(rest), + } +} + +fn resource_segment(name: &str) -> bool { + let mut chars = name.chars(); + let Some(first) = chars.next() else { + return false; + }; + first.is_ascii_alphanumeric() + && name + .chars() + .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-' | '@' | ':')) +} + +fn allowed_keys(route: &str) -> &'static [&'static str] { + match route { + "systemd/units" => &["state"], + "journal" | "journal/follow" => &["unit", "priority", "lines", "since"], + "docker/ps" => &["all"], + logs if logs.starts_with("docker/containers/") && logs.ends_with("/logs") => { + &["tail", "since", "follow"] + } + _ => &[], + } +} + +fn allow_query( + peer: &PeerName, + route: &str, + query: &[(String, String)], +) -> Result, AppError> { + if query.len() > 8 { + return Err(query_rejected(peer.as_str())); + } + let allowed = allowed_keys(route); + let mut seen = Vec::new(); + let mut out = Vec::with_capacity(query.len()); + for (key, value) in query { + let known = allowed.iter().any(|name| *name == key); + if !known || !query_key_ok(key) || !query_value_ok(value) || seen.contains(&key.as_str()) { + return Err(query_rejected(peer.as_str())); + } + seen.push(key.as_str()); + out.push((key.clone(), value.clone())); + } + Ok(out) +} + +fn query_key_ok(key: &str) -> bool { + !key.is_empty() + && key.len() <= 32 + && key + .chars() + .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') +} + +fn query_value_ok(value: &str) -> bool { + value.len() <= 1024 + && !value + .chars() + .any(|c| c.is_control() || c == '\\' || c == '/') + && !value.contains("..") + && !encoding_forbidden(value) +} + +fn route_is_follow(route: &str, query: &[(String, String)]) -> bool { + if route == "journal/follow" { + return true; + } + route.ends_with("/logs") + && query + .iter() + .any(|(key, value)| key == "follow" && matches!(value.as_str(), "1" | "true")) } #[cfg(test)] mod tests { use super::*; use crate::peers::testing::{fake_peers_config, FakeTransport}; + use futures::StreamExt; + + fn peer_cfg() -> PeerConfig { + fake_peers_config(&["beta"]).peer.remove(0) + } + + fn header_of<'a>(req: &'a PeerRequest, name: &str) -> Option<&'a str> { + req.headers + .iter() + .find(|(k, _)| k.as_str() == name) + .map(|(_, v)| v.to_str().unwrap()) + } #[tokio::test] async fn adds_hop_and_trace_context_but_no_credentials() { let fake = FakeTransport::new(); - let cfg = fake_peers_config(&["beta"]); + let peer = peer_cfg(); let opts = ProxyOpts { traceparent: Some("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01".into()), accept_sse: true, }; get( fake.as_ref(), - &cfg.peer[0], + &peer, "/journal/follow", &[("lines".into(), "10".into())], opts, @@ -82,26 +362,20 @@ mod tests { assert_eq!(req.path, "/journal/follow"); assert_eq!(req.query, vec![("lines".to_owned(), "10".to_owned())]); assert_eq!(req.accept.as_deref(), Some("text/event-stream")); - let header = |name: &str| { - req.headers - .iter() - .find(|(k, _)| k.as_str() == name) - .map(|(_, v)| v.to_str().unwrap()) - }; - assert_eq!(header("x-slinuxm-hop"), Some("1")); - assert!(header("traceparent").unwrap().starts_with("00-")); - assert_eq!(header("authorization"), None); + assert_eq!(header_of(req, "x-slinuxm-hop"), Some("1")); + assert!(header_of(req, "traceparent").unwrap().starts_with("00-")); + assert_eq!(header_of(req, "authorization"), None); + assert_eq!(header_of(req, "cookie"), None); } #[tokio::test] async fn maps_rejected_with_peer_name() { let fake = FakeTransport::new(); fake.peer("beta") - .respond_err("/systemd/units", crate::peers::error::PeerError::Rejected); - let cfg = fake_peers_config(&["beta"]); + .respond_err("/systemd/units", PeerError::Rejected); let err = get( fake.as_ref(), - &cfg.peer[0], + &peer_cfg(), "/systemd/units", &[], ProxyOpts::default(), @@ -110,7 +384,243 @@ mod tests { .await .unwrap_err(); assert_eq!(err.code(), "unavailable"); - assert!(err.message().contains("beta")); assert_eq!(err.message(), "peer beta: peer rejected credentials"); } + + #[tokio::test] + async fn allowlist_forwards_documented_routes_only() { + let fake = FakeTransport::new(); + let peer = peer_cfg(); + let ok = [ + "/systemd/units", + "systemd/units/nginx.service", + "/systemd/timers", + "/journal", + "/journal/follow", + "/docker/ps", + "/docker/containers/web", + "/docker/containers/web/logs", + "/api/v1/systemd/units", + "systemd/units/user@100.service", + ]; + for path in ok { + get( + fake.as_ref(), + &peer, + path, + &[], + ProxyOpts::default(), + Duration::from_secs(1), + ) + .await + .unwrap(); + } + let paths: Vec<_> = fake.calls().into_iter().map(|(_, req)| req.path).collect(); + assert_eq!( + paths, + vec![ + "/systemd/units", + "/systemd/units/nginx.service", + "/systemd/timers", + "/journal", + "/journal/follow", + "/docker/ps", + "/docker/containers/web", + "/docker/containers/web/logs", + "/systemd/units", + "/systemd/units/user@100.service", + ] + ); + } + + #[tokio::test] + async fn rejects_traversal_excluded_routes_and_bad_query_without_calling() { + let fake = FakeTransport::new(); + fake.peer("beta") + .respond("/systemd/units", 200, "application/json", "{}") + .respond("/journal/follow", 200, "application/json", "{}"); + let peer = peer_cfg(); + let bad_paths = [ + "/ping", + "/api/v1/ping", + "/peers/gamma/systemd/units", + "/healthz", + "/systemd", + "/systemd/units/../timers", + "/systemd/../docker/ps", + "systemd/units/%2e%2e", + "journal%2ffollow", + "systemd/units/foo%2Fbar", + "/systemd/units//nginx.service", + "/docker/containers/a/b", + "/journal/follow/extra", + "", + "/", + "/systemd/units/%2e.", + "systemd\\units", + ]; + for path in bad_paths { + let err = get( + fake.as_ref(), + &peer, + path, + &[], + ProxyOpts::default(), + Duration::from_secs(1), + ) + .await + .unwrap_err(); + assert_eq!(err.code(), "bad_request", "{path}"); + assert_eq!(err.message(), "peer beta: path not allowed", "{path}"); + } + let bad_queries = [ + vec![("token".into(), "abc".into())], + vec![ + ("state".into(), "failed".into()), + ("lines".into(), "1".into()), + ], + vec![ + ("state".into(), "failed".into()), + ("state".into(), "active".into()), + ], + vec![("State".into(), "failed".into())], + vec![("state".into(), "a/b".into())], + vec![("state".into(), "a%2fb".into())], + vec![("state".into(), "..".into())], + ]; + for query in bad_queries { + let err = get( + fake.as_ref(), + &peer, + "/systemd/units", + &query, + ProxyOpts::default(), + Duration::from_secs(1), + ) + .await + .unwrap_err(); + assert_eq!(err.code(), "bad_request", "{query:?}"); + assert_eq!(err.message(), "peer beta: unsupported query parameter"); + } + assert!(fake.calls().is_empty()); + } + + #[tokio::test] + async fn forwards_only_allowlisted_query_and_follow_accept() { + let fake = FakeTransport::new(); + let peer = peer_cfg(); + get( + fake.as_ref(), + &peer, + "/systemd/units", + &[("state".into(), "failed".into())], + ProxyOpts::default(), + Duration::from_secs(1), + ) + .await + .unwrap(); + get( + fake.as_ref(), + &peer, + "/docker/containers/web/logs", + &[("tail".into(), "10".into()), ("follow".into(), "1".into())], + ProxyOpts::default(), + Duration::from_secs(1), + ) + .await + .unwrap(); + get( + fake.as_ref(), + &peer, + "/docker/containers/web/logs", + &[("follow".into(), "0".into())], + ProxyOpts::default(), + Duration::from_secs(1), + ) + .await + .unwrap(); + let calls = fake.calls(); + assert_eq!(calls[0].1.query, vec![("state".into(), "failed".into())]); + assert!(calls[0].1.accept.is_none()); + assert_eq!(calls[1].1.accept.as_deref(), Some("text/event-stream")); + assert!(calls[2].1.accept.is_none()); + } + + #[tokio::test] + async fn slow_peer_is_a_timeout() { + let fake = FakeTransport::new(); + fake.peer("beta").delay(Duration::from_secs(30)); + let err = get( + fake.as_ref(), + &peer_cfg(), + "/systemd/units", + &[], + ProxyOpts::default(), + Duration::from_millis(20), + ) + .await + .unwrap_err(); + assert_eq!(err.message(), "peer beta: timeout"); + } + + #[tokio::test] + async fn inbound_hop_is_rejected() { + let mut headers = HeaderMap::new(); + headers.insert(HOP_HEADER, HeaderValue::from_static("1")); + let err = reject_inbound_hop("beta", &headers).unwrap_err(); + assert_eq!(err.code(), "bad_request"); + assert_eq!(err.message(), "peer beta: chained requests are not allowed"); + headers.insert(HOP_HEADER, HeaderValue::from_static("0")); + assert!(reject_inbound_hop("beta", &headers).is_err()); + assert!(reject_inbound_hop("beta", &HeaderMap::new()).is_ok()); + } + + #[tokio::test] + async fn query_raw_rejects_encoded_slash() { + let err = query_from_raw("beta", Some("state=failed&unit=a%2Fb")).unwrap_err(); + assert_eq!(err.message(), "peer beta: unsupported query parameter"); + let pairs = query_from_raw("beta", Some("state=failed")).unwrap(); + assert_eq!(pairs, vec![("state".into(), "failed".into())]); + } + + #[tokio::test] + async fn body_cap_and_stream_stop_on_shutdown() { + let name = PeerName::parse("beta").unwrap(); + let err = match prepare_body( + &name, + PeerBody::Bytes(Bytes::from_static(b"0123456789")), + 4, + Instant::now() + Duration::from_secs(5), + CancellationToken::new(), + ) { + Err(err) => err, + Ok(_) => panic!("expected an oversize error"), + }; + assert_eq!(err.message(), "peer beta: oversize"); + + let shutdown = CancellationToken::new(); + let stream = futures::stream::unfold(0u32, |n| async move { + if n > 0 { + tokio::time::sleep(Duration::from_secs(30)).await; + } + Some(( + Ok::(Bytes::from(format!("data: {n}\n\n"))), + n + 1, + )) + }); + let PreparedBody::Stream(mut body) = prepare_body( + &name, + PeerBody::Stream(Box::pin(stream)), + 1024, + Instant::now() + Duration::from_secs(30), + shutdown.clone(), + ) + .unwrap() else { + panic!("expected a stream"); + }; + let first = body.next().await.unwrap().unwrap(); + assert_eq!(&first[..], b"data: 0\n\n"); + shutdown.cancel(); + assert!(body.next().await.is_none()); + } }