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:
parent
72c1f45d62
commit
bcae8901c6
11 changed files with 300 additions and 106 deletions
|
|
@ -237,3 +237,25 @@ async fn plain_connection_uses_the_transport_peer() {
|
|||
let outcome = handle_plain(&gateway, peer).await;
|
||||
assert_eq!(outcome.client_ip, Some(peer.ip()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trusted_network_overlap_checks_prefix_intersection() {
|
||||
let net: TrustedProxyCidr = "10.0.0.0/8".parse().expect("cidr");
|
||||
assert!(net.overlaps_prefix("10.1.2.0".parse().expect("ip"), 24), "same range");
|
||||
assert!(
|
||||
net.overlaps_prefix("10.0.0.0".parse().expect("ip"), 7),
|
||||
"wider prefix overlaps"
|
||||
);
|
||||
assert!(
|
||||
!net.overlaps_prefix("11.0.0.0".parse().expect("ip"), 8),
|
||||
"sibling range"
|
||||
);
|
||||
assert!(
|
||||
!net.overlaps_prefix("10.0.0.0".parse().expect("ip"), 0),
|
||||
"/0 is rejected"
|
||||
);
|
||||
|
||||
let v6: TrustedProxyCidr = "fd00::/8".parse().expect("cidr");
|
||||
assert!(v6.overlaps_prefix("fd12::".parse().expect("ip"), 16));
|
||||
assert!(!v6.overlaps_prefix("2001:db8::".parse().expect("ip"), 32));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//! Integration tests for subnet-level detection: tracker, detector and verdicts.
|
||||
|
||||
use rampart::config::Config;
|
||||
use rampart::config::{Config, TrustedProxyCidr};
|
||||
use rampart::engine::subnet::monitor::enforce_tracker_verdicts;
|
||||
use rampart::engine::subnet_tracker::SubnetTracker;
|
||||
use rampart::filter::blacklist::Blacklist;
|
||||
|
|
@ -299,7 +299,7 @@ fn block_verdict(prefix: IpAddr) -> PrefixVerdict {
|
|||
fn trusted_tracker_block_reaches_the_blacklist_with_the_configured_ttl() {
|
||||
let blacklist = Blacklist::new();
|
||||
let verdicts = [block_verdict(v4([203, 0, 113, 0]))];
|
||||
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true);
|
||||
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true, &[]);
|
||||
assert_eq!(banned, 1);
|
||||
assert!(blacklist.is_blocked(v4([203, 0, 113, 0])));
|
||||
}
|
||||
|
|
@ -308,7 +308,7 @@ fn trusted_tracker_block_reaches_the_blacklist_with_the_configured_ttl() {
|
|||
fn untrusted_tracker_block_only_warns() {
|
||||
let blacklist = Blacklist::new();
|
||||
let verdicts = [block_verdict(v4([198, 51, 100, 0]))];
|
||||
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), false);
|
||||
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), false, &[]);
|
||||
assert_eq!(banned, 0);
|
||||
assert!(blacklist.is_empty());
|
||||
}
|
||||
|
|
@ -317,7 +317,7 @@ fn untrusted_tracker_block_only_warns() {
|
|||
fn tracker_ban_uses_the_given_duration() {
|
||||
let blacklist = Blacklist::new();
|
||||
let verdicts = [block_verdict(v4([192, 0, 2, 0]))];
|
||||
enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_millis(1), true);
|
||||
enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_millis(1), true, &[]);
|
||||
std::thread::sleep(Duration::from_millis(2));
|
||||
assert!(
|
||||
!blacklist.is_blocked(v4([192, 0, 2, 0])),
|
||||
|
|
@ -331,7 +331,7 @@ fn non_block_verdicts_do_not_ban() {
|
|||
let mut verdict = block_verdict(v4([203, 0, 113, 0]));
|
||||
verdict.verdict = SubnetVerdict::Challenge;
|
||||
assert_eq!(
|
||||
enforce_tracker_verdicts(&[verdict], &blacklist, Duration::from_secs(60), true),
|
||||
enforce_tracker_verdicts(&[verdict], &blacklist, Duration::from_secs(60), true, &[]),
|
||||
0
|
||||
);
|
||||
assert!(blacklist.is_empty());
|
||||
|
|
@ -375,3 +375,23 @@ fn prefix_key_normalises_ipv4_mapped_addresses() {
|
|||
assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(prefix_of(mapped)));
|
||||
assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(v4([1, 2, 3, 0])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracker_block_is_suppressed_when_a_trusted_proxy_lives_in_the_prefix() {
|
||||
let blacklist = Blacklist::new();
|
||||
let verdicts = [block_verdict(v4([10, 9, 0, 0]))];
|
||||
let trusted: Vec<TrustedProxyCidr> = vec!["10.9.0.5/32".parse().expect("trusted cidr")];
|
||||
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true, &trusted);
|
||||
assert_eq!(banned, 0, "a ban covering a trusted proxy must be suppressed");
|
||||
assert!(blacklist.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracker_block_still_applies_when_trusted_networks_do_not_overlap() {
|
||||
let blacklist = Blacklist::new();
|
||||
let verdicts = [block_verdict(v4([10, 9, 0, 0]))];
|
||||
let trusted: Vec<TrustedProxyCidr> = vec!["192.0.2.0/24".parse().expect("trusted cidr")];
|
||||
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true, &trusted);
|
||||
assert_eq!(banned, 1);
|
||||
assert!(blacklist.is_blocked(v4([10, 9, 0, 7])));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -446,3 +446,33 @@ fn alerts_fire_only_on_state_transitions() {
|
|||
assert!(alerts[0].message.contains("attack started"));
|
||||
assert!(alerts[1].message.contains("attack ended"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_ban_never_targets_a_trusted_proxy_network() {
|
||||
let reputation = Arc::new(IpReputation::new());
|
||||
let blacklist = Arc::new(Blacklist::new());
|
||||
let trusted: Vec<rampart::config::TrustedProxyCidr> = vec!["10.0.0.0/8".parse().expect("trusted cidr")];
|
||||
let hook =
|
||||
TrafficHook::new(Arc::clone(&reputation), Arc::clone(&blacklist), autoban_cfg(true), 60).with_trusted(trusted);
|
||||
let inside = ip([10, 1, 2, 3]);
|
||||
|
||||
for _ in 0..6 {
|
||||
reputation.record_bad(inside);
|
||||
hook.on_connection_end(inside);
|
||||
}
|
||||
assert!(
|
||||
reputation.score(inside) <= -50,
|
||||
"reputation must fall below the threshold"
|
||||
);
|
||||
assert_eq!(hook.ban_offenders(), 0, "trusted addresses are never banned");
|
||||
|
||||
let public = ip([198, 51, 100, 9]);
|
||||
for _ in 0..6 {
|
||||
reputation.record_bad(public);
|
||||
hook.on_connection_end(public);
|
||||
}
|
||||
assert!(
|
||||
blacklist.is_blocked(public),
|
||||
"non-trusted offenders are still banned by the reputation path"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue