From 9c31dbcfe18c37970811596638ab98161638aa8d Mon Sep 17 00:00:00 2001 From: aien Date: Tue, 29 Sep 2026 14:30:12 -0500 Subject: [PATCH] fix(cockpit): lock /ws/terminal to loopback origin and peer; bind 127.0.0.1 The /ws/terminal WebSocket spawned a login shell over a PTY with no authentication, no Origin check and no peer check, and the listener bound 0.0.0.0:18095. Any local process, or any web page open in a browser on this host (cross-site WebSocket hijacking; CORS does not cover WebSockets), could get a shell. - Bind the cockpit to 127.0.0.1:18095. - Serve with connect info and reject the terminal upgrade with 403 unless the Origin is exactly http://127.0.0.1:18095 or http://localhost:18095 and the peer address is loopback. - Unit tests for allowed, missing, foreign and look-alike origins and a non-loopback peer. Co-authored-by: Drake Stapleton Co-Authored-By: Claude Opus 5.5 --- crates/spark-cockpit-rs/src/main.rs | 91 +++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 5 deletions(-) diff --git a/crates/spark-cockpit-rs/src/main.rs b/crates/spark-cockpit-rs/src/main.rs index c987ebd5..6bf5834f 100644 --- a/crates/spark-cockpit-rs/src/main.rs +++ b/crates/spark-cockpit-rs/src/main.rs @@ -1,7 +1,7 @@ use axum::{ Router, extract::{ - Query, State, + ConnectInfo, Query, State, ws::{Message, WebSocket, WebSocketUpgrade}, }, http::{HeaderMap, StatusCode}, @@ -1041,14 +1041,19 @@ async fn main() { ) .with_state(state); - let addr = SocketAddr::from(([0, 0, 0, 0], 18095)); + let addr = SocketAddr::from(([127, 0, 0, 1], 18095)); println!( "🚀 AIEN Native Sovereign Cockpit (Rust Axum) active on http://{}", addr ); let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); - axum::serve(listener, app).await.unwrap(); + axum::serve( + listener, + app.into_make_service_with_connect_info::(), + ) + .await + .unwrap(); } // ========================================================================= @@ -1241,8 +1246,27 @@ async fn handle_telemetry_live() -> Json { // ========================================================================= // ENDPOINT 2: LIVE INTERACTIVE PTY TERMINAL (/ws/terminal) // ========================================================================= -async fn handle_ws_terminal(ws: WebSocketUpgrade) -> impl IntoResponse { - ws.on_upgrade(handle_terminal_socket) +const TERMINAL_ALLOWED_ORIGINS: [&str; 2] = ["http://127.0.0.1:18095", "http://localhost:18095"]; + +/// The terminal spawns a shell, so only same-host browser pages served by the +/// cockpit itself may open it: exact loopback Origin and a loopback peer. +fn terminal_request_allowed(headers: &HeaderMap, peer: &SocketAddr) -> bool { + let origin_ok = headers + .get(axum::http::header::ORIGIN) + .and_then(|v| v.to_str().ok()) + .is_some_and(|o| TERMINAL_ALLOWED_ORIGINS.contains(&o)); + origin_ok && peer.ip().is_loopback() +} + +async fn handle_ws_terminal( + ConnectInfo(peer): ConnectInfo, + headers: HeaderMap, + ws: WebSocketUpgrade, +) -> Response { + if !terminal_request_allowed(&headers, &peer) { + return StatusCode::FORBIDDEN.into_response(); + } + ws.on_upgrade(handle_terminal_socket).into_response() } async fn handle_terminal_socket(mut socket: WebSocket) { @@ -3233,6 +3257,63 @@ async fn handle_mail_send( mod tests { use super::*; + fn origin_headers(origin: Option<&str>) -> HeaderMap { + let mut h = HeaderMap::new(); + if let Some(o) = origin { + h.insert(axum::http::header::ORIGIN, o.parse().unwrap()); + } + h + } + + #[test] + fn terminal_allows_loopback_origin_and_peer() { + let peer: SocketAddr = "127.0.0.1:50000".parse().unwrap(); + assert!(terminal_request_allowed( + &origin_headers(Some("http://127.0.0.1:18095")), + &peer + )); + assert!(terminal_request_allowed( + &origin_headers(Some("http://localhost:18095")), + &peer + )); + let peer6: SocketAddr = "[::1]:50000".parse().unwrap(); + assert!(terminal_request_allowed( + &origin_headers(Some("http://localhost:18095")), + &peer6 + )); + } + + #[test] + fn terminal_rejects_missing_or_foreign_origin() { + let peer: SocketAddr = "127.0.0.1:50000".parse().unwrap(); + assert!(!terminal_request_allowed(&origin_headers(None), &peer)); + assert!(!terminal_request_allowed( + &origin_headers(Some("http://evil.example")), + &peer + )); + assert!(!terminal_request_allowed( + &origin_headers(Some("http://127.0.0.1:18095.evil.example")), + &peer + )); + assert!(!terminal_request_allowed( + &origin_headers(Some("http://192.168.1.108:18095")), + &peer + )); + assert!(!terminal_request_allowed( + &origin_headers(Some("null")), + &peer + )); + } + + #[test] + fn terminal_rejects_non_loopback_peer() { + let peer: SocketAddr = "192.168.1.50:50000".parse().unwrap(); + assert!(!terminal_request_allowed( + &origin_headers(Some("http://127.0.0.1:18095")), + &peer + )); + } + #[test] fn skill_index_omits_bodies_and_preview_is_one_paragraph() { let dir = std::env::temp_dir().join(format!("aien-skill-filter-{}", std::process::id()));