MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / parse_allowed_ips

Function parse_allowed_ips

crates/openshell-supervisor-network/src/proxy.rs:2671–2727  ·  view source on GitHub ↗

Parse CIDR/IP strings into `IpNet` values, rejecting invalid entries and entries that overlap always-blocked ranges (loopback, link-local, unspecified). Returns parsed networks on success, or an error describing which entries are invalid or always-blocked. Logs a warning for overly broad CIDRs that are not outright blocked.

(raw: &[String])

Source from the content-addressed store, hash-verified

2669/// are invalid or always-blocked. Logs a warning for overly broad CIDRs
2670/// that are not outright blocked.
2671fn parse_allowed_ips(raw: &[String]) -> std::result::Result<Vec<ipnet::IpNet>, String> {
2672 use openshell_core::net::is_always_blocked_net;
2673
2674 let mut nets = Vec::with_capacity(raw.len());
2675 let mut errors = Vec::new();
2676
2677 for entry in raw {
2678 // Try as CIDR first, then as bare IP (convert to /32 or /128)
2679 let parsed = entry.parse::<ipnet::IpNet>().or_else(|_| {
2680 entry
2681 .parse::<IpAddr>()
2682 .map(|ip| match ip {
2683 IpAddr::V4(v4) => ipnet::IpNet::V4(ipnet::Ipv4Net::from(v4)),
2684 IpAddr::V6(v6) => ipnet::IpNet::V6(ipnet::Ipv6Net::from(v6)),
2685 })
2686 .map_err(|_| ())
2687 });
2688
2689 match parsed {
2690 Ok(n) => {
2691 // Reject entries that overlap always-blocked ranges — these
2692 // would be silently denied at runtime by is_always_blocked_ip
2693 // and cause confusing UX (accepted in policy, never works).
2694 if is_always_blocked_net(n) {
2695 errors.push(format!(
2696 "allowed_ips entry {entry} falls within always-blocked range \
2697 (loopback/link-local/unspecified); remove this entry — \
2698 SSRF hardening prevents traffic to these destinations \
2699 regardless of policy"
2700 ));
2701 continue;
2702 }
2703
2704 if n.prefix_len() < MIN_SAFE_PREFIX_LEN {
2705 let event = NetworkActivityBuilder::new(openshell_ocsf::ctx::ctx())
2706 .activity(ActivityId::Other)
2707 .severity(SeverityId::Medium)
2708 .message(format!(
2709 "allowed_ips entry has a very broad CIDR {n} (/{}) < /{MIN_SAFE_PREFIX_LEN}; \
2710 this may expose control-plane services on the same network",
2711 n.prefix_len()
2712 ))
2713 .build();
2714 ocsf_emit!(event);
2715 }
2716 nets.push(n);
2717 }
2718 Err(()) => errors.push(format!("invalid CIDR/IP in allowed_ips: {entry}")),
2719 }
2720 }
2721
2722 if errors.is_empty() {
2723 Ok(nets)
2724 } else {
2725 Err(errors.join("; "))
2726 }
2727}
2728

Calls 7

is_always_blocked_netFunction · 0.85
lenMethod · 0.80
pushMethod · 0.80
messageMethod · 0.80
ctxFunction · 0.50
buildMethod · 0.45
is_emptyMethod · 0.45