diff --git a/src/api/files.rs b/src/api/files.rs index 288a10a..4acf443 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(&state.config, &headers) { + if !super::websocket_origin_ok(&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 84f0d74..b4ae054 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(&state.config, &headers) { + if !super::websocket_origin_ok(&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 158787f..f7c092a 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -80,25 +80,30 @@ pub fn router(state: Shared) -> Router { .with_state(state) } -/// Whether a browser WebSocket handshake came from the app's origin. +/// 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(config: &crate::config::Config, headers: &HeaderMap) -> bool { +pub(crate) fn websocket_origin_ok(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('/'); - let public = config.public_url.trim_end_matches('/'); - if origin == public { + let Some(host) = headers + .get(header::HOST) + .and_then(|value| value.to_str().ok()) + else { return true; - } - // The Nuxt dev server proxies the sockets from port 3000 while the - // API keeps its own port. Both are loopback, so accept that pair. - let loopback = public.starts_with("http://localhost") || public.starts_with("http://127.0.0.1"); - loopback && (origin.starts_with("http://localhost:") || origin.starts_with("http://127.0.0.1:")) + }; + let origin_authority = origin + .split_once("://") + .map(|(_, rest)| rest) + .and_then(|rest| rest.split('/').next()) + .unwrap_or(origin); + origin_authority.eq_ignore_ascii_case(host) } /// Rejects a mutating API request without the matching CSRF header. @@ -233,3 +238,47 @@ async fn spa(State(state): State, uri: Uri) -> Response { .body(Body::from(bytes)) .unwrap_or_else(|_| (StatusCode::INTERNAL_SERVER_ERROR, "error").into_response()) } + +#[cfg(test)] +mod tests { + use super::*; + use axum::http::{HeaderMap, HeaderValue, header}; + + fn headers(origin: &str, host: &str) -> HeaderMap { + let mut headers = HeaderMap::new(); + headers.insert(header::ORIGIN, HeaderValue::from_str(origin).unwrap()); + headers.insert(header::HOST, HeaderValue::from_str(host).unwrap()); + headers + } + + #[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" + ))); + } + + #[test] + fn websocket_origin_rejects_another_host() { + assert!(!websocket_origin_ok(&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)); + } +}