Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kad): Skip invalid multiaddr #3284

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

[PR 3239]: https://github.com/libp2p/rust-libp2p/pull/3239

# 0.42.1

- Skip unparsable multiaddr in `Peer::addrs`. See [PR 3280].

[PR 3280]: https://github.com/libp2p/rust-libp2p/pull/3280

# 0.42.0

- Update to `libp2p-core` `v0.38.0`.
Expand Down
32 changes: 29 additions & 3 deletions protocols/kad/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@ impl TryFrom<proto::message::Peer> for KadPeer {

let mut addrs = Vec::with_capacity(peer.addrs.len());
for addr in peer.addrs.into_iter() {
let as_ma = Multiaddr::try_from(addr).map_err(invalid_data)?;
addrs.push(as_ma);
match Multiaddr::try_from(addr) {
Ok(a) => addrs.push(a),
Err(e) => {
log::debug!("Unable to parse multiaddr: {e}");
}
};
}
debug_assert_eq!(addrs.len(), addrs.capacity());

let connection_ty = proto::message::ConnectionType::from_i32(peer.connection)
.ok_or_else(|| invalid_data("unknown connection type"))?
Expand Down Expand Up @@ -601,6 +604,29 @@ where

#[cfg(test)]
mod tests {
use super::*;

#[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 = proto::message::Peer {
id: PeerId::random().to_bytes(),
addrs: vec![valid_multiaddr_bytes, invalid_multiaddr],
connection: proto::message::ConnectionType::CanConnect.into(),
};

let peer = KadPeer::try_from(payload).expect("not to fail");

assert_eq!(peer.multiaddrs, vec![valid_multiaddr])
}

/*// TODO: restore
use self::libp2p_tcp::TcpTransport;
Expand Down