fix: never ban a trusted proxy network from any ban source

The gateway already refused to ban addresses from proxy_protocol.trusted_cidrs,
but the traffic-intelligence auto-ban and the subnet monitor still did, so a
trusted load balancer living inside a banned /24 (or below the reputation
threshold) could be knocked out together with its clients.

- TrustedProxyCidr moved to src/config/trusted.rs (the edge section file hit
  the 250-line gate) and gained `overlaps_prefix`: a ban is suppressed when the
  banned prefix intersects a trusted network.
- TrafficHook::with_trusted suppresses reputation and offender bans; its ban()
  now reports whether the ban was installed so `ban_offenders` counts real
  bans.
- The subnet monitor takes a MonitorSetup bundle (spawn had 8 arguments) and
  suppresses both userspace CIDR bans and XDP prefix bans that overlap a
  trusted network, logging a warning instead.
- IPv4-mapped addresses are normalised in `contains` too.
- Tests: overlap maths (v4/v6, /0 rejected), tracker suppression vs unrelated
  networks, and auto-ban skipping a trusted range while still banning public
  offenders.
This commit is contained in:
Loki 2026-11-16 13:03:50 +01:00
parent 72c1f45d62
commit bcae8901c6
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
11 changed files with 300 additions and 106 deletions

View file

@ -1,8 +1,7 @@
//! Edge sections: bind, backend, workers, limits, ban, PoW and PROXY protocol.
use crate::config::trusted::TrustedProxyCidr;
use serde::{Deserialize, Deserializer};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
/// `[bind]` section: listen address and port of the edge node.
#[derive(Debug, Clone, Deserialize)]
@ -159,66 +158,6 @@ fn default_pow_difficulty() -> u8 {
4
}
/// CIDR network allowed to send inbound PROXY protocol headers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TrustedProxyCidr {
network: IpAddr,
prefix_len: u8,
}
impl TrustedProxyCidr {
/// Returns whether `ip` belongs to this network.
#[must_use]
pub fn contains(&self, ip: IpAddr) -> bool {
// Masks are family-specific: a prefix above 32 would underflow the v4 shift.
let prefix = self.prefix_len;
match (self.network, ip) {
(IpAddr::V4(net), IpAddr::V4(ip)) => u32::from(net) & mask_v4(prefix) == u32::from(ip) & mask_v4(prefix),
(IpAddr::V6(net), IpAddr::V6(ip)) => u128::from(net) & mask_v6(prefix) == u128::from(ip) & mask_v6(prefix),
_ => false,
}
}
}
impl FromStr for TrustedProxyCidr {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let (address, prefix) = value.split_once('/').map_or((value, None), |(a, p)| (a, Some(p)));
let network = IpAddr::from_str(address).map_err(|_| format!("invalid trusted proxy CIDR: {value}"))?;
let max_prefix = if network.is_ipv4() { 32 } else { 128 };
let prefix_len = prefix
.map_or(Ok(max_prefix), |prefix| prefix.parse::<u8>())
.map_err(|_| format!("invalid trusted proxy CIDR prefix: {value}"))?;
if prefix_len > max_prefix {
return Err(format!("trusted proxy CIDR prefix is too large: {value}"));
}
let network = match network {
IpAddr::V4(ip) => IpAddr::V4(Ipv4Addr::from(u32::from(ip) & mask_v4(prefix_len))),
IpAddr::V6(ip) => IpAddr::V6(Ipv6Addr::from(u128::from(ip) & mask_v6(prefix_len))),
};
Ok(Self { network, prefix_len })
}
}
impl<'de> Deserialize<'de> for TrustedProxyCidr {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
value.parse().map_err(serde::de::Error::custom)
}
}
fn mask_v4(prefix: u8) -> u32 {
if prefix == 0 { 0 } else { u32::MAX << (32 - prefix) }
}
fn mask_v6(prefix: u8) -> u128 {
if prefix == 0 { 0 } else { u128::MAX << (128 - prefix) }
}
/// `[proxy_protocol]` section: inbound PROXY protocol ingestion.
#[derive(Debug, Clone, Deserialize)]
pub struct ProxyProtocolConfig {

View file

@ -11,7 +11,5 @@ pub use detect::MinecraftProtocolConfig;
#[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))]
pub use detect::ProtocolConfig;
pub use detect::{DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig};
pub use edge::{
BackendConfig, BanConfig, BindConfig, LimitsConfig, PowConfig, ProxyProtocolConfig, TrustedProxyCidr, WorkerConfig,
};
pub use edge::{BackendConfig, BanConfig, BindConfig, LimitsConfig, PowConfig, ProxyProtocolConfig, WorkerConfig};
pub use platform::{LoggingConfig, MetricsConfig, StoreConfig, XdpConfig};