diff --git a/pkgs/id/src/commands/peers.rs b/pkgs/id/src/commands/peers.rs index 6cd07d9a5e..bc603afc55 100644 --- a/pkgs/id/src/commands/peers.rs +++ b/pkgs/id/src/commands/peers.rs @@ -469,12 +469,21 @@ mod tests { use crate::discovery::PeerDiscovery; use iroh_base::SecretKey; + fn now_secs() -> u64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs() + } + fn announcement(byte: u8, name: Option<&str>) -> PeerAnnouncement { PeerAnnouncement { node_id: SecretKey::from_bytes(&[byte; 32]).public(), name: name.map(ToOwned::to_owned), blob_count: 7, - timestamp_secs: 1_700_000_000, + // Fresh stamp: PeerDiscovery::update refuses announcements + // older than STALE_THRESHOLD relative to the wall clock. + timestamp_secs: now_secs(), } } @@ -555,7 +564,7 @@ mod tests { ); } - let now = 1_700_000_120; + let now = now_secs() + 120; let mut rows: Vec = peers.iter().map(format_peer_row).collect(); rows.extend( peers diff --git a/pkgs/id/src/discovery.rs b/pkgs/id/src/discovery.rs index 2f46b6c34e..690ee51fa7 100644 --- a/pkgs/id/src/discovery.rs +++ b/pkgs/id/src/discovery.rs @@ -33,7 +33,7 @@ use std::collections::HashMap; use std::sync::{Arc, LazyLock, RwLock}; -use std::time::{Duration, Instant}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use iroh_base::EndpointId; use serde::{Deserialize, Serialize}; @@ -395,11 +395,17 @@ pub struct PeerInfo { /// let discovery = PeerDiscovery::new(); /// /// let node_id = SecretKey::from_bytes(&[1u8; 32]).public(); +/// // Announcements must be stamped near the local wall clock: `update` +/// // refuses stamps older than `STALE_THRESHOLD` (see its docs). +/// let timestamp_secs = std::time::SystemTime::now() +/// .duration_since(std::time::UNIX_EPOCH) +/// .unwrap() +/// .as_secs(); /// let announcement = PeerAnnouncement { /// node_id, /// name: Some("peer-1".to_string()), /// blob_count: 10, -/// timestamp_secs: 1700000000, +/// timestamp_secs, /// }; /// /// discovery.update(announcement); @@ -425,16 +431,51 @@ impl PeerDiscovery { /// Updates the peer table with a new announcement. /// - /// If the peer is already known, its announcement and last-seen - /// timestamp are updated. If the peer is new, it is inserted. + /// Two checks guard the table against stale gossip, so every caller + /// gets them (the `serve.rs` receive loop needs no trust logic of + /// its own): + /// + /// - **Freshness bound.** An announcement stamped older than + /// [`STALE_THRESHOLD`] relative to the local wall clock is + /// refused: it neither inserts a peer nor refreshes one, so + /// replaying a captured announcement cannot keep a departed peer + /// live. The bound is symmetric: a stamp more than + /// [`STALE_THRESHOLD`] in the future is also refused, so a forged + /// far-future stamp (e.g. `u64::MAX`) can never enter the table + /// and poison the ordering watermark against every later genuine + /// announcement. Stamps within the bound — past or future — + /// count as fresh; the ordering check then governs whether they + /// may replace stored state. + /// - **Ordering.** An announcement older than the stored one for + /// the same node is dropped: out-of-order delivery never + /// regresses the table. Equal timestamps are a tie, broken + /// deterministically — the most recent arrival wins. + /// + /// An announcement that passes both checks replaces the stored one + /// (or inserts a new peer), stamped with the local arrival time. pub fn update(&self, announcement: PeerAnnouncement) { + let now_secs = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |d| d.as_secs()); + if now_secs.saturating_sub(announcement.timestamp_secs) > STALE_THRESHOLD.as_secs() + || announcement.timestamp_secs.saturating_sub(now_secs) > STALE_THRESHOLD.as_secs() + { + return; + } let node_id = announcement.node_id; - let info = PeerInfo { - announcement, - last_seen: Instant::now(), - }; if let Ok(mut peers) = self.peers.write() { - peers.insert(node_id, info); + if let Some(existing) = peers.get(&node_id) + && announcement.timestamp_secs < existing.announcement.timestamp_secs + { + return; + } + peers.insert( + node_id, + PeerInfo { + announcement, + last_seen: Instant::now(), + }, + ); } } @@ -540,15 +581,120 @@ mod tests { secret.public() } + fn now_secs() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() + } + fn make_announcement(byte: u8, name: Option<&str>, blob_count: u64) -> PeerAnnouncement { + make_announcement_at(byte, name, blob_count, now_secs()) + } + + fn make_announcement_at( + byte: u8, + name: Option<&str>, + blob_count: u64, + timestamp_secs: u64, + ) -> PeerAnnouncement { PeerAnnouncement { node_id: make_node_id(byte), name: name.map(ToOwned::to_owned), blob_count, - timestamp_secs: 1_700_000_000, + timestamp_secs, } } + #[test] + fn test_out_of_order_announcement_does_not_overwrite_newer() { + let discovery = PeerDiscovery::new(); + let now = now_secs(); + discovery.update(make_announcement_at(1, Some("new-name"), 42, now)); + discovery.update(make_announcement_at(1, Some("old-name"), 7, now - 60)); + let peers = discovery.peers(); + assert_eq!(peers.len(), 1); + assert_eq!(peers[0].announcement.name, Some("new-name".to_owned())); + assert_eq!(peers[0].announcement.blob_count, 42); + assert_eq!(peers[0].announcement.timestamp_secs, now); + } + + #[test] + fn test_ancient_announcement_is_refused_and_not_fresh() { + let discovery = PeerDiscovery::new(); + // The probe's ghost case (#1552 case 3): stamped 1970. + discovery.update(make_announcement_at(1, Some("ghost"), 99, 1)); + assert_eq!(discovery.count(), 0); + assert!(discovery.peers().is_empty()); + + // Nor does an ancient replay touch a live peer's stored state. + let now = now_secs(); + discovery.update(make_announcement_at(2, Some("live"), 3, now)); + discovery.update(make_announcement_at(2, Some("ghost"), 99, now - 3600)); + let peers = discovery.peers(); + assert_eq!(peers.len(), 1); + assert_eq!(peers[0].announcement.name, Some("live".to_owned())); + assert_eq!(peers[0].announcement.blob_count, 3); + } + + #[test] + fn test_in_order_announcements_still_replace_and_ties_go_to_the_latest() { + let discovery = PeerDiscovery::new(); + let now = now_secs(); + discovery.update(make_announcement_at(1, Some("first"), 1, now - 10)); + discovery.update(make_announcement_at(1, Some("second"), 2, now)); + let peers = discovery.peers(); + assert_eq!(peers[0].announcement.name, Some("second".to_owned())); + assert_eq!(peers[0].announcement.blob_count, 2); + // Equal timestamps: the documented tie rule — most recent arrival wins. + discovery.update(make_announcement_at(1, Some("tie"), 3, now)); + let peers = discovery.peers(); + assert_eq!(peers[0].announcement.name, Some("tie".to_owned())); + assert_eq!(peers[0].announcement.blob_count, 3); + } + + #[test] + fn test_far_future_announcement_is_refused_and_cannot_poison_ordering() { + let discovery = PeerDiscovery::new(); + // A forged stamp at the end of time never enters the table. + discovery.update(make_announcement_at(1, Some("forged"), 99, u64::MAX)); + assert_eq!(discovery.count(), 0); + assert!(discovery.peers().is_empty()); + + // Nor does a far-future stamp overwrite a live peer's stored + // state, and the peer's genuine announcements keep updating it + // afterwards (no ordering-watermark lockout). + let now = now_secs(); + discovery.update(make_announcement_at(2, Some("live"), 3, now)); + discovery.update(make_announcement_at( + 2, + Some("forged"), + 99, + now + STALE_THRESHOLD.as_secs() + 1, + )); + discovery.update(make_announcement_at(2, Some("live-2"), 4, now)); + let peers = discovery.peers(); + assert_eq!(peers.len(), 1); + assert_eq!(peers[0].announcement.name, Some("live-2".to_owned())); + assert_eq!(peers[0].announcement.blob_count, 4); + assert_eq!(peers[0].announcement.timestamp_secs, now); + } + + #[test] + fn test_near_future_announcement_within_skew_bound_is_accepted() { + let discovery = PeerDiscovery::new(); + let now = now_secs(); + discovery.update(make_announcement_at( + 1, + Some("skewed"), + 5, + now + STALE_THRESHOLD.as_secs(), + )); + let peers = discovery.peers(); + assert_eq!(peers.len(), 1); + assert_eq!(peers[0].announcement.name, Some("skewed".to_owned())); + } + #[test] fn test_announcement_serialization_roundtrip() { let announcement = make_announcement(1, Some("test-peer"), 42);