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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ pub enum AddressError {
/// The provided address contains an invalid multihash.
#[error("Multihash does not contain a valid peer ID : `{0:?}`")]
InvalidPeerId(Multihash),
/// The provided multihash is invalid.
#[error("The provided multihash is not valid")]
InvalidMultihash,
}

#[derive(Debug, thiserror::Error, PartialEq)]
Expand Down Expand Up @@ -179,6 +182,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
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use types::ConnectionId;

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

use crate::error::AddressError;
pub use bandwidth::BandwidthSink;
pub use error::Error;
pub use peer_id::PeerId;
Expand Down Expand Up @@ -352,9 +353,9 @@ 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 = Multihash::from_bytes(&local_peer_id.to_bytes())
.map_err(|_e| Error::AddressError(AddressError::InvalidMultihash))?;
listen_addresses.push(address.with(Protocol::P2p(peer_id_multihash)));
}

transport_manager.register_transport(SupportedTransport::WebRtc, Box::new(transport));
Expand Down