use std::net::{IpAddr, Ipv6Addr, SocketAddr};
#[must_use]
pub fn is_path_absolute(path: &str) -> bool {
let Some(rest) = path.strip_prefix('/') else {
return false;
};
if rest.starts_with('/') {
return false;
}
let mut bytes = rest.bytes();
while let Some(byte) = bytes.next() {
match byte {
b'/' | b':' | b'@' | b'-' | b'.' | b'_' | b'~' | b'!' | b'$' | b'&' | b'\'' | b'('
| b')' | b'*' | b'+' | b',' | b';' | b'=' => {}
b'%' => {
let (Some(high), Some(low)) = (bytes.next(), bytes.next()) else {
return false;
};
if !(high.is_ascii_hexdigit() && low.is_ascii_hexdigit()) {
return false;
}
}
byte if byte.is_ascii_alphanumeric() => {}
_ => return false,
}
}
true
}
#[must_use]
pub fn is_local_address(address: &str) -> bool {
if let Ok(socket) = address.parse::<SocketAddr>() {
return is_local_ip(socket.ip());
}
let bare = address
.strip_prefix('[')
.and_then(|rest| rest.strip_suffix(']'))
.unwrap_or(address);
if let Ok(ip) = bare.parse::<IpAddr>() {
return is_local_ip(ip);
}
let host = address
.rsplit_once(':')
.filter(|(_, port)| !port.is_empty() && port.bytes().all(|byte| byte.is_ascii_digit()))
.map_or(address, |(host, _)| host)
.trim_end_matches('.')
.to_ascii_lowercase();
let last_label = host.rsplit('.').next().unwrap_or("");
host == "localhost"
|| host == "localhost.localdomain"
|| host.ends_with(".localhost")
|| last_label
.strip_prefix("0x")
.is_some_and(|hex| !hex.is_empty() && hex.bytes().all(|byte| byte.is_ascii_hexdigit()))
|| (!last_label.is_empty() && last_label.bytes().all(|byte| byte.is_ascii_digit()))
}
fn is_local_ip(ip: IpAddr) -> bool {
match ip {
IpAddr::V4(ip) => ip.is_loopback() || ip.is_unspecified(),
IpAddr::V6(ip) => {
ip.is_loopback()
|| ip.is_unspecified()
|| embedded_ipv4(ip)
.is_some_and(|inner| inner.is_loopback() || inner.is_unspecified())
}
}
}
fn embedded_ipv4(ip: Ipv6Addr) -> Option<std::net::Ipv4Addr> {
let [a, b, c, d, e, f, _, _] = ip.segments();
let [.., o1, o2, o3, o4] = ip.octets();
let inner = std::net::Ipv4Addr::new(o1, o2, o3, o4);
let mapped = [a, b, c, d, e] == [0, 0, 0, 0, 0] && f == 0xffff;
let compatible = [a, b, c, d, e, f] == [0; 6];
let nat64 = [a, b, c, d, e, f] == [0x64, 0xff9b, 0, 0, 0, 0];
(mapped || compatible || nat64).then_some(inner)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_path_must_be_path_absolute() {
for path in [
"/",
"/sendspin",
"/a/b",
"/a%20b",
"/%41",
"/a%41",
"/ab%41",
"/a:b@c",
"/~x_y-z.w",
"/a/",
"/a;b=c",
] {
assert!(is_path_absolute(path), "{path:?}");
}
for path in [
"", "sendspin", "//a", "/a?b", "/a#b", "/a b", "/a%zz", "/a%2G", "/a%", "/é",
] {
assert!(!is_path_absolute(path), "{path:?}");
}
}
#[test]
fn an_embedded_ipv4_is_extracted_whole() {
let parse = |address: &str| address.parse::<Ipv6Addr>().expect("the address parses");
for address in ["::ffff:192.0.2.1", "::c000:201", "64:ff9b::c000:201"] {
assert_eq!(
embedded_ipv4(parse(address)),
Some(std::net::Ipv4Addr::new(192, 0, 2, 1)),
"{address}"
);
}
assert_eq!(embedded_ipv4(parse("2001:db8::1")), None);
}
#[test]
fn local_addresses_are_refused_in_every_spelling() {
for address in [
"127.0.0.1:8927",
"127.0.0.1",
"[::1]:8927",
"::1",
"[::1]",
"0.0.0.0:8927",
"[::]:8927",
"[::ffff:127.0.0.1]:8927",
"[::ffff:0.0.0.0]:8927",
"[::7f00:1]:8927",
"[64:ff9b::7f00:1]:8927",
"localhost:8927",
"LOCALHOST.:8927",
"kitchen.localhost:8927",
"localhost.localdomain:8927",
"127.1:8927",
"0x7f.1:8927",
"0x7f000001:8927",
"2130706433:8927",
] {
assert!(is_local_address(address), "{address}");
}
}
#[test]
fn ordinary_addresses_are_kept() {
for address in [
"speaker.local:8927",
"my-host2:8927",
"a.localhosting.example:8927",
"host.0xfoo:8927",
"host.0x:8927",
"192.0.2.1:8927",
"192.0.2.1",
"192.0.2.1:abc",
"[1:2:3:4:5:ffff:7f00:1]:8927",
"[::1:7f00:1]:8927",
"[2001:db8::1]:8927",
"[fe80::1%eth0]:8927",
"[64:ff9b::c000:201]:8927",
"[::c000:201]:8927",
] {
assert!(!is_local_address(address), "{address}");
}
}
}