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

Enforce right types for spec-reserved keys #39

Merged
merged 21 commits into from
Aug 2, 2023
Merged
Changes from 7 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
74 changes: 74 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,16 @@ impl<K: EnrKey> Enr<K> {
rlp::decode::<u16>(&value).map_err(|err| EnrError::InvalidRlpData(err.to_string()))?;
}

if is_valid_ipv4(key.as_ref(), &value) {
rlp::decode::<Vec<u8>>(&value)
.map_err(|err| EnrError::InvalidRlpData(err.to_string()))?;
}

if is_valid_secp256k1(key.as_ref(), &value) {
rlp::decode::<Vec<u8>>(&value)
.map_err(|err| EnrError::InvalidRlpData(err.to_string()))?;
}

let previous_value = self.content.insert(key.as_ref().to_vec(), value);
// add the new public key
let public_key = enr_key.public();
Expand Down Expand Up @@ -1040,6 +1050,20 @@ const fn is_keyof_u16(key: &[u8]) -> bool {
matches!(key, b"tcp" | b"tcp6" | b"udp" | b"udp6")
}

fn is_valid_ipv4(key: &[u8], value: &[u8]) -> bool {
if key == b"ip" && value[0] - 0x80 == 0x04 {
divagant-martian marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
false
}

fn is_valid_secp256k1(key: &[u8], value: &[u8]) -> bool {
if key == b"secp256k1" && value[0] - 0x80 == 0x21 {
divagant-martian marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
false
}

#[cfg(test)]
#[cfg(feature = "k256")]
mod tests {
Expand Down Expand Up @@ -1620,4 +1644,54 @@ mod tests {
assert_eq!(enr.get_raw_rlp("tcp").unwrap(), rlp::encode(&tcp).to_vec());
assert_eq!(enr.tcp4(), Some(tcp));
}

#[test]
fn test_is_valid_ipv4() {
let mut rng = rand::thread_rng();
let key = k256::ecdsa::SigningKey::random(&mut rng);
let ip_addr = b"\x7f\x00\x00\x01";
let tcp = 30303;

let mut enr = {
let mut builder = EnrBuilder::new("v4");
builder.tcp4(tcp);
builder.build(&key).unwrap()
};

let encoded = rlp::encode(&(ip_addr as &[u8])).freeze();
let decoded = rlp::decode::<Vec<u8>>(&encoded);
assert!(decoded
.clone()
.map_err(|err| EnrError::InvalidRlpData(err.to_string()))
.is_ok());

if let Ok(d) = decoded {
enr.insert(b"ip", &d.to_vec(), &key).unwrap();
let ip = Ipv4Addr::from(*ip_addr);
assert_eq!(enr.ip4().unwrap(), ip);
assert!(enr.verify());
assert!(is_valid_ipv4(b"ip", &encoded));
}
}

#[test]
fn test_is_valid_secp256k1() {
let mut rng = rand::thread_rng();
let key = k256::ecdsa::SigningKey::random(&mut rng);

let mut enr = {
let mut builder = EnrBuilder::new("v4");
builder.build(&key).unwrap()
};

let new_key = k256::ecdsa::SigningKey::random(&mut rng);

let encoded_key = rlp::encode(&(&new_key.public().encode() as &[u8]));

let decoded_key = rlp::decode::<Vec<u8>>(&encoded_key).unwrap();

assert!(is_valid_secp256k1(b"secp256k1", &encoded_key));

assert!(!enr.insert(b"secp256k1", &decoded_key, &key).is_err());
}
}