Skip to content

Commit

Permalink
fix: advance RLP decoding buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Mar 27, 2024
1 parent 83cd989 commit 3521aa4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/keys/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
//! Currently only `secp256k1` and `ed25519` key types are supported.
use super::{ed25519_dalek as ed25519, EnrKey, EnrPublicKey, SigningError};
use crate::Key;
use alloy_rlp::Error as DecoderError;
use bytes::Bytes;
pub use k256;
use std::{collections::BTreeMap, convert::TryFrom};
use zeroize::Zeroize;

use crate::Key;

/// A standard implementation of the `EnrKey` trait used to sign and modify ENR records. The variants here represent the currently
/// supported in-built signing schemes.
pub enum CombinedKey {
Expand Down
25 changes: 9 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,13 +781,13 @@ impl<K: EnrKey> Enr<K> {
.map(|_| {})
}

/// Returns wether the node can be reached over UDP or not.
/// Returns whether the node can be reached over UDP or not.
#[must_use]
pub fn is_udp_reachable(&self) -> bool {
self.udp4_socket().is_some() || self.udp6_socket().is_some()
}

/// Returns wether the node can be reached over TCP or not.
/// Returns whether the node can be reached over TCP or not.
#[must_use]
pub fn is_tcp_reachable(&self) -> bool {
self.tcp4_socket().is_some() || self.tcp6_socket().is_some()
Expand Down Expand Up @@ -981,25 +981,16 @@ impl<K: EnrKey> Decodable for Enr<K> {
return Err(DecoderError::Custom("enr exceeds max size"));
}

let header = Header::decode(buf)?;
let payload = &mut Header::decode_bytes(buf, true)?;

if !header.list {
return Err(DecoderError::Custom("Invalid format of header"));
}

if header.payload_length > buf.len() {
return Err(DecoderError::Custom("payload length exceeds buffer size"));
}

let payload = &mut &buf[..header.payload_length];
if payload.is_empty() {
return Err(DecoderError::Custom("Payload is empty"));
}

let signature = Bytes::decode(payload)?;

if payload.is_empty() {
return Err(DecoderError::Custom("Seq is miising"));
return Err(DecoderError::Custom("Seq is missing"));
}

let seq = u64::decode(payload)?;
Expand Down Expand Up @@ -1080,6 +1071,7 @@ impl<K: EnrKey> Decodable for Enr<K> {
if !enr.verify() {
return Err(DecoderError::Custom("Invalid Signature"));
}

Ok(enr)
}
}
Expand Down Expand Up @@ -1149,7 +1141,6 @@ fn check_spec_reserved_keys(key: &[u8], mut value: &[u8]) -> Result<(), Error> {
mod tests {
use super::*;
use std::convert::TryFrom;
use std::net::Ipv4Addr;

type DefaultEnr = Enr<k256::ecdsa::SigningKey>;

Expand All @@ -1162,7 +1153,9 @@ mod tests {
hex::decode("03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138")
.unwrap();

let enr = DefaultEnr::decode(&mut valid_record.as_slice()).unwrap();
let mut buf = valid_record.as_slice();
let enr = DefaultEnr::decode(&mut buf).unwrap();
assert!(buf.is_empty());

let pubkey = enr.public_key().encode();

Expand Down Expand Up @@ -1764,7 +1757,7 @@ mod tests {

let mut huge_enr = Enr::empty(&key).unwrap();
let large_vec: Vec<u8> = std::iter::repeat(0).take(MAX_ENR_SIZE).collect();
let large_vec_encoded = alloy_rlp::encode(&large_vec);
let large_vec_encoded = alloy_rlp::encode(large_vec);

huge_enr
.content
Expand Down
2 changes: 1 addition & 1 deletion src/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ mod tests {
#[test]
fn test_serde_value() {
let node = NodeId::random();
let value = serde_json::to_value(&node).unwrap();
let value = serde_json::to_value(node).unwrap();
assert_eq!(node, serde_json::from_value::<NodeId>(value).unwrap());
}

Expand Down

0 comments on commit 3521aa4

Please sign in to comment.