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:
1 parent
be04a6cf90
commit
f21fdfd729
3 files changed
+44
-25
No files matched your search
+1
-1
@@ -237,7 +237,7 @@ pub async fn terminal(
|
||||
headers: HeaderMap,
|
||||
ws: WebSocketUpgrade,
|
||||
) -> 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"));
|
||||
}
|
||||
member_of(&state, &room, &user.user.id)?;
|
||||
|
||||
+1
-1
@@ -220,7 +220,7 @@ pub async fn presence(
|
||||
headers: HeaderMap,
|
||||
ws: WebSocketUpgrade,
|
||||
) -> 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"));
|
||||
}
|
||||
let member = member_of(&state, &room, &user.user.id)?;
|
||||
|
||||
+42
-23
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user