From f21fdfd729c82a8110a2b1c5b88959dea0b8b0a3 Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Sat, 28 Nov 2026 02:19:44 +0100 Subject: [PATCH] fix: accept either the page host or the public url An upstream proxy may rewrite Host to its own address. Keep the configured public_url as a fallback so the terminal and presence sockets still pass in that setup, while the Host check covers localhost and LAN addresses. --- src/api/files.rs | 2 +- src/api/messages.rs | 2 +- src/api/mod.rs | 65 +++++++++++++++++++++++++++++---------------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/api/files.rs b/src/api/files.rs index 4acf443..288a10a 100644 --- a/src/api/files.rs +++ b/src/api/files.rs @@ -237,7 +237,7 @@ pub async fn terminal( headers: HeaderMap, ws: WebSocketUpgrade, ) -> AppResult { - if !super::websocket_origin_ok(&headers) { + if !super::websocket_origin_ok(&state.config, &headers) { return Err(AppError::forbidden("websocket origin does not match")); } member_of(&state, &room, &user.user.id)?; diff --git a/src/api/messages.rs b/src/api/messages.rs index b4ae054..84f0d74 100644 --- a/src/api/messages.rs +++ b/src/api/messages.rs @@ -220,7 +220,7 @@ pub async fn presence( headers: HeaderMap, ws: WebSocketUpgrade, ) -> AppResult { - if !super::websocket_origin_ok(&headers) { + if !super::websocket_origin_ok(&state.config, &headers) { return Err(AppError::forbidden("websocket origin does not match")); } let member = member_of(&state, &room, &user.user.id)?; diff --git a/src/api/mod.rs b/src/api/mod.rs index f7c092a..1fe8d40 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -81,22 +81,27 @@ pub fn router(state: Shared) -> Router { } /// Whether a browser WebSocket handshake came from the page's own -/// origin. The request carries the Host the browser used, so this works -/// behind a proxy and on any LAN address without trusting `public_url`. -/// A client that sends no `Origin` is not a browser page and is left to -/// the cookie check. -pub(crate) fn websocket_origin_ok(headers: &HeaderMap) -> bool { +/// origin. The request carries the Host the browser used, so the check +/// works behind a proxy and on any LAN address without relying on +/// `public_url` alone. `public_url` is still accepted for a proxy that +/// rewrites Host to its upstream. A client that sends no `Origin` is +/// not a browser page and is left to the cookie check. +pub(crate) fn websocket_origin_ok(config: &crate::config::Config, headers: &HeaderMap) -> bool { let Some(origin) = headers .get(header::ORIGIN) .and_then(|value| value.to_str().ok()) else { return true; }; + let origin = origin.trim_end_matches('/'); + if origin.eq_ignore_ascii_case(config.public_url.trim_end_matches('/')) { + return true; + } let Some(host) = headers .get(header::HOST) .and_then(|value| value.to_str().ok()) else { - return true; + return false; }; let origin_authority = origin .split_once("://") @@ -244,6 +249,12 @@ mod tests { use super::*; use axum::http::{HeaderMap, HeaderValue, header}; + fn config() -> crate::config::Config { + let mut config = crate::config::Config::default(); + config.public_url = "http://10.4.1.222:7800".into(); + config + } + fn headers(origin: &str, host: &str) -> HeaderMap { let mut headers = HeaderMap::new(); headers.insert(header::ORIGIN, HeaderValue::from_str(origin).unwrap()); @@ -253,32 +264,40 @@ mod tests { #[test] fn websocket_origin_matches_the_request_host() { - assert!(websocket_origin_ok(&headers( - "http://10.4.1.222:7800", - "10.4.1.222:7800" - ))); - assert!(websocket_origin_ok(&headers( - "https://together.example.com", - "together.example.com" - ))); - assert!(websocket_origin_ok(&headers( - "http://localhost:7800", - "localhost:7800" - ))); + assert!(websocket_origin_ok( + &config(), + &headers("http://10.4.1.222:7800", "10.4.1.222:7800") + )); + assert!(websocket_origin_ok( + &config(), + &headers("https://together.example.com", "together.example.com") + )); + assert!(websocket_origin_ok( + &config(), + &headers("http://localhost:7800", "localhost:7800") + )); + } + + #[test] + fn the_configured_public_url_survives_a_rewritten_host() { + assert!(websocket_origin_ok( + &config(), + &headers("http://10.4.1.222:7800", "127.0.0.1:7800") + )); } #[test] fn websocket_origin_rejects_another_host() { - assert!(!websocket_origin_ok(&headers( - "http://evil.example", - "10.4.1.222:7800" - ))); + assert!(!websocket_origin_ok( + &config(), + &headers("http://evil.example", "10.4.1.222:7800") + )); } #[test] fn a_missing_origin_is_left_to_the_cookie() { let mut headers = HeaderMap::new(); headers.insert(header::HOST, HeaderValue::from_static("localhost:7800")); - assert!(websocket_origin_ok(&headers)); + assert!(websocket_origin_ok(&config(), &headers)); } }