diff --git a/crates/iz-web/src/main.rs b/crates/iz-web/src/main.rs index f8466fe..01d8e06 100644 --- a/crates/iz-web/src/main.rs +++ b/crates/iz-web/src/main.rs @@ -219,7 +219,8 @@ async fn main() { .at("/files"), ) .cookies() - .assets(bundle), + .assets(bundle) + .trusted_proxies(iz_web::server::trusted_proxies()), oidc, ) .app_context(store.clone()) diff --git a/crates/iz-web/src/server.rs b/crates/iz-web/src/server.rs index 4b586b5..1744167 100644 --- a/crates/iz-web/src/server.rs +++ b/crates/iz-web/src/server.rs @@ -19,7 +19,7 @@ use sha2::Digest; use topcoat::asset::AssetBundle; use topcoat::context::{Cx, app_context, memoize, try_app_context}; use topcoat::cookie::{Cookies, cookies}; -use topcoat::router::request::headers; +use topcoat::router::request::{client_ip, headers}; use topcoat::router::{ route, Body, HeaderValue, Next, StatusCode, header, response::IntoResponse, response::Response, to_bytes, @@ -35,6 +35,25 @@ pub fn config(cx: &Cx) -> iz_core::Config { app_context::(cx).clone() } +/// One proxy hop in front of the process. `iz` is reached through that hop, +/// so [`client_ip`] reads the address it appended rather than the hop's own. +/// A direct connection, with no hop in front, is its own address. +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()) +} + /// The app-level `setting` key the background beat in `main.rs` stores /// im's family list under, and `layout.rs`'s switcher renders from. The /// value is a JSON array of `{key, name, url}` — exactly what im's @@ -1051,3 +1070,60 @@ mod call_id_tests { assert_eq!(call_id("/api/link_tasks?x=1"), "link_tasksx1"); } } + +#[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"); + } +} diff --git a/crates/iz-web/tests/http.rs b/crates/iz-web/tests/http.rs index 278491c..b1a8644 100644 --- a/crates/iz-web/tests/http.rs +++ b/crates/iz-web/tests/http.rs @@ -370,7 +370,8 @@ impl App { .assets( AssetBundle::load_dir(asset_dir()) .expect("run `topcoat asset bundle` before the http suite"), - ), + ) + .trusted_proxies(iz_web::server::trusted_proxies()), client.clone(), ) .app_context(store.clone()) @@ -467,7 +468,8 @@ impl App { .assets( AssetBundle::load_dir(asset_dir()) .expect("run `topcoat asset bundle` before the http suite"), - ), + ) + .trusted_proxies(iz_web::server::trusted_proxies()), client.clone(), ) .app_context(store.clone())