@@ -439,8 +439,13 @@ impl PeerDiscovery { |
439 | 439 | /// [`STALE_THRESHOLD`] relative to the local wall clock is |
440 | 440 | /// refused: it neither inserts a peer nor refreshes one, so |
441 | 441 | /// replaying a captured announcement cannot keep a departed peer |
442 | | - /// live. (A stamp in the future counts as fresh; the ordering |
443 | | - /// check still governs whether it may replace stored state.) |
| 442 | + /// live. The bound is symmetric: a stamp more than |
| 443 | + /// [`STALE_THRESHOLD`] in the future is also refused, so a forged |
| 444 | + /// far-future stamp (e.g. `u64::MAX`) can never enter the table |
| 445 | + /// and poison the ordering watermark against every later genuine |
| 446 | + /// announcement. Stamps within the bound — past or future — |
| 447 | + /// count as fresh; the ordering check then governs whether they |
| 448 | + /// may replace stored state. |
444 | 449 | /// - **Ordering.** An announcement older than the stored one for |
445 | 450 | /// the same node is dropped: out-of-order delivery never |
446 | 451 | /// regresses the table. Equal timestamps are a tie, broken |
@@ -452,7 +457,9 @@ impl PeerDiscovery { |
452 | 457 | let now_secs = SystemTime::now() |
453 | 458 | .duration_since(UNIX_EPOCH) |
454 | 459 | .map_or(0, |d| d.as_secs()); |
455 | | - if now_secs.saturating_sub(announcement.timestamp_secs) > STALE_THRESHOLD.as_secs() { |
| 460 | + if now_secs.saturating_sub(announcement.timestamp_secs) > STALE_THRESHOLD.as_secs() |
| 461 | + || announcement.timestamp_secs.saturating_sub(now_secs) > STALE_THRESHOLD.as_secs() |
| 462 | + { |
456 | 463 | return; |
457 | 464 | } |
458 | 465 | let node_id = announcement.node_id; |
@@ -646,6 +653,48 @@ mod tests { |
646 | 653 | assert_eq!(peers[0].announcement.blob_count, 3); |
647 | 654 | } |
648 | 655 |
|
| 656 | + #[test] |
| 657 | + fn test_far_future_announcement_is_refused_and_cannot_poison_ordering() { |
| 658 | + let discovery = PeerDiscovery::new(); |
| 659 | + // A forged stamp at the end of time never enters the table. |
| 660 | + discovery.update(make_announcement_at(1, Some("forged"), 99, u64::MAX)); |
| 661 | + assert_eq!(discovery.count(), 0); |
| 662 | + assert!(discovery.peers().is_empty()); |
| 663 | + |
| 664 | + // Nor does a far-future stamp overwrite a live peer's stored |
| 665 | + // state, and the peer's genuine announcements keep updating it |
| 666 | + // afterwards (no ordering-watermark lockout). |
| 667 | + let now = now_secs(); |
| 668 | + discovery.update(make_announcement_at(2, Some("live"), 3, now)); |
| 669 | + discovery.update(make_announcement_at( |
| 670 | + 2, |
| 671 | + Some("forged"), |
| 672 | + 99, |
| 673 | + now + STALE_THRESHOLD.as_secs() + 1, |
| 674 | + )); |
| 675 | + discovery.update(make_announcement_at(2, Some("live-2"), 4, now)); |
| 676 | + let peers = discovery.peers(); |
| 677 | + assert_eq!(peers.len(), 1); |
| 678 | + assert_eq!(peers[0].announcement.name, Some("live-2".to_owned())); |
| 679 | + assert_eq!(peers[0].announcement.blob_count, 4); |
| 680 | + assert_eq!(peers[0].announcement.timestamp_secs, now); |
| 681 | + } |
| 682 | + |
| 683 | + #[test] |
| 684 | + fn test_near_future_announcement_within_skew_bound_is_accepted() { |
| 685 | + let discovery = PeerDiscovery::new(); |
| 686 | + let now = now_secs(); |
| 687 | + discovery.update(make_announcement_at( |
| 688 | + 1, |
| 689 | + Some("skewed"), |
| 690 | + 5, |
| 691 | + now + STALE_THRESHOLD.as_secs(), |
| 692 | + )); |
| 693 | + let peers = discovery.peers(); |
| 694 | + assert_eq!(peers.len(), 1); |
| 695 | + assert_eq!(peers[0].announcement.name, Some("skewed".to_owned())); |
| 696 | + } |
| 697 | + |
649 | 698 | #[test] |
650 | 699 | fn test_announcement_serialization_roundtrip() { |
651 | 700 | let announcement = make_announcement(1, Some("test-peer"), 42); |
|