diff --git a/src/oci.rs b/src/oci.rs index b681352..5b5c416 100644 --- a/src/oci.rs +++ b/src/oci.rs @@ -6,7 +6,7 @@ use sha2::{Digest, Sha256}; use std::collections::HashSet; use std::fs::{self, File}; use std::io::{BufReader, Read, Write}; -use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; +use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs}; use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; @@ -60,10 +60,16 @@ pub fn is_public(ip: IpAddr) -> bool { || o[0] == 0 || (o[0] == 100 && o[1] & 0xc0 == 64)) // CGNAT 100.64/10 } - IpAddr::V6(v) => match v.to_ipv4_mapped() { - Some(v4) => is_public(v4.into()), - None => !(v.is_loopback() || v.is_unspecified() || v.is_multicast() || v.is_unique_local() || v.is_unicast_link_local()), - }, + IpAddr::V6(v) => { + let v4 = |hi: u16, lo: u16| is_public(Ipv4Addr::from((hi as u32) << 16 | lo as u32).into()); + match v.segments() { + [0, 0, 0, 0, 0, 0xffff, hi, lo] => v4(hi, lo), // IPv4-mapped + [0x64, 0xff9b, 0, 0, 0, 0, hi, lo] => v4(hi, lo), // NAT64 well-known prefix + [0x2002, hi, lo, ..] => v4(hi, lo), // 6to4 + [0x2001, 0xdb8, ..] | [0x2001, 0, ..] => false, // documentation, Teredo + s => s[0] & 0xe000 == 0x2000, // global unicast 2000::/3 only + } + } } } @@ -470,10 +476,12 @@ mod tests { #[test] fn public_addresses() { - for ip in ["127.0.0.1", "10.1.2.3", "172.16.0.1", "192.168.64.1", "169.254.169.254", "100.64.0.1", "0.0.0.0", "::1", "fe80::1", "fd00::1", "::ffff:127.0.0.1"] { + for ip in ["127.0.0.1", "10.1.2.3", "172.16.0.1", "192.168.64.1", "169.254.169.254", "100.64.0.1", "0.0.0.0", "::1", "fe80::1", "fd00::1", "::ffff:127.0.0.1", + "64:ff9b::7f00:1", "64:ff9b::a00:1", "64:ff9b:1::1", "2002:7f00:1::1", "2002:c0a8:101::", "::7f00:1", "::a00:1", + "2001:db8::1", "2001::1", "ff02::1"] { assert!(!is_public(ip.parse().unwrap()), "{ip} must be rejected"); } - for ip in ["8.8.8.8", "140.82.121.4", "2606:4700::6810:84e5"] { + for ip in ["8.8.8.8", "140.82.121.4", "2606:4700::6810:84e5", "64:ff9b::808:808", "2002:808:808::1", "::ffff:8.8.8.8"] { assert!(is_public(ip.parse().unwrap()), "{ip} must be allowed"); } }