diff --git a/crates/im-web/src/auth.rs b/crates/im-web/src/auth.rs index 32523d9..582dd14 100644 --- a/crates/im-web/src/auth.rs +++ b/crates/im-web/src/auth.rs @@ -89,13 +89,13 @@ fn url_origin(raw: &str) -> Option { } /// Creation-time facts for the session row: the address the browser came -/// through and the agent it claims to be. The address is `client_ip` under -/// the router's one trusted hop: the address that hop appended, or the peer -/// on a direct connection. A forwarding header a client supplies itself is -/// never read. +/// through and the agent it claims to be. The address is the family's shared +/// [`server::client_label`], with `unknown` folded back to none so the +/// sessions list keeps its dash. fn session_meta(cx: &Cx) -> im_core::sessions::SessionMeta { let headers = topcoat::router::request::headers(cx); - let ip = topcoat::router::request::client_ip(cx).map(|ip| ip.to_string()); + let label = server::client_label(cx); + let ip = (label != "unknown").then_some(label); let agent = headers .get("user-agent") .and_then(|value| value.to_str().ok()) @@ -739,67 +739,4 @@ mod tests { // The local-path rule stands beside the origin rule. assert_eq!(logout_target(Some("/\\in.example"), &services), "/"); } - - mod session_meta_ip { - use std::net::SocketAddr; - - use topcoat::context::Cx; - use topcoat::router::response::IntoResponse as _; - use topcoat::router::{ - Body, Method, RemoteAddr, RouteFn, RouteFuture, Router, request::Request, to_bytes, - }; - - use super::session_meta; - use crate::server::trusted_proxies; - - fn echo(cx: &Cx, _body: Body) -> RouteFuture<'_> { - Box::pin(async move { - session_meta(cx) - .ip - .unwrap_or_else(|| "none".to_string()) - .into_response(cx) - }) - } - - fn router(trust_one_hop: bool) -> Router { - let builder = Router::builder().route(RouteFn::new(Method::GET, "/meta", echo)); - if trust_one_hop { - builder.trusted_proxies(trusted_proxies()).build() - } else { - builder.build() - } - } - - async fn ip(trust_one_hop: bool, peer: &str, forwarded: Option<&str>) -> String { - let mut builder = Request::builder() - .method(Method::GET) - .uri("/meta") - .extension(RemoteAddr(peer.parse::().unwrap())); - if let Some(value) = forwarded { - builder = builder.header("x-forwarded-for", value); - } - let response = router(trust_one_hop) - .handle(builder.body(Body::empty()).unwrap()) - .await; - String::from_utf8(to_bytes(response.into_body(), 64).await.unwrap().to_vec()).unwrap() - } - - #[tokio::test] - async fn one_trusted_hop_reports_the_client_not_the_proxy() { - let got = ip(true, "10.0.0.1:4242", Some("198.51.100.1")).await; - assert_eq!(got, "198.51.100.1"); - } - - #[tokio::test] - async fn a_missing_header_falls_back_to_the_peer() { - let got = ip(true, "203.0.113.9:4242", None).await; - assert_eq!(got, "203.0.113.9"); - } - - #[tokio::test] - async fn an_untrusted_peer_cannot_spoof_the_forwarded_header() { - let got = ip(false, "203.0.113.9:4242", Some("198.51.100.1")).await; - assert_eq!(got, "203.0.113.9"); - } - } } diff --git a/crates/im-web/src/server.rs b/crates/im-web/src/server.rs index 8e59d41..b6ac470 100644 --- a/crates/im-web/src/server.rs +++ b/crates/im-web/src/server.rs @@ -16,6 +16,7 @@ use im_core::model::User; use im_core::store::Store; use topcoat::context::{Cx, try_app_context}; use topcoat::cookie::{Cookie, Cookies, cookie, cookies}; +use topcoat::router::request::client_ip; use crate::config::Config; @@ -32,6 +33,19 @@ pub const PENDING_MINUTES: i64 = 10; pub fn trusted_proxies() -> topcoat::router::TrustedProxies { topcoat::router::TrustedProxies::new().nearest(1) } + +/// A stable-enough label for the client. +/// +/// [`client_ip`] under [`trusted_proxies`]: the address the one trusted hop +/// reported, or the peer when the request did not come through it. `unknown` +/// when neither is there. A client-supplied `x-forwarded-for` is not read +/// unless that hop appended it. +pub fn client_label(cx: &Cx) -> String { + client_ip(cx) + .map(|ip| ip.to_string()) + .unwrap_or_else(|| "unknown".to_string()) +} + pub struct App { pub store: Arc, pub config: Config, @@ -288,3 +302,60 @@ pub async fn app_client(cx: &Cx) -> Option { .ok()??; im_core::oidc::verify_client_secret(&client, secret).then(|| client_id.to_string()) } + +#[cfg(test)] +mod client_label_tests { + use std::net::SocketAddr; + + use topcoat::router::response::IntoResponse; + use topcoat::router::{ + Body, Method, RemoteAddr, RouteFn, RouteFuture, Router, request::Request, to_bytes, + }; + + use super::{client_label, trusted_proxies}; + + fn echo(cx: &topcoat::context::Cx, _body: Body) -> RouteFuture<'_> { + Box::pin(async move { client_label(cx).into_response(cx) }) + } + + fn router(trust_one_hop: bool) -> Router { + let builder = Router::builder().route(RouteFn::new(Method::GET, "/label", echo)); + if trust_one_hop { + builder.trusted_proxies(trusted_proxies()).build() + } else { + builder.build() + } + } + + async fn label(trust_one_hop: bool, peer: &str, forwarded: Option<&str>) -> String { + let mut builder = Request::builder() + .method(Method::GET) + .uri("/label") + .extension(RemoteAddr(peer.parse::().unwrap())); + if let Some(value) = forwarded { + builder = builder.header("x-forwarded-for", value); + } + let response = router(trust_one_hop) + .handle(builder.body(Body::empty()).unwrap()) + .await; + String::from_utf8(to_bytes(response.into_body(), 64).await.unwrap().to_vec()).unwrap() + } + + #[tokio::test] + async fn one_trusted_hop_reports_the_client_not_the_proxy() { + let got = label(true, "10.0.0.1:4242", Some("198.51.100.1")).await; + assert_eq!(got, "198.51.100.1"); + } + + #[tokio::test] + async fn a_missing_header_falls_back_to_the_peer() { + let got = label(true, "203.0.113.9:4242", None).await; + assert_eq!(got, "203.0.113.9"); + } + + #[tokio::test] + async fn an_untrusted_peer_cannot_spoof_the_forwarded_header() { + let got = label(false, "203.0.113.9:4242", Some("198.51.100.1")).await; + assert_eq!(got, "203.0.113.9"); + } +}