diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index fa1d6dc4f6a..f01f9b51c4e 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.41.1 + +- Skip invalid multiaddr in `listen_addrs`. See [PR 3246]. + +[PR 3246]: https://github.com/libp2p/rust-libp2p/pull/3246 + # 0.41.0 - Change default `cache_size` of `Config` to 100. See [PR 2995]. diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index 69dc2d97985..da8e511ffb6 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-identify" edition = "2021" rust-version = "1.62.0" description = "Nodes identifcation protocol for libp2p" -version = "0.41.0" +version = "0.41.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -32,7 +32,7 @@ libp2p = { path = "../..", features = ["full"] } [build-dependencies] prost-build = "0.11" -# Passing arguments to the docsrs builder in order to properly document cfg's. +# Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index 1a969c6a967..060e84314e8 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -26,7 +26,7 @@ use libp2p_core::{ upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}, Multiaddr, PublicKey, }; -use log::trace; +use log::{debug, trace}; use std::convert::TryFrom; use std::{fmt, io, iter, pin::Pin}; use thiserror::Error; @@ -236,14 +236,25 @@ impl TryFrom for Info { let listen_addrs = { let mut addrs = Vec::new(); for addr in msg.listen_addrs.into_iter() { - addrs.push(parse_multiaddr(addr)?); + match parse_multiaddr(addr) { + Ok(a) => addrs.push(a), + Err(e) => { + debug!("Unable to parse multiaddr: {e:?}"); + } + } } addrs }; let public_key = PublicKey::from_protobuf_encoding(&msg.public_key.unwrap_or_default())?; - let observed_addr = parse_multiaddr(msg.observed_addr.unwrap_or_default())?; + let observed_addr = match parse_multiaddr(msg.observed_addr.unwrap_or_default()) { + Ok(a) => a, + Err(e) => { + debug!("Unable to parse multiaddr: {e:?}"); + Multiaddr::empty() + } + }; let info = Info { public_key, protocol_version: msg.protocol_version.unwrap_or_default(), @@ -363,4 +374,33 @@ mod tests { bg_task.await; }); } + + #[test] + fn skip_invalid_multiaddr() { + let valid_multiaddr: Multiaddr = "/ip6/2001:db8::/tcp/1234".parse().unwrap(); + let valid_multiaddr_bytes = valid_multiaddr.to_vec(); + + let invalid_multiaddr = { + let a = vec![255; 8]; + assert!(Multiaddr::try_from(a.clone()).is_err()); + a + }; + + let payload = structs_proto::Identify { + agent_version: None, + listen_addrs: vec![valid_multiaddr_bytes, invalid_multiaddr], + observed_addr: None, + protocol_version: None, + protocols: vec![], + public_key: Some( + identity::Keypair::generate_ed25519() + .public() + .to_protobuf_encoding(), + ), + }; + + let info = Info::try_from(payload).expect("not to fail"); + + assert_eq!(info.listen_addrs, vec![valid_multiaddr]) + } }