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 #3280

Merged
merged 5 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,3 +1,9 @@
# 0.42.1

- Skip invalid multiaddr in `Peer` `addrs`. See [PR XXX].
mxinden marked this conversation as resolved.
Show resolved Hide resolved

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Skip invalid multiaddr in `Peer` `addrs`. See [PR XXX].
[PR XXX]: https://github.com/libp2p/rust-libp2p/pull/XXX
- Skip invalid 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
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 <[email protected]>"]
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal preference only but I like not importing these because they often come and go with refactorings and log::debug is actually very expressive.

mxinden marked this conversation as resolved.
Show resolved Hide resolved
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:?}");
mxinden marked this conversation as resolved.
Show resolved Hide resolved
}
};
}
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