Skip to content

improve error handling in webRTC-related noise function #377

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

Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions src/crypto/noise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ impl NoiseContext {
/// Get remote public key from the received Noise payload.
#[cfg(feature = "webrtc")]
pub fn get_remote_public_key(&mut self, reply: &[u8]) -> Result<PublicKey, NegotiationError> {
if reply.len() < 2 {
tracing::error!(target: LOG_TARGET, "reply too short to contain length prefix");
return Err(NegotiationError::ParseError(ParseError::InvalidReplyLength));
}

let (len_slice, reply) = reply.split_at(2);
let len = u16::from_be_bytes(
len_slice
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ pub enum ParseError {
/// This error is protocol specific.
#[error("Invalid data")]
InvalidData,
/// The provided reply length is not valid
#[error("Invalid reply length")]
InvalidReplyLength,
}

#[derive(Debug, thiserror::Error)]
Expand Down
33 changes: 20 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ use transport::Endpoint;
use types::ConnectionId;

use std::{collections::HashSet, sync::Arc};

pub use bandwidth::BandwidthSink;
pub use error::Error;
pub use peer_id::PeerId;
Expand Down Expand Up @@ -332,9 +331,11 @@ impl Litep2p {

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
listen_addresses.push(address.with(Protocol::P2p(
Multihash::from_bytes(&local_peer_id.to_bytes()).unwrap(),
)));
let peer_id_multihash = match Multihash::from_bytes(&local_peer_id.to_bytes()) {
Ok(hash) => hash,
Err(_) => continue,
};
listen_addresses.push(address.with(Protocol::P2p(peer_id_multihash)));
}

transport_manager.register_transport(SupportedTransport::Tcp, Box::new(transport));
Expand All @@ -349,9 +350,11 @@ impl Litep2p {

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
listen_addresses.push(address.with(Protocol::P2p(
Multihash::from_bytes(&local_peer_id.to_bytes()).unwrap(),
)));
let peer_id_multihash = match Multihash::from_bytes(&local_peer_id.to_bytes()) {
Ok(hash) => hash,
Err(_) => continue,
};
listen_addresses.push(address.with(Protocol::P2p(peer_id_multihash)));
}

transport_manager.register_transport(SupportedTransport::Quic, Box::new(transport));
Expand All @@ -366,9 +369,11 @@ impl Litep2p {

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
listen_addresses.push(address.with(Protocol::P2p(
Multihash::from_bytes(&local_peer_id.to_bytes()).unwrap(),
)));
let peer_id_multihash = match Multihash::from_bytes(&local_peer_id.to_bytes()) {
Ok(hash) => hash,
Err(_) => continue,
};
listen_addresses.push(address.with(Protocol::P2p(peer_id_multihash)));
}

transport_manager.register_transport(SupportedTransport::WebRtc, Box::new(transport));
Expand All @@ -383,9 +388,11 @@ impl Litep2p {

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
listen_addresses.push(address.with(Protocol::P2p(
Multihash::from_bytes(&local_peer_id.to_bytes()).unwrap(),
)));
let peer_id_multihash = match Multihash::from_bytes(&local_peer_id.to_bytes()) {
Ok(hash) => hash,
Err(_) => continue,
};
listen_addresses.push(address.with(Protocol::P2p(peer_id_multihash)));
}

transport_manager
Expand Down