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:
parent
14dcaccdbf
commit
c1a076a4a3
57 changed files with 4193 additions and 1171 deletions
|
|
@ -57,3 +57,201 @@ fn blacklist_remove_clears_ban() {
|
|||
blacklist.remove(ip(7));
|
||||
assert!(!blacklist.is_blocked(ip(7)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blacklist_expired_cleanup_keeps_fresh_reban() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add(ip(8), Duration::ZERO, "stale");
|
||||
assert!(!blacklist.is_blocked(ip(8)), "zero-duration ban is already expired");
|
||||
|
||||
blacklist.add(ip(8), Duration::from_secs(60), "fresh");
|
||||
assert!(
|
||||
blacklist.is_blocked(ip(8)),
|
||||
"expired-entry cleanup must not delete a fresh ban"
|
||||
);
|
||||
assert_eq!(blacklist.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blacklist_expired_cleanup_does_not_race_with_reban() {
|
||||
let blacklist = std::sync::Arc::new(Blacklist::new());
|
||||
let reader = blacklist.clone();
|
||||
let handle = std::thread::spawn(move || {
|
||||
for _ in 0..2000 {
|
||||
let _ = reader.is_blocked(ip(9));
|
||||
std::thread::yield_now();
|
||||
}
|
||||
});
|
||||
for _ in 0..2000 {
|
||||
blacklist.add(ip(9), Duration::ZERO, "stale");
|
||||
blacklist.add(ip(9), Duration::from_secs(60), "fresh");
|
||||
}
|
||||
handle.join().expect("reader thread must not panic");
|
||||
assert!(
|
||||
blacklist.is_blocked(ip(9)),
|
||||
"a concurrently refreshed ban must survive stale cleanup"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blacklist_clamps_huge_durations_without_panicking() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add(ip(10), Duration::from_secs(u64::MAX), "hostile");
|
||||
blacklist.add_redis(ip(11), Duration::from_secs(u64::MAX), "hostile");
|
||||
|
||||
assert!(blacklist.is_blocked(ip(10)), "local ban must be clamped, not dropped");
|
||||
assert!(blacklist.is_blocked(ip(11)), "redis ban must be clamped, not dropped");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replace_redis_keeps_new_bans_and_local_entries() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add(ip(12), Duration::from_secs(60), "local");
|
||||
blacklist.add_redis(ip(13), Duration::from_secs(60), "stale-upstream");
|
||||
|
||||
blacklist.replace_redis([
|
||||
(ip(14), Duration::from_secs(60), "upstream".to_string()),
|
||||
(ip(15), Duration::from_secs(60), "upstream".to_string()),
|
||||
]);
|
||||
|
||||
assert!(blacklist.is_blocked(ip(14)), "new upstream ban must apply immediately");
|
||||
assert!(blacklist.is_blocked(ip(15)), "new upstream ban must apply immediately");
|
||||
assert!(!blacklist.is_blocked(ip(13)), "ban removed upstream must stop applying");
|
||||
assert!(
|
||||
blacklist.is_blocked(ip(12)),
|
||||
"local bans must survive a Redis reconcile"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replace_redis_can_clear_every_mirrored_ban() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add_redis(ip(16), Duration::from_secs(60), "stale-upstream");
|
||||
|
||||
blacklist.replace_redis(std::iter::empty());
|
||||
|
||||
assert!(!blacklist.is_blocked(ip(16)));
|
||||
assert!(blacklist.is_empty());
|
||||
}
|
||||
|
||||
fn numbered(index: u32) -> IpAddr {
|
||||
IpAddr::V4(Ipv4Addr::from(0x0a00_0000 + index))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rate_limiter_enforces_a_hard_bucket_cap() {
|
||||
let limiter = RateLimiter::with_capacity(1.0, 1.0, 4, Duration::from_secs(600));
|
||||
let admitted = (0..100).filter(|index| limiter.check(numbered(*index))).count();
|
||||
assert_eq!(admitted, 4, "only max_buckets new sources may be admitted");
|
||||
assert_eq!(limiter.len(), 4);
|
||||
assert!(
|
||||
!limiter.check(numbered(500)),
|
||||
"a new source fails closed once the table is saturated"
|
||||
);
|
||||
assert_eq!(limiter.len(), 4, "a refused source must not allocate a bucket");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rate_limiter_sweep_frees_capacity_and_keeps_active() {
|
||||
let limiter = RateLimiter::with_capacity(1.0, 10.0, 2, Duration::from_millis(100));
|
||||
assert!(limiter.check(ip(1)));
|
||||
assert!(limiter.check(ip(2)));
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
assert!(limiter.check(ip(2)), "the active source still has tokens");
|
||||
|
||||
for _ in 0..8 {
|
||||
limiter.sweep();
|
||||
}
|
||||
assert_eq!(limiter.len(), 1, "idle bucket evicted, active bucket kept");
|
||||
assert!(limiter.check(ip(3)), "eviction must free a slot for a new source");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rate_limiter_sweep_is_incremental() {
|
||||
let limiter = RateLimiter::with_capacity(1000.0, 1.0, 4096, Duration::from_millis(10));
|
||||
for index in 0..4096 {
|
||||
assert!(limiter.check(numbered(index)));
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
|
||||
let first = limiter.sweep();
|
||||
assert!(first > 0, "the first sweep must reclaim idle buckets");
|
||||
assert!(first < 4096, "one sweep must not scan the whole table");
|
||||
assert!(!limiter.is_empty(), "a single sweep leaves work for the next tick");
|
||||
|
||||
for _ in 0..8 {
|
||||
limiter.sweep();
|
||||
}
|
||||
assert!(limiter.is_empty(), "a full shard cycle reclaims every idle bucket");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cidr_ban_covers_the_whole_range() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add_cidr(
|
||||
IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)),
|
||||
24,
|
||||
Duration::from_secs(60),
|
||||
"subnet",
|
||||
);
|
||||
assert!(blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 1))));
|
||||
assert!(blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 255))));
|
||||
assert!(!blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(10, 1, 3, 1))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cidr_ban_normalises_host_bits_and_v4_mapped_addresses() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add_cidr(
|
||||
IpAddr::V4(Ipv4Addr::new(10, 7, 9, 200)),
|
||||
24,
|
||||
Duration::from_secs(60),
|
||||
"subnet",
|
||||
);
|
||||
let mapped: IpAddr = "::ffff:10.7.9.1".parse().expect("mapped ipv4");
|
||||
assert!(blacklist.is_blocked(mapped), "/24 ban must cover the mapped form too");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cidr_ban_expires() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add_cidr(
|
||||
IpAddr::V4(Ipv4Addr::new(10, 8, 0, 0)),
|
||||
16,
|
||||
Duration::from_millis(1),
|
||||
"subnet",
|
||||
);
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
assert!(!blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(10, 8, 0, 1))));
|
||||
blacklist.clear_expired();
|
||||
assert!(blacklist.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_cidr_prefix_is_ignored() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add_cidr(
|
||||
IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)),
|
||||
0,
|
||||
Duration::from_secs(60),
|
||||
"subnet",
|
||||
);
|
||||
blacklist.add_cidr(
|
||||
IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)),
|
||||
33,
|
||||
Duration::from_secs(60),
|
||||
"subnet",
|
||||
);
|
||||
assert!(blacklist.is_empty(), "a /0 or /33 ban must not be installed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ipv6_cidr_ban_covers_the_range() {
|
||||
let blacklist = Blacklist::new();
|
||||
let prefix: IpAddr = "fd00:1234:5678:9abc::".parse().expect("ipv6 prefix");
|
||||
blacklist.add_cidr(prefix, 64, Duration::from_secs(60), "subnet");
|
||||
let inside: IpAddr = "fd00:1234:5678:9abc::42".parse().expect("ipv6 inside");
|
||||
let outside: IpAddr = "fd00:1234:5678:9abd::1".parse().expect("ipv6 outside");
|
||||
assert!(blacklist.is_blocked(inside));
|
||||
assert!(!blacklist.is_blocked(outside));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue