fix(core): close auth bypass, hangs and memory leaks found by review

Security:
- An empty API_PASSWORD passed validation (only the literal "changeme" was
  rejected) and verify_password("", "") returned true, so a login with an
  empty password minted an admin token. Require >= 16 characters and >= 32
  non-whitespace bytes for JWT_SECRET; verify rejects an empty stored secret.
- syn_challenge_enabled required a real challenge_secret_hex: the public
  placeholder from config.h is now rejected in every spelling.
- docker-compose: Redis/ClickHouse/Grafana are loopback-only and every
  password uses ${VAR:?} instead of a default. An exposed Redis was a way to
  lift bans and publish unbans to the whole fleet with one redis-cli.
- Empty CORS_ORIGIN no longer means "allow any origin".
- ban_cidr / userspace blacklist validate and mask prefixes; CIDR bans are
  stored as ranges, so a /24 or /64 ban actually covers the subnet (before,
  only the network address was blocked).

Availability:
- ClickHouse client has connect/request timeouts and no mutex held across
  await: a hung analytics backend can no longer freeze the ban path. Events
  go through a bounded buffer with a drop counter.
- Redis uses a ConnectionManager with per-command timeouts; pubsub and
  reconcile reset the backoff after success; publish cannot block shutdown.
- Ban durations from pubsub are clamped and use checked_add; the sync task
  survives panics instead of silently dying.
- Blacklist TOCTOU (remove_if) and replace_redis no longer empties the table
  during a Redis reconnect.
- RateLimiter and reputation have hard caps with incremental eviction instead
  of an unbounded DashMap and a stop-the-world retain.
- The attack detector scores before updating its baseline and needs several
  quiet windows to leave UnderAttack, so a sustained flood keeps banning new
  offenders instead of ending after one tick.
- accept() backs off on EMFILE/ENFILE, shutdown Err is treated as shutdown,
  a global semaphore bounds in-flight connections, rate limiting runs before
  PoW, whitelisted IPs are never banned, and connect/handshake/relay have
  timeouts with an idle budget.
- PoW uses the configured difficulty as a floor, the clamp is enforced, and
  nonce framing survives split segments and coalesced handshake bytes.
- Subnet tracker memory is capped (prefixes and per-prefix sources) and the
  monitor bans with the configured TTL.

CI and docs:
- CI runs the test suite with --no-default-features --features protocol-http;
  make ebpf fails on errors; the secrets gate catches weak compose defaults.
- doctor skips protected endpoints without a token and exits non-zero on
  failure; a drain timeout returns exit code 1.
- TODO §2b records the review findings, what is fixed and what is deferred;
  configuration/testing docs describe the new XDP and secret semantics.
This commit is contained in:
Loki 2026-11-16 12:06:23 +01:00
parent 14dcaccdbf
commit c1a076a4a3
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
57 changed files with 4193 additions and 1171 deletions

View file

