forge
Explore
Log in

Don't you dare go Hollow

iz: trust one proxy hop, carry the family's client_label

Browse files at this commit

4c154b40b17df88ac0ac8234000f3e476186e150Ahmet Kaan GümüşParent: c96d861
Same helper pair as im and in: trusted_proxies(nearest 1) on the production router, client_label beside it, and the three shared tests. iz reads no client address today; the helper is the family contract, ready when one does.
crates/iz-web/src/main.rsmodified+2 -1
@@ -219,7 +219,8 @@ async fn main() {
219219 .at("/files"),
220220 )
221221 .cookies()
222- .assets(bundle),
222+ .assets(bundle)
223+ .trusted_proxies(iz_web::server::trusted_proxies()),
223224 oidc,
224225 )
225226 .app_context(store.clone())
crates/iz-web/src/server.rsmodified+77 -1
@@ -19,7 +19,7 @@ use sha2::Digest;
1919 use topcoat::asset::AssetBundle;
2020 use topcoat::context::{Cx, app_context, memoize, try_app_context};
2121 use topcoat::cookie::{Cookies, cookies};
22-use topcoat::router::request::headers;
22+use topcoat::router::request::{client_ip, headers};
2323 use topcoat::router::{
2424 route, Body, HeaderValue, Next, StatusCode, header, response::IntoResponse, response::Response,
2525 to_bytes,
@@ -35,6 +35,25 @@ pub fn config(cx: &Cx) -> iz_core::Config {
3535 app_context::<iz_core::Config>(cx).clone()
3636 }
3737
38+/// One proxy hop in front of the process. `iz` is reached through that hop,
39+/// so [`client_ip`] reads the address it appended rather than the hop's own.
40+/// A direct connection, with no hop in front, is its own address.
41+pub fn trusted_proxies() -> topcoat::router::TrustedProxies {
42+ topcoat::router::TrustedProxies::new().nearest(1)
43+}
44+
45+/// A stable-enough label for the client.
46+///
47+/// [`client_ip`] under [`trusted_proxies`]: the address the one trusted hop
48+/// reported, or the peer when the request did not come through it. `unknown`
49+/// when neither is there. A client-supplied `x-forwarded-for` is not read
50+/// unless that hop appended it.
51+pub fn client_label(cx: &Cx) -> String {
52+ client_ip(cx)
53+ .map(|ip| ip.to_string())
54+ .unwrap_or_else(|| "unknown".to_string())
55+}
56+
3857 /// The app-level `setting` key the background beat in `main.rs` stores
3958 /// im's family list under, and `layout.rs`'s switcher renders from. The
4059 /// value is a JSON array of `{key, name, url}` — exactly what im's
@@ -1051,3 +1070,60 @@ mod call_id_tests {
10511070 assert_eq!(call_id("/api/link_tasks?x=1"), "link_tasksx1");
10521071 }
10531072 }
1073+
1074+#[cfg(test)]
1075+mod client_label_tests {
1076+ use std::net::SocketAddr;
1077+
1078+ use topcoat::router::response::IntoResponse;
1079+ use topcoat::router::{
1080+ Body, Method, RemoteAddr, RouteFn, RouteFuture, Router, request::Request, to_bytes,
1081+ };
1082+
1083+ use super::{client_label, trusted_proxies};
1084+
1085+ fn echo(cx: &topcoat::context::Cx, _body: Body) -> RouteFuture<'_> {
1086+ Box::pin(async move { client_label(cx).into_response(cx) })
1087+ }
1088+
1089+ fn router(trust_one_hop: bool) -> Router {
1090+ let builder = Router::builder().route(RouteFn::new(Method::GET, "/label", echo));
1091+ if trust_one_hop {
1092+ builder.trusted_proxies(trusted_proxies()).build()
1093+ } else {
1094+ builder.build()
1095+ }
1096+ }
1097+
1098+ async fn label(trust_one_hop: bool, peer: &str, forwarded: Option<&str>) -> String {
1099+ let mut builder = Request::builder()
1100+ .method(Method::GET)
1101+ .uri("/label")
1102+ .extension(RemoteAddr(peer.parse::<SocketAddr>().unwrap()));
1103+ if let Some(value) = forwarded {
1104+ builder = builder.header("x-forwarded-for", value);
1105+ }
1106+ let response = router(trust_one_hop)
1107+ .handle(builder.body(Body::empty()).unwrap())
1108+ .await;
1109+ String::from_utf8(to_bytes(response.into_body(), 64).await.unwrap().to_vec()).unwrap()
1110+ }
1111+
1112+ #[tokio::test]
1113+ async fn one_trusted_hop_reports_the_client_not_the_proxy() {
1114+ let got = label(true, "10.0.0.1:4242", Some("198.51.100.1")).await;
1115+ assert_eq!(got, "198.51.100.1");
1116+ }
1117+
1118+ #[tokio::test]
1119+ async fn a_missing_header_falls_back_to_the_peer() {
1120+ let got = label(true, "203.0.113.9:4242", None).await;
1121+ assert_eq!(got, "203.0.113.9");
1122+ }
1123+
1124+ #[tokio::test]
1125+ async fn an_untrusted_peer_cannot_spoof_the_forwarded_header() {
1126+ let got = label(false, "203.0.113.9:4242", Some("198.51.100.1")).await;
1127+ assert_eq!(got, "203.0.113.9");
1128+ }
1129+}
crates/iz-web/tests/http.rsmodified+4 -2
@@ -370,7 +370,8 @@ impl App {
370370 .assets(
371371 AssetBundle::load_dir(asset_dir())
372372 .expect("run `topcoat asset bundle` before the http suite"),
373- ),
373+ )
374+ .trusted_proxies(iz_web::server::trusted_proxies()),
374375 client.clone(),
375376 )
376377 .app_context(store.clone())
@@ -467,7 +468,8 @@ impl App {
467468 .assets(
468469 AssetBundle::load_dir(asset_dir())
469470 .expect("run `topcoat asset bundle` before the http suite"),
470- ),
471+ )
472+ .trusted_proxies(iz_web::server::trusted_proxies()),
471473 client.clone(),
472474 )
473475 .app_context(store.clone())