Skip to content

Commit

Permalink
fead: improve ipv4 and ipv6 validation
Browse files Browse the repository at this point in the history
  • Loading branch information
renauter committed Sep 26, 2023
1 parent 4c29c6f commit 0a9ecb3
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions substrate-node/support/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,17 @@ impl IP4 {
let gw4 = IPv4::parse(&self.gw).map_err(|_| PublicIpError::InvalidGw4)?;
let ip4 = IPv4Cidr::parse(&self.ip).map_err(|_| PublicIpError::InvalidIp4)?;

if gw4.is_public()
&& gw4.is_unicast()
&& ip4.is_public()
&& ip4.is_unicast()
&& ip4.contains(gw4)
if ip4.as_prefix() == gw4
|| !gw4.is_public()
|| !gw4.is_unicast()
|| !ip4.is_public()
|| !ip4.is_unicast()
|| !ip4.contains(gw4)
{
Ok(())
} else {
Err(PublicIpError::InvalidPublicIp)
return Err(PublicIpError::InvalidPublicIp);
}

Ok(())
}
}

Expand All @@ -155,18 +156,19 @@ pub struct IP6 {
impl IP6 {
pub fn is_valid(&self) -> Result<(), PublicIpError> {
let gw6 = IPv6::parse(&self.gw).map_err(|_| PublicIpError::InvalidGw6)?;
let ipv6 = IPv6Cidr::parse(&self.ip).map_err(|_| PublicIpError::InvalidIp6)?;

if gw6.is_public()
&& gw6.is_unicast()
&& ipv6.is_public()
&& ipv6.is_unicast()
&& ipv6.contains(gw6)
let ip6 = IPv6Cidr::parse(&self.ip).map_err(|_| PublicIpError::InvalidIp6)?;

if ip6.as_ip() == gw6
|| !gw6.is_public()
|| !gw6.is_unicast()
|| !ip6.is_public()
|| !ip6.is_unicast()
|| !ip6.contains(gw6)
{
Ok(())
} else {
Err(PublicIpError::InvalidPublicIp)
return Err(PublicIpError::InvalidPublicIp);
}

Ok(())
}
}

Expand Down

0 comments on commit 0a9ecb3

Please sign in to comment.