iz: trust one proxy hop, carry the family's client_label
Browse files at this commit4c154b40b17df88ac0ac8234000f3e476186e150Parent: c96d8613 changed files +83 -4
crates/iz-web/src/main.rsmodified+2 -1
| @@ -219,7 +219,8 @@ async fn main() { | ||
| 219 | 219 | .at("/files"), |
| 220 | 220 | ) |
| 221 | 221 | .cookies() |
| 222 | - .assets(bundle), | |
| 222 | + .assets(bundle) | |
| 223 | + .trusted_proxies(iz_web::server::trusted_proxies()), | |
| 223 | 224 | oidc, |
| 224 | 225 | ) |
| 225 | 226 | .app_context(store.clone()) |
crates/iz-web/src/server.rsmodified+77 -1
| @@ -19,7 +19,7 @@ use sha2::Digest; | ||
| 19 | 19 | use topcoat::asset::AssetBundle; |
| 20 | 20 | use topcoat::context::{Cx, app_context, memoize, try_app_context}; |
| 21 | 21 | use topcoat::cookie::{Cookies, cookies}; |
| 22 | -use topcoat::router::request::headers; | |
| 22 | +use topcoat::router::request::{client_ip, headers}; | |
| 23 | 23 | use topcoat::router::{ |
| 24 | 24 | route, Body, HeaderValue, Next, StatusCode, header, response::IntoResponse, response::Response, |
| 25 | 25 | to_bytes, |
| @@ -35,6 +35,25 @@ pub fn config(cx: &Cx) -> iz_core::Config { | ||
| 35 | 35 | app_context::<iz_core::Config>(cx).clone() |
| 36 | 36 | } |
| 37 | 37 | |
| 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 | + | |
| 38 | 57 | /// The app-level `setting` key the background beat in `main.rs` stores |
| 39 | 58 | /// im's family list under, and `layout.rs`'s switcher renders from. The |
| 40 | 59 | /// value is a JSON array of `{key, name, url}` — exactly what im's |
| @@ -1051,3 +1070,60 @@ mod call_id_tests { | ||
| 1051 | 1070 | assert_eq!(call_id("/api/link_tasks?x=1"), "link_tasksx1"); |
| 1052 | 1071 | } |
| 1053 | 1072 | } |
| 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 { | ||
| 370 | 370 | .assets( |
| 371 | 371 | AssetBundle::load_dir(asset_dir()) |
| 372 | 372 | .expect("run `topcoat asset bundle` before the http suite"), |
| 373 | - ), | |
| 373 | + ) | |
| 374 | + .trusted_proxies(iz_web::server::trusted_proxies()), | |
| 374 | 375 | client.clone(), |
| 375 | 376 | ) |
| 376 | 377 | .app_context(store.clone()) |
| @@ -467,7 +468,8 @@ impl App { | ||
| 467 | 468 | .assets( |
| 468 | 469 | AssetBundle::load_dir(asset_dir()) |
| 469 | 470 | .expect("run `topcoat asset bundle` before the http suite"), |
| 470 | - ), | |
| 471 | + ) | |
| 472 | + .trusted_proxies(iz_web::server::trusted_proxies()), | |
| 471 | 473 | client.clone(), |
| 472 | 474 | ) |
| 473 | 475 | .app_context(store.clone()) |