diff --git a/pkgs/id/src/abuse.rs b/pkgs/id/src/abuse.rs index d38e95e8f4..a616826f41 100644 --- a/pkgs/id/src/abuse.rs +++ b/pkgs/id/src/abuse.rs @@ -700,7 +700,10 @@ impl AbuseEvaluator { /// Like [`AbuseEvaluator::new`], with the operator block list /// (#2245) attached: a new connection from an endpoint blocked by - /// key is refused with [`Refusal::blocked_by_operator`]. The list + /// key is refused with [`Refusal::blocked_by_operator`], and so is + /// a connection whose directly observed IP is blocked, when the + /// listener supplies one to + /// [`AbuseEvaluator::open_connection_with_direct_ip`]. The list /// is consulted live on every open, so a block or an unblock lands /// on the next connection. Frame and byte checks are unchanged — /// a block refuses new sessions; it does not retroactively charge @@ -982,6 +985,40 @@ impl AbuseEvaluator { &mut self, endpoint: EndpointId, now_ms: u64, + ) -> Result { + self.open_connection_inner(endpoint, None, now_ms) + } + + /// Like [`AbuseEvaluator::open_connection`], for a listener that + /// directly observed the connection's remote IP (#2245): when + /// `direct_ip` is `Some` and the attached block list blocks that + /// IP, the connection is refused with + /// [`Refusal::blocked_by_operator`] before any bucket or cap is + /// consulted — the listener-only, best-effort IP enforcement the + /// issue asks for. A relayed or otherwise obscured connection + /// passes `None` and is judged exactly as by + /// [`AbuseEvaluator::open_connection`]: an IP block never refuses + /// a peer whose IP was not directly observed. + /// + /// # Errors + /// + /// The same refusals as [`AbuseEvaluator::open_connection`], plus + /// a [`RefusalReason::BlockedByOperator`] refusal when `direct_ip` + /// names a blocked IP. + pub fn open_connection_with_direct_ip( + &mut self, + endpoint: EndpointId, + direct_ip: Option, + now_ms: u64, + ) -> Result { + self.open_connection_inner(endpoint, direct_ip, now_ms) + } + + fn open_connection_inner( + &mut self, + endpoint: EndpointId, + direct_ip: Option, + now_ms: u64, ) -> Result { if self .blocks @@ -990,6 +1027,11 @@ impl AbuseEvaluator { { return Err(Refusal::blocked_by_operator()); } + if let (Some(blocks), Some(ip)) = (&self.blocks, direct_ip) { + if blocks.ip_blocked(&ip) { + return Err(Refusal::blocked_by_operator()); + } + } let cap = self.policy.max_connections_per_peer; // Ids are unique and never reused; a refused open may skip // an id, which is harmless. @@ -1250,6 +1292,32 @@ mod tests { .expect("an unblocked endpoint still connects"); } + #[test] + fn a_blocked_direct_ip_is_refused_a_new_connection() { + // #2245 listener seam: with a block list attached, a connection + // whose directly observed IP is blocked is refused as an + // operator block — and no connection is registered. An + // unblocked direct IP still connects, and a relayed + // connection (no IP observed) is unaffected by the IP block. + let blocked_ip: IpAddr = "203.0.113.7".parse().expect("test ip"); + let other_ip: IpAddr = "203.0.113.8".parse().expect("test ip"); + let blocks = crate::pull_policy::testing::stub_blocks_with_ips(&[], &[], &[blocked_ip]); + let mut eval = evaluator().with_block_list(blocks); + let refused = eval + .open_connection_with_direct_ip(endpoint(7), Some(blocked_ip), 0) + .expect_err("a blocked direct IP was admitted"); + assert_eq!(refused, Refusal::blocked_by_operator()); + assert_eq!(refused.reason, RefusalReason::BlockedByOperator); + assert_eq!(refused.retry_after_ms, None); + assert_eq!(eval.active_connection_count(&endpoint(7)), 0); + eval.open_connection_with_direct_ip(endpoint(7), Some(other_ip), 0) + .expect("an unblocked direct IP still connects"); + eval.open_connection_with_direct_ip(endpoint(8), None, 0) + .expect("a relayed connection is unaffected by IP blocks"); + eval.open_connection(endpoint(9), 0) + .expect("the pre-#2245 entry point is unchanged"); + } + #[test] fn connection_cap_is_per_peer() { let mut eval = evaluator(); diff --git a/pkgs/id/src/log_sync.rs b/pkgs/id/src/log_sync.rs index 615f71d689..1586ede00d 100644 --- a/pkgs/id/src/log_sync.rs +++ b/pkgs/id/src/log_sync.rs @@ -1744,6 +1744,7 @@ mod tests { let blocks = std::sync::Arc::new(testing::StubBlocks { peers: Default::default(), topics: [topic.as_bytes().to_vec()].into_iter().collect(), + ips: Default::default(), }); let gate = testing::open_gate().with_blocks(blocks); let (server, _router, mut results) = responder_gated(&store_a, gate).await; diff --git a/pkgs/id/src/pull_policy.rs b/pkgs/id/src/pull_policy.rs index 1710af400f..e6260ea021 100644 --- a/pkgs/id/src/pull_policy.rs +++ b/pkgs/id/src/pull_policy.rs @@ -263,6 +263,9 @@ pub(crate) mod testing { pub peers: std::collections::HashSet<[u8; 32]>, /// Blocked topic tags, as raw bytes. pub topics: std::collections::HashSet>, + /// Blocked IPs, in the text form `BlockList::ip_blocked` + /// formats them in. + pub ips: std::collections::HashSet, } impl crate::event_store::BlockList for StubBlocks { @@ -272,7 +275,9 @@ pub(crate) mod testing { <[u8; 32]>::try_from(value).is_ok_and(|key| self.peers.contains(&key)) } crate::event_store::BlockKind::TopicTag => self.topics.iter().any(|t| t == value), - crate::event_store::BlockKind::Ip => false, + crate::event_store::BlockKind::Ip => { + std::str::from_utf8(value).is_ok_and(|text| self.ips.contains(text)) + } } } } @@ -282,6 +287,21 @@ pub(crate) mod testing { Arc::new(StubBlocks { peers: peers.iter().copied().collect(), topics: topics.iter().map(|t| t.as_bytes().to_vec()).collect(), + ips: std::collections::HashSet::new(), + }) + } + + /// A stub block list like [`stub_blocks`], additionally naming + /// `ips` as blocked. + pub(crate) fn stub_blocks_with_ips( + peers: &[[u8; 32]], + topics: &[&str], + ips: &[std::net::IpAddr], + ) -> Arc { + Arc::new(StubBlocks { + peers: peers.iter().copied().collect(), + topics: topics.iter().map(|t| t.as_bytes().to_vec()).collect(), + ips: ips.iter().map(|ip| ip.to_string()).collect(), }) } diff --git a/pkgs/id/src/world_net.rs b/pkgs/id/src/world_net.rs index 6ee44d1d69..0c62ec8fe8 100644 --- a/pkgs/id/src/world_net.rs +++ b/pkgs/id/src/world_net.rs @@ -214,6 +214,22 @@ impl Drop for PeerConnectionRegistration { } } +/// The remote IP of the connection's selected path, when that path +/// is a direct IP path (#2245). A connection whose selected path is +/// a relay or a custom transport has no peer IP an operator block +/// could name: it yields `None`, and IP blocks never apply to it. +/// Best-effort by construction — a direct path that opens after +/// admission is not re-judged, exactly as the issue scopes IP +/// enforcement to the listener. +fn direct_ip_of(conn: &Connection) -> Option { + let paths = conn.paths(); + let selected = paths.iter().find(|path| path.is_selected())?; + match selected.remote_addr() { + iroh::TransportAddr::Ip(addr) => Some(addr.ip()), + _ => None, + } +} + impl ProtocolHandler for WorldProtocol { async fn accept(&self, conn: Connection) -> Result<(), AcceptError> { // Bound stale endpoint and counter state as new connections arrive; @@ -223,8 +239,11 @@ impl ProtocolHandler for WorldProtocol { let remote = conn.remote_id(); tracing::debug!("world: connection from node {remote}"); let remote_endpoint: AbuseEndpointId = *remote.as_bytes(); + let direct_ip = direct_ip_of(&conn); let connection_id = match self.abuse.lock() { - Ok(mut abuse) => match abuse.open_connection(remote_endpoint, abuse_now_ms()) { + Ok(mut abuse) => match abuse + .open_connection_with_direct_ip(remote_endpoint, direct_ip, abuse_now_ms()) + { Ok(connection_id) => connection_id, Err(refusal) if refusal.reason == RefusalReason::BlockedByOperator => { // #2245: an operator block is not a cap refusal — it