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.
This commit is contained in:
vhaudiquet committed 2026-11-28 02:19:44 +01:00
1 parent be04a6cf90
commit f21fdfd729
3 files changed
+44 -25

No files matched your search

+1 -1
View File
@@ -237,7 +237,7 @@ pub async fn terminal(
headers: HeaderMap, headers: HeaderMap,
ws: WebSocketUpgrade, ws: WebSocketUpgrade,
) -> AppResult<Response> { ) -> AppResult<Response> {
if !super::websocket_origin_ok(&headers) { if !super::websocket_origin_ok(&state.config, &headers) {
return Err(AppError::forbidden("websocket origin does not match")); return Err(AppError::forbidden("websocket origin does not match"));
} }
member_of(&state, &room, &user.user.id)?; member_of(&state, &room, &user.user.id)?;
+1 -1
View File
@@ -220,7 +220,7 @@ pub async fn presence(
headers: HeaderMap, headers: HeaderMap,
ws: WebSocketUpgrade, ws: WebSocketUpgrade,
) -> AppResult<Response> { ) -> AppResult<Response> {
if !super::websocket_origin_ok(&headers) { if !super::websocket_origin_ok(&state.config, &headers) {
return Err(AppError::forbidden("websocket origin does not match")); return Err(AppError::forbidden("websocket origin does not match"));
} }
let member = member_of(&state, &room, &user.user.id)?; let member = member_of(&state, &room, &user.user.id)?;
+42 -23
View File
@@ -81,22 +81,27 @@ pub fn router(state: Shared) -> Router {
} }
/// Whether a browser WebSocket handshake came from the page's own /// Whether a browser WebSocket handshake came from the page's own
/// origin. The request carries the Host the browser used, so this works /// origin. The request carries the Host the browser used, so the check
/// behind a proxy and on any LAN address without trusting `public_url`. /// works behind a proxy and on any LAN address without relying on
/// A client that sends no `Origin` is not a browser page and is left to /// `public_url` alone. `public_url` is still accepted for a proxy that
/// the cookie check. /// rewrites Host to its upstream. A client that sends no `Origin` is
pub(crate) fn websocket_origin_ok(headers: &HeaderMap) -> bool { /// 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 let Some(origin) = headers
.get(header::ORIGIN) .get(header::ORIGIN)
.and_then(|value| value.to_str().ok()) .and_then(|value| value.to_str().ok())
else { else {
return true; 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 let Some(host) = headers
.get(header::HOST) .get(header::HOST)
.and_then(|value| value.to_str().ok()) .and_then(|value| value.to_str().ok())
else { else {
return true; return false;
}; };
let origin_authority = origin let origin_authority = origin
.split_once("://") .split_once("://")
@@ -244,6 +249,12 @@ mod tests {
use super::*; use super::*;
use axum::http::{HeaderMap, HeaderValue, header}; 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 { fn headers(origin: &str, host: &str) -> HeaderMap {
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert(header::ORIGIN, HeaderValue::from_str(origin).unwrap()); headers.insert(header::ORIGIN, HeaderValue::from_str(origin).unwrap());
@@ -253,32 +264,40 @@ mod tests {
#[test] #[test]
fn websocket_origin_matches_the_request_host() { fn websocket_origin_matches_the_request_host() {
assert!(websocket_origin_ok(&headers( assert!(websocket_origin_ok(
"http://10.4.1.222:7800", &config(),
"10.4.1.222:7800" &headers("http://10.4.1.222:7800", "10.4.1.222:7800")
))); ));
assert!(websocket_origin_ok(&headers( assert!(websocket_origin_ok(
"https://together.example.com", &config(),
"together.example.com" &headers("https://together.example.com", "together.example.com")
))); ));
assert!(websocket_origin_ok(&headers( assert!(websocket_origin_ok(
"http://localhost:7800", &config(),
"localhost:7800" &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] #[test]
fn websocket_origin_rejects_another_host() { fn websocket_origin_rejects_another_host() {
assert!(!websocket_origin_ok(&headers( assert!(!websocket_origin_ok(
"http://evil.example", &config(),
"10.4.1.222:7800" &headers("http://evil.example", "10.4.1.222:7800")
))); ));
} }
#[test] #[test]
fn a_missing_origin_is_left_to_the_cookie() { fn a_missing_origin_is_left_to_the_cookie() {
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert(header::HOST, HeaderValue::from_static("localhost:7800")); headers.insert(header::HOST, HeaderValue::from_static("localhost:7800"));
assert!(websocket_origin_ok(&headers)); assert!(websocket_origin_ok(&config(), &headers));
} }
} }