Something went wrong. Try again.
contributor fork of @ptr.pet hydrant forked from ptr.pet/hydrant
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100use std::net::IpAddr;
use url::Url;
pub(crate) fn list_hosts_url(base: &Url, cursor: Option<&str>) -> Url { let mut url = base.clone(); url.set_path("/xrpc/com.atproto.sync.listHosts"); { let mut pairs = url.query_pairs_mut(); pairs.append_pair("limit", "1000"); if let Some(cursor) = cursor { pairs.append_pair("cursor", cursor); } } url}
pub(crate) fn is_private_ip(ip: IpAddr) -> bool { match ip { IpAddr::V4(ip) => { let [a, b, c, _] = ip.octets(); ip.is_loopback() || ip.is_private() || ip.is_link_local() || ip.is_multicast() || ip.is_unspecified() || ip.is_broadcast() || a == 0 || (a == 100 && b & 0xc0 == 0x40) || (a == 192 && b == 0 && c == 0) || (a == 192 && b == 0 && c == 2) || (a == 192 && b == 88 && c == 99) || (a == 198 && b & 0xfe == 18) || (a == 198 && b == 51 && c == 100) || (a == 203 && b == 0 && c == 113) || a >= 240 } IpAddr::V6(ip) => { let octets = ip.octets(); ip.is_loopback() || ip.is_multicast() || ip.is_unspecified() || ip .to_ipv4_mapped() .is_some_and(|ipv4| is_private_ip(IpAddr::V4(ipv4))) || (octets[0] & 0xfe) == 0xfc || (octets[0] == 0xfe && octets[1] & 0xc0 == 0x80) || (octets[0] == 0xfe && octets[1] & 0xc0 == 0xc0) || (octets[..4] == [0x20, 0x01, 0x0d, 0xb8]) } }}
pub(crate) fn is_safe_seed_host(host: &str) -> bool { if host.is_empty() { return false; } if host == "localhost" || host.ends_with(".local") || host.ends_with(".internal") { return false; } if let Ok(ip) = host.parse::<IpAddr>() { return !is_private_ip(ip); } true}
#[cfg(test)]mod tests { use super::*;
#[test] fn rejects_non_public_address_ranges() { for address in [ "0.0.0.1", "10.0.0.1", "100.64.0.1", "127.0.0.1", "169.254.1.1", "192.0.2.1", "198.18.0.1", "198.51.100.1", "203.0.113.1", "240.0.0.1", "::1", "::ffff:127.0.0.1", "2001:db8::1", "fc00::1", "fe80::1", "fec0::1", ] { assert!( is_private_ip(address.parse().unwrap()), "accepted {address}" ); } assert!(!is_private_ip("1.1.1.1".parse().unwrap())); assert!(!is_private_ip("2606:4700:4700::1111".parse().unwrap())); }}