From b18e350dd38808d86a8568de0ee329279fbcbf52 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 18:38:11 -0600 Subject: [PATCH] fix(id): refuse negative peers --depth at parse and crawl entry (#2125) --- pkgs/id/src/cli.rs | 26 +++++++++++++++++++++++++- pkgs/id/src/commands/peers.rs | 28 +++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/pkgs/id/src/cli.rs b/pkgs/id/src/cli.rs index 4c0949a201..b095be6388 100644 --- a/pkgs/id/src/cli.rs +++ b/pkgs/id/src/cli.rs @@ -1270,7 +1270,7 @@ pub enum Command { /// /// 0 = no crawl, 1 = query direct peers (default), /// N = recursively query up to N levels deep. - #[arg(long, default_value = "1")] + #[arg(long, default_value = "1", allow_negative_numbers = true, value_parser = clap::value_parser!(i32).range(0..))] depth: i32, /// Maximum total peers to discover. /// @@ -2936,6 +2936,30 @@ mod tests { } } + #[test] + fn test_cli_peers_depth_rejects_negative_and_preserves_non_negative_values() { + for argv in [ + vec!["id", "peers", "--depth=-1"], + vec!["id", "peers", "--depth", "-1"], + ] { + let err = Cli::try_parse_from(argv).unwrap_err(); + assert!( + err.to_string().contains("depth"), + "error should name depth: {err}" + ); + } + + for depth in ["0", "1", "2"] { + let cli = Cli::try_parse_from(["id", "peers", "--depth", depth]).unwrap(); + match cli.command { + Some(Command::Peers { depth: parsed, .. }) => { + assert_eq!(parsed, depth.parse::().unwrap()); + } + _ => panic!("Expected Peers command"), + } + } + } + #[test] fn test_cli_parse_peers_with_node() { let node_id = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; diff --git a/pkgs/id/src/commands/peers.rs b/pkgs/id/src/commands/peers.rs index d72e9b757e..6f322d5e07 100644 --- a/pkgs/id/src/commands/peers.rs +++ b/pkgs/id/src/commands/peers.rs @@ -120,6 +120,13 @@ pub struct PeersOptions { /// tab, CR, LF or other structure-breaking character is quoted and escaped by /// [`plain_field`] instead of shifting a column or forging a row (#1613). pub async fn cmd_peers(node: Option, options: PeersOptions) -> Result<()> { + if options.depth < 0 { + bail!( + "peer crawl depth must be non-negative, got {}", + options.depth + ); + } + let mut all_peers: HashMap = HashMap::new(); // Determine mode @@ -160,7 +167,7 @@ pub async fn cmd_peers(node: Option, options: PeersOptions) -> Result<() options.no_relay, options.no_mdns, ) - .await; + .await?; for peer in crawled { all_peers.insert(peer.node_id, peer); } @@ -297,7 +304,11 @@ async fn crawl_peers( timeout_secs: u64, no_relay: bool, no_mdns: bool, -) -> Vec { +) -> Result> { + if depth < 0 { + bail!("peer crawl depth must be non-negative, got {depth}"); + } + let mut all_peers: HashMap = HashMap::new(); let mut visited: HashSet = HashSet::new(); let mut frontier: Vec = Vec::new(); @@ -309,10 +320,9 @@ async fn crawl_peers( } let mut current_depth = 0; - let max_depth = if depth < 0 { i32::MAX } else { depth }; let timeout = tokio::time::Duration::from_secs(timeout_secs); - while current_depth < max_depth && !frontier.is_empty() { + while current_depth < depth && !frontier.is_empty() { if all_peers.len() >= max_peers { tracing::warn!( "peers: max-peers cap ({}) reached at depth {}", @@ -362,7 +372,7 @@ async fn crawl_peers( current_depth += 1; } - all_peers.into_values().collect() + Ok(all_peers.into_values().collect()) } /// Discovers peers via gossip by joining the discovery topic. @@ -486,6 +496,14 @@ mod tests { use crate::discovery::PeerDiscovery; use iroh_base::SecretKey; + #[tokio::test] + async fn crawl_peers_refuses_negative_depth_before_networking() { + let err = crawl_peers(&[], -1, 1000, 30, false, false) + .await + .unwrap_err(); + assert!(err.to_string().contains("depth must be non-negative")); + } + fn now_secs() -> u64 { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH)