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

*: Don't leak prost dependency in error types #3058

Merged
merged 23 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d93cdb6
Use `thiserror` for `FromEnvelopeError`
thomaseizinger Oct 24, 2022
e2bd6f6
Use `thiserror` for `DecodingError`
thomaseizinger Oct 24, 2022
3712834
Remove `#[source]` when implied by `#[from]`
thomaseizinger Oct 24, 2022
a1e6a24
Remove unnecessary prefix for already imported item
thomaseizinger Oct 24, 2022
60c8ce8
Don't leak `prost::Error` through error type of `prost-codec`
thomaseizinger Oct 24, 2022
56571aa
Use `thiserror` for `floodsub::FloodsubDecodeError`
thomaseizinger Oct 24, 2022
02dc2f0
Sort dependencies in `libp2p-rendezvous alphabetically`
thomaseizinger Oct 24, 2022
d1f16e6
Use `prost-codec` in `libp2p-rendezvous`
thomaseizinger Oct 24, 2022
0f5105a
Sort dependencies alphabetically
thomaseizinger Oct 24, 2022
a3d4905
Don't log and return errors
thomaseizinger Oct 24, 2022
e113092
Add dedicated error variant for failing to parse public key
thomaseizinger Oct 24, 2022
77dac53
Rename error variant to better describe the problem
thomaseizinger Oct 24, 2022
b89d577
Add dedicated error variant for failing to parse peer ID
thomaseizinger Oct 24, 2022
31232e7
Remove `Option` from error variant
thomaseizinger Oct 24, 2022
ac31e0e
Don't leak `prost` dependency in `PlainTextError`
thomaseizinger Oct 24, 2022
747e5f1
Bump version for `libp2p-plaintext` accordingly
thomaseizinger Oct 24, 2022
e8190cc
Don't leak `prost` dependency in `NoiseError`
thomaseizinger Oct 24, 2022
f0cafa8
Don't leak `prost` dependency in `FloodsubDecodeError`
thomaseizinger Oct 24, 2022
9aa6a68
Don't leak `prost` dependency in `FromEnvelopeError` and `DecodingError`
thomaseizinger Oct 24, 2022
65ba2de
Bump version accordingly
thomaseizinger Oct 24, 2022
44fce3f
Set changelog number correctly
thomaseizinger Oct 24, 2022
3b63700
Merge branch 'master' into wrap-prost-errors
thomaseizinger Oct 24, 2022
6905829
Merge branch 'master' into wrap-prost-errors
mxinden Nov 2, 2022
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
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,30 @@
See [PR 3001].
- Introduce [`libp2p-tls` `v0.1.0`](transports/tls/CHANGELOG.md#010). See [PR 2945].
- Update individual crates.
- Update to [`libp2p-autonat` `v0.89.0`](protocols/autonat/CHANGELOG.md#090).
- Update to [`libp2p-core` `v0.38.0`](core/CHANGELOG.md#0380).
- Update to [`libp2p-dcutr` `v0.8.0`](protocols/dcutr/CHANGELOG.md#080).
- Update to [`libp2p-deflate` `v0.38.0`](transports/deflate/CHANGELOG.md#0380).
- Update to [`libp2p-dns` `v0.38.0`](transports/dns/CHANGELOG.md#0380).
- Update to [`libp2p-floodsub` `v0.41.0`](protocols/floodsub/CHANGELOG.md#0410).
- Update to [`libp2p-gossipsub` `v0.43.0`](protocols/gossipsub/CHANGELOG.md#0430).
- Update to [`libp2p-identify` `v0.41.0`](protocols/identify/CHANGELOG.md#0410).
- Update to [`libp2p-kad` `v0.42.0`](protocols/kad/CHANGELOG.md#0420).
- Update to [`libp2p-mdns` `v0.42.0`](protocols/mdns/CHANGELOG.md#0420).
- Update to [`libp2p-metrics` `v0.11.0`](misc/metrics/CHANGELOG.md#0110).
- Update to [`libp2p-mplex` `v0.38.0`](muxers/mplex/CHANGELOG.md#0380).
- Update to [`libp2p-noise` `v0.41.0`](transports/noise/CHANGELOG.md#0410).
- Update to [`libp2p-ping` `v0.41.0`](protocols/ping/CHANGELOG.md#0410).
- Update to [`libp2p-plaintext` `v0.38.0`](transports/plaintext/CHANGELOG.md#0380).
- Update to [`libp2p-relay` `v0.14.0`](protocols/relay/CHANGELOG.md#0140).
- Update to [`libp2p-rendezvous` `v0.11.0`](protocols/rendezovus/CHANGELOG.md#0110).
- Update to [`libp2p-request-response` `v0.23.0`](protocols/request-response/CHANGELOG.md#0230).
- Update to [`libp2p-swarm` `v0.41.0`](swarm/CHANGELOG.md#0410).
- Update to [`libp2p-tcp` `v0.38.0`](transports/tcp/CHANGELOG.md#0380).
- Update to [`libp2p-uds` `v0.37.0`](transports/uds/CHANGELOG.md#0370).
- Update to [`libp2p-wasm-ext` `v0.38.0`](transports/wasm-ext/CHANGELOG.md#0380).
- Update to [`libp2p-websocket` `v0.40.0`](transports/websocket/CHANGELOG.md#0400).
- Update to [`libp2p-yamux` `v0.42.0`](muxers/yamux/CHANGELOG.md#0420).

[PR 3001]: https://github.com/libp2p/rust-libp2p/pull/3001
[PR 2945]: https://github.com/libp2p/rust-libp2p/pull/2945
Expand Down
46 changes: 23 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,41 +86,41 @@ getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature
instant = "0.1.11" # Explicit dependency to be used in `wasm-bindgen` feature
lazy_static = "1.2"

libp2p-autonat = { version = "0.8.0", path = "protocols/autonat", optional = true }
libp2p-core = { version = "0.37.0", path = "core" }
libp2p-dcutr = { version = "0.7.0", path = "protocols/dcutr", optional = true }
libp2p-floodsub = { version = "0.40.1", path = "protocols/floodsub", optional = true }
libp2p-identify = { version = "0.40.0", path = "protocols/identify", optional = true }
libp2p-kad = { version = "0.41.0", path = "protocols/kad", optional = true }
libp2p-metrics = { version = "0.10.0", path = "misc/metrics", optional = true }
libp2p-mplex = { version = "0.37.0", path = "muxers/mplex", optional = true }
libp2p-noise = { version = "0.40.0", path = "transports/noise", optional = true }
libp2p-ping = { version = "0.40.1", path = "protocols/ping", optional = true }
libp2p-plaintext = { version = "0.37.0", path = "transports/plaintext", optional = true }
libp2p-autonat = { version = "0.9.0", path = "protocols/autonat", optional = true }
libp2p-core = { version = "0.38.0", path = "core" }
libp2p-dcutr = { version = "0.8.0", path = "protocols/dcutr", optional = true }
libp2p-floodsub = { version = "0.41.0", path = "protocols/floodsub", optional = true }
libp2p-identify = { version = "0.41.0", path = "protocols/identify", optional = true }
libp2p-kad = { version = "0.42.0", path = "protocols/kad", optional = true }
libp2p-metrics = { version = "0.11.0", path = "misc/metrics", optional = true }
libp2p-mplex = { version = "0.38.0", path = "muxers/mplex", optional = true }
libp2p-noise = { version = "0.41.0", path = "transports/noise", optional = true }
libp2p-ping = { version = "0.41.0", path = "protocols/ping", optional = true }
libp2p-plaintext = { version = "0.38.0", path = "transports/plaintext", optional = true }
libp2p-pnet = { version = "0.22.1", path = "transports/pnet", optional = true }
libp2p-relay = { version = "0.13.0", path = "protocols/relay", optional = true }
libp2p-rendezvous = { version = "0.10.0", path = "protocols/rendezvous", optional = true }
libp2p-request-response = { version = "0.22.1", path = "protocols/request-response", optional = true }
libp2p-swarm = { version = "0.40.1", path = "swarm" }
libp2p-relay = { version = "0.14.0", path = "protocols/relay", optional = true }
libp2p-rendezvous = { version = "0.11.0", path = "protocols/rendezvous", optional = true }
libp2p-request-response = { version = "0.23.0", path = "protocols/request-response", optional = true }
libp2p-swarm = { version = "0.41.0", path = "swarm" }
libp2p-swarm-derive = { version = "0.30.1", path = "swarm-derive" }
libp2p-uds = { version = "0.36.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.37.0", path = "transports/wasm-ext", optional = true }
libp2p-yamux = { version = "0.41.0", path = "muxers/yamux", optional = true }
libp2p-uds = { version = "0.37.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.38.0", path = "transports/wasm-ext", optional = true }
libp2p-yamux = { version = "0.42.0", path = "muxers/yamux", optional = true }
multiaddr = { version = "0.14.0" }
parking_lot = "0.12.0"
pin-project = "1.0.0"
smallvec = "1.6.1"

[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
libp2p-deflate = { version = "0.37.0", path = "transports/deflate", optional = true }
libp2p-dns = { version = "0.37.0", path = "transports/dns", optional = true }
libp2p-mdns = { version = "0.41.0", path = "protocols/mdns", optional = true }
libp2p-deflate = { version = "0.38.0", path = "transports/deflate", optional = true }
libp2p-dns = { version = "0.38.0", path = "transports/dns", optional = true }
libp2p-mdns = { version = "0.42.0", path = "protocols/mdns", optional = true }
libp2p-tcp = { version = "0.38.0", path = "transports/tcp", optional = true }
libp2p-websocket = { version = "0.39.0", path = "transports/websocket", optional = true }
libp2p-websocket = { version = "0.40.0", path = "transports/websocket", optional = true }
libp2p-tls = { version = "0.1.0-alpha", path = "transports/tls", optional = true }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
libp2p-gossipsub = { version = "0.42.1", path = "protocols/gossipsub", optional = true }
libp2p-gossipsub = { version = "0.43.0", path = "protocols/gossipsub", optional = true }

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
Expand Down
6 changes: 6 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.38.0 [unreleased]

- Hide `prost::Error` from public API in `FromEnvelopeError::InvalidPeerRecord` and `signed_envelope::DecodingError`. See [PR 3058].

[PR 3058]: https://github.com/libp2p/rust-libp2p/pull/3058

# 0.37.0

- Implement `Hash` and `Ord` for `PublicKey`. See [PR 2915].
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-core"
edition = "2021"
rust-version = "1.56.1"
description = "Core traits and structs of libp2p"
version = "0.37.0"
version = "0.38.0"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
4 changes: 4 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ pub use translation::address_translation;
pub use transport::Transport;
pub use upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeError, UpgradeInfo};

#[derive(thiserror::Error, Debug)]
#[error(transparent)]
pub struct DecodeError(prost::DecodeError);

use std::{future::Future, pin::Pin};

/// Implemented on objects that can run a `Future` in the background.
Expand Down
75 changes: 12 additions & 63 deletions core/src/peer_record.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::identity::error::SigningError;
use crate::identity::Keypair;
use crate::signed_envelope::SignedEnvelope;
use crate::{peer_record_proto, signed_envelope, Multiaddr, PeerId};
use crate::{peer_record_proto, signed_envelope, DecodeError, Multiaddr, PeerId};
use instant::SystemTime;
use std::convert::TryInto;
use std::fmt;

const PAYLOAD_TYPE: &str = "/libp2p/routing-state-record";
const DOMAIN_SEP: &str = "libp2p-routing-state";
Expand Down Expand Up @@ -34,7 +33,7 @@ impl PeerRecord {

let (payload, signing_key) =
envelope.payload_and_signing_key(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?;
let record = peer_record_proto::PeerRecord::decode(payload)?;
let record = peer_record_proto::PeerRecord::decode(payload).map_err(DecodeError)?;

let peer_id = PeerId::from_bytes(&record.peer_id)?;

Expand Down Expand Up @@ -124,73 +123,23 @@ impl PeerRecord {
}
}

#[derive(Debug)]
#[derive(thiserror::Error, Debug)]
pub enum FromEnvelopeError {
/// Failed to extract the payload from the envelope.
BadPayload(signed_envelope::ReadPayloadError),
#[error("Failed to extract payload from envelope")]
BadPayload(#[from] signed_envelope::ReadPayloadError),
/// Failed to decode the provided bytes as a [`PeerRecord`].
InvalidPeerRecord(prost::DecodeError),
#[error("Failed to decode bytes as PeerRecord")]
InvalidPeerRecord(#[from] DecodeError),
/// Failed to decode the peer ID.
InvalidPeerId(multihash::Error),
#[error("Failed to decode bytes as PeerId")]
InvalidPeerId(#[from] multihash::Error),
/// The signer of the envelope is different than the peer id in the record.
#[error("The signer of the envelope is different than the peer id in the record")]
MismatchedSignature,
/// Failed to decode a multi-address.
InvalidMultiaddr(multiaddr::Error),
}

impl From<signed_envelope::ReadPayloadError> for FromEnvelopeError {
fn from(e: signed_envelope::ReadPayloadError) -> Self {
Self::BadPayload(e)
}
}

impl From<prost::DecodeError> for FromEnvelopeError {
fn from(e: prost::DecodeError) -> Self {
Self::InvalidPeerRecord(e)
}
}

impl From<multihash::Error> for FromEnvelopeError {
fn from(e: multihash::Error) -> Self {
Self::InvalidPeerId(e)
}
}

impl From<multiaddr::Error> for FromEnvelopeError {
fn from(e: multiaddr::Error) -> Self {
Self::InvalidMultiaddr(e)
}
}

impl fmt::Display for FromEnvelopeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BadPayload(_) => write!(f, "Failed to extract payload from envelope"),
Self::InvalidPeerRecord(_) => {
write!(f, "Failed to decode bytes as PeerRecord")
}
Self::InvalidPeerId(_) => write!(f, "Failed to decode bytes as PeerId"),
Self::MismatchedSignature => write!(
f,
"The signer of the envelope is different than the peer id in the record"
),
Self::InvalidMultiaddr(_) => {
write!(f, "Failed to decode bytes as MultiAddress")
}
}
}
}

impl std::error::Error for FromEnvelopeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidPeerRecord(inner) => Some(inner),
Self::InvalidPeerId(inner) => Some(inner),
Self::MismatchedSignature => None,
Self::InvalidMultiaddr(inner) => Some(inner),
Self::BadPayload(inner) => Some(inner),
}
}
#[error("Failed to decode bytes as MultiAddress")]
InvalidMultiaddr(#[from] multiaddr::Error),
}

#[cfg(test)]
Expand Down
45 changes: 8 additions & 37 deletions core/src/signed_envelope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::identity::error::SigningError;
use crate::identity::Keypair;
use crate::{identity, PublicKey};
use crate::{identity, DecodeError, PublicKey};
use std::convert::TryInto;
use std::fmt;
use unsigned_varint::encode::usize_buffer;
Expand Down Expand Up @@ -94,7 +94,7 @@ impl SignedEnvelope {
pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<Self, DecodingError> {
use prost::Message;

let envelope = crate::envelope_proto::Envelope::decode(bytes)?;
let envelope = crate::envelope_proto::Envelope::decode(bytes).map_err(DecodeError)?;

Ok(Self {
key: envelope
Expand Down Expand Up @@ -140,48 +140,19 @@ fn signature_payload(domain_separation: String, payload_type: &[u8], payload: &[
}

/// Errors that occur whilst decoding a [`SignedEnvelope`] from its byte representation.
#[derive(Debug)]
#[derive(thiserror::Error, Debug)]
pub enum DecodingError {
/// Decoding the provided bytes as a signed envelope failed.
InvalidEnvelope(prost::DecodeError),
#[error("Failed to decode envelope")]
InvalidEnvelope(#[from] DecodeError),
/// The public key in the envelope could not be converted to our internal public key type.
InvalidPublicKey(identity::error::DecodingError),
#[error("Failed to convert public key")]
InvalidPublicKey(#[from] identity::error::DecodingError),
/// The public key in the envelope could not be converted to our internal public key type.
#[error("Public key is missing from protobuf struct")]
MissingPublicKey,
}

impl From<prost::DecodeError> for DecodingError {
fn from(e: prost::DecodeError) -> Self {
Self::InvalidEnvelope(e)
}
}

impl From<identity::error::DecodingError> for DecodingError {
fn from(e: identity::error::DecodingError) -> Self {
Self::InvalidPublicKey(e)
}
}

impl fmt::Display for DecodingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidEnvelope(_) => write!(f, "Failed to decode envelope"),
Self::InvalidPublicKey(_) => write!(f, "Failed to convert public key"),
Self::MissingPublicKey => write!(f, "Public key is missing from protobuf struct"),
}
}
}

impl std::error::Error for DecodingError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidEnvelope(inner) => Some(inner),
Self::InvalidPublicKey(inner) => Some(inner),
Self::MissingPublicKey => None,
}
}
}

/// Errors that occur whilst extracting the payload of a [`SignedEnvelope`].
#[derive(Debug)]
pub enum ReadPayloadError {
Expand Down
2 changes: 1 addition & 1 deletion misc/keygen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ clap = { version = "4.0.13", features = ["derive"] }
zeroize = "1"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
libp2p-core = { version = "0.37.0", path = "../../core" }
libp2p-core = { version = "0.38.0", path = "../../core" }
base64 = "0.13.0"
19 changes: 19 additions & 0 deletions misc/metrics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# 0.11.0 [unreleased]

- Update to `libp2p-dcutr` `v0.8.0`.

- Update to `libp2p-identify` `v0.41.0`.

- Update to `libp2p-relay` `v0.14.0`.

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

- Update to `libp2p-swarm` `v0.41.0`.

- Update to `libp2p-ping` `v0.41.0`.

- Update to `libp2p-kad` `v0.42.0`.

- Update to `libp2p-gossipsub` `v0.43.0`.

# 0.10.0

- Update to `libp2p-swarm` `v0.40.0`.
Expand Down Expand Up @@ -129,6 +147,7 @@
- Drop support for gossipsub in wasm32-unknown-unknown target (see [PR 2506]).

[PR 2442]: https://github.com/libp2p/rust-libp2p/pull/2442

[PR 2506]: https://github.com/libp2p/rust-libp2p/pull/2506

# 0.3.0 [2022-01-27]
Expand Down
18 changes: 9 additions & 9 deletions misc/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-metrics"
edition = "2021"
rust-version = "1.56.1"
description = "Metrics for libp2p"
version = "0.10.0"
version = "0.11.0"
authors = ["Max Inden <mail@max-inden.de>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand All @@ -19,17 +19,17 @@ relay = ["libp2p-relay"]
dcutr = ["libp2p-dcutr"]

[dependencies]
libp2p-core = { version = "0.37.0", path = "../../core" }
libp2p-dcutr = { version = "0.7.0", path = "../../protocols/dcutr", optional = true }
libp2p-identify = { version = "0.40.0", path = "../../protocols/identify", optional = true }
libp2p-kad = { version = "0.41.0", path = "../../protocols/kad", optional = true }
libp2p-ping = { version = "0.40.0", path = "../../protocols/ping", optional = true }
libp2p-relay = { version = "0.13.0", path = "../../protocols/relay", optional = true }
libp2p-swarm = { version = "0.40.0", path = "../../swarm" }
libp2p-core = { version = "0.38.0", path = "../../core" }
libp2p-dcutr = { version = "0.8.0", path = "../../protocols/dcutr", optional = true }
libp2p-identify = { version = "0.41.0", path = "../../protocols/identify", optional = true }
libp2p-kad = { version = "0.42.0", path = "../../protocols/kad", optional = true }
libp2p-ping = { version = "0.41.0", path = "../../protocols/ping", optional = true }
libp2p-relay = { version = "0.14.0", path = "../../protocols/relay", optional = true }
libp2p-swarm = { version = "0.41.0", path = "../../swarm" }
prometheus-client = "0.18.0"

[target.'cfg(not(target_os = "unknown"))'.dependencies]
libp2p-gossipsub = { version = "0.42.0", path = "../../protocols/gossipsub", optional = true }
libp2p-gossipsub = { version = "0.43.0", path = "../../protocols/gossipsub", optional = true }

[dev-dependencies]
log = "0.4.0"
Expand Down
8 changes: 7 additions & 1 deletion misc/prost-codec/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# 0.3.0 [unreleased]

- Don't leak `prost` dependency in `Error` type. See [PR 3058].

[PR 3058]: https://github.com/libp2p/rust-libp2p/pull/3058/

# 0.2.0

- Update to prost(-build) `v0.11`. See [PR 2788].

[PR 2788]: https://github.com/libp2p/rust-libp2p/pull/2788/
[PR 2788]: https://github.com/libp2p/rust-libp2p/pull/2788/
2 changes: 1 addition & 1 deletion misc/prost-codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "prost-codec"
edition = "2021"
rust-version = "1.56.1"
description = "Asynchronous de-/encoding of Protobuf structs using asynchronous-codec, unsigned-varint and prost."
version = "0.2.0"
version = "0.3.0"
authors = ["Max Inden <mail@max-inden.de>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
Loading