Skip to content

Commit 954d01b

Browse files
transport-manager: stricter supported multiaddress check (#403)
Ensure multiaddresses always end with `/p2p/...` protocol when registering them in `TransportManager` and not try to dial invalid addresses.
1 parent d98bd09 commit 954d01b

File tree

1 file changed

+131
-9
lines changed

1 file changed

+131
-9
lines changed

src/transport/manager/handle.rs

Lines changed: 131 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,21 @@ impl TransportManagerHandle {
143143

144144
match iter.next() {
145145
None => false,
146-
Some(Protocol::Tcp(_)) => match iter.next() {
147-
Some(Protocol::P2p(_)) =>
146+
Some(Protocol::Tcp(_)) => match (iter.next(), iter.next(), iter.next()) {
147+
(Some(Protocol::P2p(_)), None, None) =>
148148
self.supported_transport.contains(&SupportedTransport::Tcp),
149149
#[cfg(feature = "websocket")]
150-
Some(Protocol::Ws(_)) =>
150+
(Some(Protocol::Ws(_)), Some(Protocol::P2p(_)), None) =>
151151
self.supported_transport.contains(&SupportedTransport::WebSocket),
152152
#[cfg(feature = "websocket")]
153-
Some(Protocol::Wss(_)) =>
153+
(Some(Protocol::Wss(_)), Some(Protocol::P2p(_)), None) =>
154154
self.supported_transport.contains(&SupportedTransport::WebSocket),
155155
_ => false,
156156
},
157157
#[cfg(feature = "quic")]
158-
Some(Protocol::Udp(_)) => match (
159-
iter.next(),
160-
self.supported_transport.contains(&SupportedTransport::Quic),
161-
) {
162-
(Some(Protocol::QuicV1), true) => true,
158+
Some(Protocol::Udp(_)) => match (iter.next(), iter.next(), iter.next()) {
159+
(Some(Protocol::QuicV1), Some(Protocol::P2p(_)), None) =>
160+
self.supported_transport.contains(&SupportedTransport::Quic),
163161
_ => false,
164162
},
165163
_ => false,
@@ -387,6 +385,29 @@ mod tests {
387385
assert!(handle.supported_transport(&address));
388386
}
389387

388+
#[tokio::test]
389+
async fn tcp_unsupported() {
390+
let (mut handle, _rx) = make_transport_manager_handle();
391+
392+
let address =
393+
"/dns4/google.com/tcp/24928/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy"
394+
.parse()
395+
.unwrap();
396+
assert!(!handle.supported_transport(&address));
397+
}
398+
399+
#[tokio::test]
400+
async fn tcp_non_terminal_unsupported() {
401+
let (mut handle, _rx) = make_transport_manager_handle();
402+
handle.supported_transport.insert(SupportedTransport::Tcp);
403+
404+
let address =
405+
"/dns4/google.com/tcp/24928/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy/p2p-circuit"
406+
.parse()
407+
.unwrap();
408+
assert!(!handle.supported_transport(&address));
409+
}
410+
390411
#[cfg(feature = "websocket")]
391412
#[tokio::test]
392413
async fn websocket_supported() {
@@ -400,6 +421,107 @@ mod tests {
400421
assert!(handle.supported_transport(&address));
401422
}
402423

424+
#[cfg(feature = "websocket")]
425+
#[tokio::test]
426+
async fn websocket_unsupported() {
427+
let (mut handle, _rx) = make_transport_manager_handle();
428+
429+
let address =
430+
"/dns4/google.com/tcp/24928/ws/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy"
431+
.parse()
432+
.unwrap();
433+
assert!(!handle.supported_transport(&address));
434+
}
435+
436+
#[cfg(feature = "websocket")]
437+
#[tokio::test]
438+
async fn websocket_non_terminal_unsupported() {
439+
let (mut handle, _rx) = make_transport_manager_handle();
440+
handle.supported_transport.insert(SupportedTransport::WebSocket);
441+
442+
let address =
443+
"/dns4/google.com/tcp/24928/ws/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy/p2p-circuit"
444+
.parse()
445+
.unwrap();
446+
assert!(!handle.supported_transport(&address));
447+
}
448+
449+
#[cfg(feature = "websocket")]
450+
#[tokio::test]
451+
async fn wss_supported() {
452+
let (mut handle, _rx) = make_transport_manager_handle();
453+
handle.supported_transport.insert(SupportedTransport::WebSocket);
454+
455+
let address =
456+
"/dns4/google.com/tcp/24928/wss/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy"
457+
.parse()
458+
.unwrap();
459+
assert!(handle.supported_transport(&address));
460+
}
461+
462+
#[cfg(feature = "websocket")]
463+
#[tokio::test]
464+
async fn wss_unsupported() {
465+
let (mut handle, _rx) = make_transport_manager_handle();
466+
467+
let address =
468+
"/dns4/google.com/tcp/24928/wss/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy"
469+
.parse()
470+
.unwrap();
471+
assert!(!handle.supported_transport(&address));
472+
}
473+
474+
#[cfg(feature = "websocket")]
475+
#[tokio::test]
476+
async fn wss_non_terminal_unsupported() {
477+
let (mut handle, _rx) = make_transport_manager_handle();
478+
handle.supported_transport.insert(SupportedTransport::WebSocket);
479+
480+
let address =
481+
"/dns4/google.com/tcp/24928/wss/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy/p2p-circuit"
482+
.parse()
483+
.unwrap();
484+
assert!(!handle.supported_transport(&address));
485+
}
486+
487+
#[cfg(feature = "quic")]
488+
#[tokio::test]
489+
async fn quic_supported() {
490+
let (mut handle, _rx) = make_transport_manager_handle();
491+
handle.supported_transport.insert(SupportedTransport::Quic);
492+
493+
let address =
494+
"/dns4/google.com/udp/24928/quic-v1/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy"
495+
.parse()
496+
.unwrap();
497+
assert!(handle.supported_transport(&address));
498+
}
499+
500+
#[cfg(feature = "quic")]
501+
#[tokio::test]
502+
async fn quic_unsupported() {
503+
let (mut handle, _rx) = make_transport_manager_handle();
504+
505+
let address =
506+
"/dns4/google.com/udp/24928/quic-v1/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy"
507+
.parse()
508+
.unwrap();
509+
assert!(!handle.supported_transport(&address));
510+
}
511+
512+
#[cfg(feature = "quic")]
513+
#[tokio::test]
514+
async fn quic_non_terminal_unsupported() {
515+
let (mut handle, _rx) = make_transport_manager_handle();
516+
handle.supported_transport.insert(SupportedTransport::Quic);
517+
518+
let address =
519+
"/dns4/google.com/udp/24928/quic-v1/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy/p2p-circuit"
520+
.parse()
521+
.unwrap();
522+
assert!(!handle.supported_transport(&address));
523+
}
524+
403525
#[test]
404526
fn transport_not_supported() {
405527
let (handle, _rx) = make_transport_manager_handle();

0 commit comments

Comments
 (0)