@ -1,12 +1,16 @@
//! Integration tests for subnet-level detection: tracker, detector and verdicts.
use rampart::config::Config;
use rampart::engine::subnet::monitor::enforce_tracker_verdicts;
use rampart::engine::subnet_tracker::SubnetTracker;
use rampart::filter::blacklist::Blacklist;
use rampart::traffic::prefix::{
PrefixKey, PrefixSnapshot, PrefixStatsSource, PrefixStatsVal, SubnetDetector, SubnetVerdict, prefix_of,
PrefixKey, PrefixSnapshot, PrefixStatsSource, PrefixStatsVal, PrefixVerdict, SubnetDetector, SubnetVerdict,
prefix_of,
};
use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::time::Duration;
fn v4(octets: [u8; 4]) -> IpAddr {
IpAddr::V4(Ipv4Addr::from(octets))
@ -194,6 +198,145 @@ fn spoof_gate_downgrades_block_to_challenge() {
assert_eq!(verdict_for(&detector, &unknown), SubnetVerdict::Challenge);
}
#[test]
fn tracker_groups_connections_by_prefix() {
let tracker = SubnetTracker::new();
tracker.record_connection(v4([203, 0, 113, 1]));
tracker.record_connection(v4([203, 0, 113, 2]));
tracker.record_connection(v4([198, 51, 100, 9]));
let by_prefix: HashMap<IpAddr, PrefixSnapshot> = tracker.take_snapshot().into_iter().collect();
assert_eq!(by_prefix.len(), 2);
assert_eq!(by_prefix[&v4([203, 0, 113, 0])].syn_count, 2);
assert_eq!(by_prefix[&v4([203, 0, 113, 0])].unique_sources, 2);
assert_eq!(by_prefix[&v4([198, 51, 100, 0])].syn_count, 1);
}
#[test]
fn tracker_snapshot_drains_window() {
let tracker = SubnetTracker::new();
tracker.record_connection(v4([10, 0, 0, 1]));
assert_eq!(tracker.take_snapshot().len(), 1);
assert!(tracker.take_snapshot().is_empty());
}
#[test]
fn tracker_mapped_addresses_share_v4_prefix() {
let tracker = SubnetTracker::new();
tracker.record_connection(v4([203, 0, 113, 5]));
// ::ffff:203.0.113.6
let mapped = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xcb00, 0x7106);
tracker.record_connection(IpAddr::V6(mapped));
let snaps = tracker.take_snapshot();
assert_eq!(snaps.len(), 1);
assert_eq!(snaps[0].0, v4([203, 0, 113, 0]));
assert_eq!(snaps[0].1.unique_sources, 2);
}
#[test]
fn tracker_prefix_cap_drops_new_prefixes_and_counts_them() {
let tracker = SubnetTracker::with_limits(2, 8);
tracker.record_connection(v4([10, 0, 0, 1]));
tracker.record_connection(v4([10, 0, 1, 1]));
tracker.record_connection(v4([10, 0, 2, 1]));
assert_eq!(tracker.dropped_prefixes(), 1);
// Existing prefixes keep accumulating even while the table is full.
tracker.record_connection(v4([10, 0, 0, 2]));
let snaps: HashMap<IpAddr, PrefixSnapshot> = tracker.take_snapshot().into_iter().collect();
assert_eq!(snaps.len(), 2);
assert_eq!(snaps[&v4([10, 0, 0, 0])].syn_count, 2);
assert!(
!snaps.contains_key(&v4([10, 0, 2, 0])),
"over-cap prefix was not stored"
);
}
#[test]
fn tracker_source_cap_bounds_per_prefix_memory() {
let tracker = SubnetTracker::with_limits(8, 3);
for last in 1..=100u8 {
tracker.record_connection(v4([203, 0, 113, last]));
}
assert_eq!(
tracker.dropped_sources(),
97,
"only the first three distinct sources are stored"
);
let snaps = tracker.take_snapshot();
assert_eq!(snaps.len(), 1);
assert_eq!(snaps[0].1.syn_count, 100, "the aggregate counter is not capped");
assert_eq!(snaps[0].1.unique_sources, 3);
}
#[test]
fn tracker_source_cap_counts_only_new_addresses() {
let tracker = SubnetTracker::with_limits(1, 2);
tracker.record_connection(v4([198, 51, 100, 1]));
tracker.record_connection(v4([198, 51, 100, 2]));
tracker.record_connection(v4([198, 51, 100, 3]));
assert_eq!(tracker.dropped_sources(), 1);
// Repeated traffic from a remembered source is not a dropped new source.
tracker.record_connection(v4([198, 51, 100, 1]));
assert_eq!(tracker.dropped_sources(), 1);
tracker.record_connection(v4([198, 51, 100, 4]));
assert_eq!(tracker.dropped_sources(), 2);
}
fn block_verdict(prefix: IpAddr) -> PrefixVerdict {
PrefixVerdict {
prefix,
verdict: SubnetVerdict::Block,
snapshot: PrefixSnapshot {
syn_count: 4000,
unique_sources: 64,
..PrefixSnapshot::default()
},
}
}
#[test]
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);
assert_eq!(banned, 1);
assert!(blacklist.is_blocked(v4([203, 0, 113, 0])));
}
#[test]
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);
assert_eq!(banned, 0);
assert!(blacklist.is_empty());
}
#[test]
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);
std::thread::sleep(Duration::from_millis(2));
assert!(
!blacklist.is_blocked(v4([192, 0, 2, 0])),
"ban must expire with its TTL"
);
}
#[test]
fn non_block_verdicts_do_not_ban() {
let blacklist = Blacklist::new();
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),
0
);
assert!(blacklist.is_empty());
}
#[test]
fn evaluate_skips_empty_prefixes() {
let detector = SubnetDetector::new(100, 0);
@ -213,3 +356,22 @@ fn evaluate_skips_empty_prefixes() {
assert_eq!(verdicts[0].prefix, v4([10, 0, 1, 0]));
assert_eq!(verdicts[0].verdict, SubnetVerdict::Block);
}
#[test]
fn prefix_key_normalises_ipv4_mapped_addresses() {
// ::ffff:1.2.3.4
let mapped = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304));
let plain = v4([1, 2, 3, 4]);
assert_eq!(
PrefixKey::new(mapped),
PrefixKey::new(plain),
"a mapped client must produce one key in XDP prefix stats and the user-space tracker"
);
assert_eq!(
PrefixKey::new(mapped).family,
rampart::traffic::prefix::PREFIX_FAMILY_V4
);
assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(prefix_of(mapped)));
assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(v4([1, 2, 3, 0])));
}