Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SPSearch9// File : crates/policy/src/net.rs10// Module : crates11// Repository : https://git.spboucher.ai/spweb12// Project Website : https://search.spboucher.ai13// Created : 2026-11-2714// Last Modified : 2026-11-2715//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author : Simon-Pierre Boucher21// Position : Professor of Finance22// Institution : Université du Québec en Outaouais (UQO)23// Department : Département des sciences administratives24// Organization : SP Boucher25//26// Website : https://spboucher.ai27// Git : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General : info@spboucher.ai34// Professional : contact@spboucher.ai35// Direct : simon@spboucher.ai36// Research : research@spboucher.ai37// Development : dev@spboucher.ai38// Support : support@spboucher.ai39// Security/Admin : admin@spboucher.ai40// Notifications : notifications@spboucher.ai41//42// Preferred Contact: contact@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification : Public67// Distribution : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License : None specified (no license file in this repository)81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788//! Garde SSRF : seules les adresses IP publiques et routables sont permises.8990use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};9192pub fn ipv4_is_public(ip: Ipv4Addr) -> bool {93 let o = ip.octets();94 !(o[0] == 0 // 0.0.0.0/895 || o[0] == 10 // 10/896 || o[0] == 127 // bouclage97 || (o[0] == 100 && (o[1] & 0xC0) == 64) // 100.64/10 CGNAT (et Tailscale)98 || (o[0] == 169 && o[1] == 254) // lien local, métadonnées cloud99 || (o[0] == 172 && (o[1] & 0xF0) == 16) // 172.16/12100 || (o[0] == 192 && o[1] == 168) // 192.168/16101 || (o[0] == 192 && o[1] == 0 && o[2] == 0) // 192.0.0/24102 || (o[0] == 192 && o[1] == 0 && o[2] == 2) // TEST-NET-1103 || (o[0] == 192 && o[1] == 88 && o[2] == 99) // relais 6to4104 || (o[0] == 198 && (o[1] & 0xFE) == 18) // 198.18/15 bancs d'essai105 || (o[0] == 198 && o[1] == 51 && o[2] == 100) // TEST-NET-2106 || (o[0] == 203 && o[1] == 0 && o[2] == 113) // TEST-NET-3107 || o[0] >= 224) // multidiffusion, réservé, diffusion108}109110pub fn ipv6_is_public(ip: Ipv6Addr) -> bool {111 if let Some(v4) = ip.to_ipv4_mapped() {112 return ipv4_is_public(v4);113 }114 let s = ip.segments();115 // ::/96 compatible IPv4 (obsolète), 64:ff9b::/96 NAT64, 2002::/16 6to4 : vérifier l'IPv4 embarquée116 if s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0 && s[4] == 0 && s[5] == 0 {117 return false; // ::, ::1, ::a.b.c.d118 }119 if s[0] == 0x64 && s[1] == 0xff9b {120 return ipv4_is_public(Ipv4Addr::new((s[6] >> 8) as u8, s[6] as u8, (s[7] >> 8) as u8, s[7] as u8));121 }122 if s[0] == 0x2002 {123 return ipv4_is_public(Ipv4Addr::new((s[1] >> 8) as u8, s[1] as u8, (s[2] >> 8) as u8, s[2] as u8));124 }125 // Seul l'espace d'adressage global unicast 2000::/3 est accepté…126 if (s[0] & 0xE000) != 0x2000 {127 return false; // fc00::/7 ULA, fe80::/10 lien local, ff00::/8 multidiffusion, etc.128 }129 // …moins les plages de documentation et spéciales130 !((s[0] == 0x2001 && s[1] == 0x0db8) // documentation131 || (s[0] == 0x2001 && s[1] < 0x0200) // 2001::/23 (Teredo, ORCHID, bancs)132 || (s[0] == 0x3fff && (s[1] & 0xF000) == 0)) // 3fff::/20 documentation133}134135pub fn ip_is_public(ip: IpAddr) -> bool {136 match ip {137 IpAddr::V4(v) => ipv4_is_public(v),138 IpAddr::V6(v) => ipv6_is_public(v),139 }140}141142/// Refuse les hôtes écrits comme adresse IP (y compris formes encodées : décimal, octal, hexadécimal).143/// SPBot n'explore que des noms de domaine publics.144pub fn host_is_ip_literal(host: &str) -> bool {145 let h = host.trim_matches(|c| c == '[' || c == ']');146 if h.parse::<IpAddr>().is_ok() {147 return true;148 }149 // 2130706433, 0x7f000001, 0177.0.0.1, 127.1 …150 let parts: Vec<&str> = h.split('.').collect();151 !parts.is_empty()152 && parts.len() <= 4153 && parts.iter().all(|p| {154 !p.is_empty()155 && (p.chars().all(|c| c.is_ascii_digit())156 || (p.len() > 2 && (p.starts_with("0x") || p.starts_with("0X")) && p[2..].chars().all(|c| c.is_ascii_hexdigit())))157 })158}159160#[cfg(test)]161mod tests {162 use super::*;163 #[test]164 fn v4() {165 for bad in ["127.0.0.1", "10.0.0.1", "172.16.5.4", "172.31.255.255", "192.168.2.1", "169.254.169.254", "100.64.0.1",166 "100.125.225.117", "0.0.0.0", "224.0.0.1", "255.255.255.255", "198.18.0.1", "192.0.2.1", "10.67.0.1"] {167 assert!(!ip_is_public(bad.parse().unwrap()), "{}", bad);168 }169 for ok in ["8.8.8.8", "1.1.1.1", "172.32.0.1", "100.63.0.1", "100.128.0.1", "51.161.112.61"] {170 assert!(ip_is_public(ok.parse().unwrap()), "{}", ok);171 }172 }173 #[test]174 fn v6() {175 for bad in ["::1", "::", "fe80::1", "fc00::1", "fd12:3456::1", "ff02::1", "::ffff:127.0.0.1", "::ffff:10.0.0.1",176 "::ffff:169.254.169.254", "64:ff9b::7f00:1", "2002:7f00:1::1", "2002:c0a8:101::1", "2001:db8::1", "::127.0.0.1", "2001::1"] {177 assert!(!ip_is_public(bad.parse().unwrap()), "{}", bad);178 }179 for ok in ["2606:4700:4700::1111", "2a00:1450:4001::64", "::ffff:8.8.8.8", "64:ff9b::808:808"] {180 assert!(ip_is_public(ok.parse().unwrap()), "{}", ok);181 }182 }183 #[test]184 fn literals() {185 for lit in ["127.0.0.1", "2130706433", "0x7f000001", "0177.0.0.1", "127.1", "[::1]", "0x7f.0.0.1", "017700000001"] {186 assert!(host_is_ip_literal(lit), "{}", lit);187 }188 for ok in ["example.org", "1.example.org", "0x.example", "123.ca"] {189 assert!(!host_is_ip_literal(ok), "{}", ok);190 }191 }192}193