Skip to content

Commit 9e1f775

Browse files
authored
fix(autonat): Skip unparsable multiaddr (#3363)
With this commit `libp2p-autonat` no longer discards the whole remote payload in case an addr is unparsable, but instead logs the failure and skips the unparsable multiaddr. See #3244 for details.
1 parent d736132 commit 9e1f775

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

protocols/autonat/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111
[PR 3153]: https://github.com/libp2p/rust-libp2p/pull/3153
1212

13+
# 0.9.1
14+
15+
- Skip unparsable multiaddr in `DialRequest::from_bytes`. See [PR 3351].
16+
17+
[PR 3351]: https://github.com/libp2p/rust-libp2p/pull/3351
18+
19+
1320
# 0.9.0
1421

1522
- Update to `libp2p-core` `v0.38.0`.

protocols/autonat/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ rustc-args = ["--cfg", "docsrs"]
4444

4545
[[example]]
4646
name = "client"
47-
path = "examples/autonat_client.rs"
47+
path = "examples/autonat_client.rs"
4848

4949
[[example]]
5050
name = "server"
51-
path = "examples/autonat_server.rs"
51+
path = "examples/autonat_server.rs"

protocols/autonat/src/protocol.rs

+41-9
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ impl DialRequest {
134134
PeerId::try_from(peer_id)
135135
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "invalid peer id"))?
136136
};
137-
let addrs = {
138-
let mut maddrs = vec![];
139-
for addr in addrs.into_iter() {
140-
let maddr = Multiaddr::try_from(addr)
141-
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
142-
maddrs.push(maddr);
143-
}
144-
maddrs
145-
};
137+
let addrs = addrs
138+
.into_iter()
139+
.filter_map(|a| match Multiaddr::try_from(a) {
140+
Ok(a) => Some(a),
141+
Err(e) => {
142+
log::debug!("Unable to parse multiaddr: {e}");
143+
None
144+
}
145+
})
146+
.collect();
146147
Ok(Self {
147148
peer_id,
148149
addresses: addrs,
@@ -333,4 +334,35 @@ mod tests {
333334
let response2 = DialResponse::from_bytes(&bytes).unwrap();
334335
assert_eq!(response, response2);
335336
}
337+
338+
#[test]
339+
fn test_skip_unparsable_multiaddr() {
340+
let valid_multiaddr: Multiaddr = "/ip6/2001:db8::/tcp/1234".parse().unwrap();
341+
let valid_multiaddr_bytes = valid_multiaddr.to_vec();
342+
343+
let invalid_multiaddr = {
344+
let a = vec![255; 8];
345+
assert!(Multiaddr::try_from(a.clone()).is_err());
346+
a
347+
};
348+
349+
let msg = structs_proto::Message {
350+
r#type: Some(structs_proto::message::MessageType::Dial.into()),
351+
dial: Some(structs_proto::message::Dial {
352+
peer: Some(structs_proto::message::PeerInfo {
353+
id: Some(PeerId::random().to_bytes()),
354+
addrs: vec![valid_multiaddr_bytes, invalid_multiaddr],
355+
}),
356+
}),
357+
dial_response: None,
358+
};
359+
360+
let mut bytes = Vec::with_capacity(msg.encoded_len());
361+
msg.encode(&mut bytes)
362+
.expect("Vec<u8> provides capacity as needed");
363+
364+
let request = DialRequest::from_bytes(&bytes).expect("not to fail");
365+
366+
assert_eq!(request.addresses, vec![valid_multiaddr])
367+
}
336368
}

0 commit comments

Comments
 (0)