forked from vhaudiquet/rhema-together
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
+59
-10
@@ -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<Shared>, 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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user