IPv4 internal address check covering RFC 1918, CGNAT (RFC 6598), and other special-use ranges that should never be reachable from sandbox egress.
(v4: Ipv4Addr)
| 191 | /// IPv4 internal address check covering RFC 1918, CGNAT (RFC 6598), and other |
| 192 | /// special-use ranges that should never be reachable from sandbox egress. |
| 193 | fn is_internal_v4(v4: Ipv4Addr) -> bool { |
| 194 | // Prefer the stable `Ipv4Addr` predicates. `is_documentation()` (RFC 5737) |
| 195 | // covers all three TEST-NET ranges, including 192.0.2.0/24 (TEST-NET-1) which |
| 196 | // the previous manual checks missed. |
| 197 | if v4.is_loopback() |
| 198 | || v4.is_private() |
| 199 | || v4.is_link_local() |
| 200 | || v4.is_unspecified() |
| 201 | || v4.is_documentation() |
| 202 | || v4.is_broadcast() |
| 203 | { |
| 204 | return true; |
| 205 | } |
| 206 | let octets = v4.octets(); |
| 207 | // The ranges below have no stable std predicate yet, so keep them manual. |
| 208 | // 100.64.0.0/10 — CGNAT / shared address space (RFC 6598; `is_shared` is |
| 209 | // unstable). Commonly used by cloud VPC peering, Tailscale, and similar overlays. |
| 210 | if octets[0] == 100 && (octets[1] & 0xC0) == 64 { |
| 211 | return true; |
| 212 | } |
| 213 | // 192.0.0.0/24 — IETF protocol assignments (RFC 6890; no stable predicate). |
| 214 | if octets[0] == 192 && octets[1] == 0 && octets[2] == 0 { |
| 215 | return true; |
| 216 | } |
| 217 | // 198.18.0.0/15 — benchmarking (RFC 2544; `is_benchmarking` is unstable). |
| 218 | if octets[0] == 198 && (octets[1] & 0xFE) == 18 { |
| 219 | return true; |
| 220 | } |
| 221 | false |
| 222 | } |
| 223 | |
| 224 | #[cfg(test)] |
| 225 | mod tests { |