Skip to content

Commit

Permalink
fix(kad): Skip invalid multiaddr
Browse files Browse the repository at this point in the history
With this commit `libp2p-kad` no longer discards the whole peer payload in case an addr is invalid,
but instead logs the failure, skips the invalid multiaddr and parses the remaining payload.

See libp2p#3244 for details.
  • Loading branch information
mxinden committed Dec 23, 2022
1 parent fd48971 commit 224d219
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
6 changes: 6 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.42.1

- Skip invalid multiaddr in `Peer` `addrs`. See [PR XXX].

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

# 0.42.0

- Update to `libp2p-core` `v0.38.0`.
Expand Down
4 changes: 2 additions & 2 deletions protocols/kad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-kad"
edition = "2021"
rust-version = "1.62.0"
description = "Kademlia protocol for libp2p"
version = "0.42.0"
version = "0.42.1"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down Expand Up @@ -44,7 +44,7 @@ prost-build = "0.11"
[features]
serde = ["dep:serde", "bytes/serde"]

# 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
Expand Down
33 changes: 30 additions & 3 deletions protocols/kad/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use futures::prelude::*;
use instant::Instant;
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use libp2p_core::{Multiaddr, PeerId};
use log::debug;
use prost::Message;
use std::{borrow::Cow, convert::TryFrom, time::Duration};
use std::{io, iter};
Expand Down Expand Up @@ -105,10 +106,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) => {
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 +605,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

0 comments on commit 224d219

Please sign in to comment.