fix: accept terminal sockets from the page host
The origin check compared the browser's Origin with the configured public_url. Reaching the app through localhost or another LAN address then rejected the terminal and presence sockets even though the page and the socket were on the same origin. Compare the Origin authority with the request's Host header instead. That is the address the browser actually used, so the check still refuses another site while working behind a proxy and on every local address.
This commit is contained in:
1 parent
d7aa268ae0
commit
be04a6cf90
3 files changed
+61
-12
No files matched your search
+1
-1
@@ -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(&state.config, &headers) {
|
if !super::websocket_origin_ok(&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
@@ -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(&state.config, &headers) {
|
if !super::websocket_origin_ok(&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)?;
|
||||||
|
|||||||
+59
-10
@@ -80,25 +80,30 @@ pub fn router(state: Shared) -> Router {
|
|||||||
.with_state(state)
|
.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
|
/// A client that sends no `Origin` is not a browser page and is left to
|
||||||
/// the cookie check.
|
/// 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
|
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('/');
|
let Some(host) = headers
|
||||||
let public = config.public_url.trim_end_matches('/');
|
.get(header::HOST)
|
||||||
if origin == public {
|
.and_then(|value| value.to_str().ok())
|
||||||
|
else {
|
||||||
return true;
|
return true;
|
||||||
}
|
};
|
||||||
// The Nuxt dev server proxies the sockets from port 3000 while the
|
let origin_authority = origin
|
||||||
// API keeps its own port. Both are loopback, so accept that pair.
|
.split_once("://")
|
||||||
let loopback = public.starts_with("http://localhost") || public.starts_with("http://127.0.0.1");
|
.map(|(_, rest)| rest)
|
||||||
loopback && (origin.starts_with("http://localhost:") || origin.starts_with("http://127.0.0.1:"))
|
.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.
|
/// Rejects a mutating API request without the matching CSRF header.
|
||||||
@@ -233,3 +238,47 @@ async fn spa(State(state): State<Shared>, uri: Uri) -> Response {
|
|||||||
.body(Body::from(bytes))
|
.body(Body::from(bytes))
|
||||||
.unwrap_or_else(|_| (StatusCode::INTERNAL_SERVER_ERROR, "error").into_response())
|
.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));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user