forge
Explore
Log in

Don't you dare go Hollow

im: session address goes through the family's client_label

Browse files at this commit

84318dc27a7050d2e1f7da1a4ca6190a4d544236Ahmet Kaan GümüşParent: e61e812
One client-label helper in server.rs serves every app; session_meta folds its unknown back to none so the sessions list keeps its dash. The session_meta_ip tests move beside the helper as client_label_tests.
crates/im-web/src/auth.rsmodified+5 -68
@@ -89,13 +89,13 @@ fn url_origin(raw: &str) -> Option<String> {
89 }89 }
90 90
91 /// Creation-time facts for the session row: the address the browser came91 /// Creation-time facts for the session row: the address the browser came
92-/// through and the agent it claims to be. The address is `client_ip` under92+/// through and the agent it claims to be. The address is the family's shared
93-/// the router's one trusted hop: the address that hop appended, or the peer93+/// [`server::client_label`], with `unknown` folded back to none so the
94-/// on a direct connection. A forwarding header a client supplies itself is94+/// sessions list keeps its dash.
95-/// never read.
96 fn session_meta(cx: &Cx) -> im_core::sessions::SessionMeta {95 fn session_meta(cx: &Cx) -> im_core::sessions::SessionMeta {
97 let headers = topcoat::router::request::headers(cx);96 let headers = topcoat::router::request::headers(cx);
98- let ip = topcoat::router::request::client_ip(cx).map(|ip| ip.to_string());97+ let label = server::client_label(cx);
98+ let ip = (label != "unknown").then_some(label);
99 let agent = headers99 let agent = headers
100 .get("user-agent")100 .get("user-agent")
101 .and_then(|value| value.to_str().ok())101 .and_then(|value| value.to_str().ok())
@@ -739,67 +739,4 @@ mod tests {
739 // The local-path rule stands beside the origin rule.739 // The local-path rule stands beside the origin rule.
740 assert_eq!(logout_target(Some("/\\in.example"), &services), "/");740 assert_eq!(logout_target(Some("/\\in.example"), &services), "/");
741 }741 }
742-
743- mod session_meta_ip {
744- use std::net::SocketAddr;
745-
746- use topcoat::context::Cx;
747- use topcoat::router::response::IntoResponse as _;
748- use topcoat::router::{
749- Body, Method, RemoteAddr, RouteFn, RouteFuture, Router, request::Request, to_bytes,
750- };
751-
752- use super::session_meta;
753- use crate::server::trusted_proxies;
754-
755- fn echo(cx: &Cx, _body: Body) -> RouteFuture<'_> {
756- Box::pin(async move {
757- session_meta(cx)
758- .ip
759- .unwrap_or_else(|| "none".to_string())
760- .into_response(cx)
761- })
762- }
763-
764- fn router(trust_one_hop: bool) -> Router {
765- let builder = Router::builder().route(RouteFn::new(Method::GET, "/meta", echo));
766- if trust_one_hop {
767- builder.trusted_proxies(trusted_proxies()).build()
768- } else {
769- builder.build()
770- }
771- }
772-
773- async fn ip(trust_one_hop: bool, peer: &str, forwarded: Option<&str>) -> String {
774- let mut builder = Request::builder()
775- .method(Method::GET)
776- .uri("/meta")
777- .extension(RemoteAddr(peer.parse::<SocketAddr>().unwrap()));
778- if let Some(value) = forwarded {
779- builder = builder.header("x-forwarded-for", value);
780- }
781- let response = router(trust_one_hop)
782- .handle(builder.body(Body::empty()).unwrap())
783- .await;
784- String::from_utf8(to_bytes(response.into_body(), 64).await.unwrap().to_vec()).unwrap()
785- }
786-
787- #[tokio::test]
788- async fn one_trusted_hop_reports_the_client_not_the_proxy() {
789- let got = ip(true, "10.0.0.1:4242", Some("198.51.100.1")).await;
790- assert_eq!(got, "198.51.100.1");
791- }
792-
793- #[tokio::test]
794- async fn a_missing_header_falls_back_to_the_peer() {
795- let got = ip(true, "203.0.113.9:4242", None).await;
796- assert_eq!(got, "203.0.113.9");
797- }
798-
799- #[tokio::test]
800- async fn an_untrusted_peer_cannot_spoof_the_forwarded_header() {
801- let got = ip(false, "203.0.113.9:4242", Some("198.51.100.1")).await;
802- assert_eq!(got, "203.0.113.9");
803- }
804- }
805 }742 }
crates/im-web/src/server.rsmodified+71 -0
@@ -16,6 +16,7 @@ use im_core::model::User;
16 use im_core::store::Store;16 use im_core::store::Store;
17 use topcoat::context::{Cx, try_app_context};17 use topcoat::context::{Cx, try_app_context};
18 use topcoat::cookie::{Cookie, Cookies, cookie, cookies};18 use topcoat::cookie::{Cookie, Cookies, cookie, cookies};
19+use topcoat::router::request::client_ip;
19 20
20 use crate::config::Config;21 use crate::config::Config;
21 22
@@ -32,6 +33,19 @@ pub const PENDING_MINUTES: i64 = 10;
32 pub fn trusted_proxies() -> topcoat::router::TrustedProxies {33 pub fn trusted_proxies() -> topcoat::router::TrustedProxies {
33 topcoat::router::TrustedProxies::new().nearest(1)34 topcoat::router::TrustedProxies::new().nearest(1)
34 }35 }
36+
37+/// A stable-enough label for the client.
38+///
39+/// [`client_ip`] under [`trusted_proxies`]: the address the one trusted hop
40+/// reported, or the peer when the request did not come through it. `unknown`
41+/// when neither is there. A client-supplied `x-forwarded-for` is not read
42+/// unless that hop appended it.
43+pub fn client_label(cx: &Cx) -> String {
44+ client_ip(cx)
45+ .map(|ip| ip.to_string())
46+ .unwrap_or_else(|| "unknown".to_string())
47+}
48+
35 pub struct App {49 pub struct App {
36 pub store: Arc<Store>,50 pub store: Arc<Store>,
37 pub config: Config,51 pub config: Config,
@@ -288,3 +302,60 @@ pub async fn app_client(cx: &Cx) -> Option<String> {
288 .ok()??;302 .ok()??;
289 im_core::oidc::verify_client_secret(&client, secret).then(|| client_id.to_string())303 im_core::oidc::verify_client_secret(&client, secret).then(|| client_id.to_string())
290 }304 }
305+
306+#[cfg(test)]
307+mod client_label_tests {
308+ use std::net::SocketAddr;
309+
310+ use topcoat::router::response::IntoResponse;
311+ use topcoat::router::{
312+ Body, Method, RemoteAddr, RouteFn, RouteFuture, Router, request::Request, to_bytes,
313+ };
314+
315+ use super::{client_label, trusted_proxies};
316+
317+ fn echo(cx: &topcoat::context::Cx, _body: Body) -> RouteFuture<'_> {
318+ Box::pin(async move { client_label(cx).into_response(cx) })
319+ }
320+
321+ fn router(trust_one_hop: bool) -> Router {
322+ let builder = Router::builder().route(RouteFn::new(Method::GET, "/label", echo));
323+ if trust_one_hop {
324+ builder.trusted_proxies(trusted_proxies()).build()
325+ } else {
326+ builder.build()
327+ }
328+ }
329+
330+ async fn label(trust_one_hop: bool, peer: &str, forwarded: Option<&str>) -> String {
331+ let mut builder = Request::builder()
332+ .method(Method::GET)
333+ .uri("/label")
334+ .extension(RemoteAddr(peer.parse::<SocketAddr>().unwrap()));
335+ if let Some(value) = forwarded {
336+ builder = builder.header("x-forwarded-for", value);
337+ }
338+ let response = router(trust_one_hop)
339+ .handle(builder.body(Body::empty()).unwrap())
340+ .await;
341+ String::from_utf8(to_bytes(response.into_body(), 64).await.unwrap().to_vec()).unwrap()
342+ }
343+
344+ #[tokio::test]
345+ async fn one_trusted_hop_reports_the_client_not_the_proxy() {
346+ let got = label(true, "10.0.0.1:4242", Some("198.51.100.1")).await;
347+ assert_eq!(got, "198.51.100.1");
348+ }
349+
350+ #[tokio::test]
351+ async fn a_missing_header_falls_back_to_the_peer() {
352+ let got = label(true, "203.0.113.9:4242", None).await;
353+ assert_eq!(got, "203.0.113.9");
354+ }
355+
356+ #[tokio::test]
357+ async fn an_untrusted_peer_cannot_spoof_the_forwarded_header() {
358+ let got = label(false, "203.0.113.9:4242", Some("198.51.100.1")).await;
359+ assert_eq!(got, "203.0.113.9");
360+ }
361+}