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

@ -142,3 +142,127 @@ events_enabled = false
assert!(!xdp.throttle_enabled);
assert!(!xdp.events_enabled);
}
#[test]
fn prefix_detect_defaults_disabled() {
let config = Config::parse_str("").expect("empty config must parse");
assert!(!config.detect.prefix.enabled);
assert_eq!(config.detect.prefix.syn_threshold, 500);
assert_eq!(config.detect.prefix.window_secs, 10);
assert_eq!(config.detect.prefix.min_unique_sources, 16);
}
#[test]
fn prefix_detect_section_parses() {
let config = Config::parse_str(
r#"
[detect.prefix]
enabled = true
syn_threshold = 1000
window_secs = 5
min_unique_sources = 8
"#,
)
.expect("detect.prefix section must parse");
assert!(config.detect.prefix.enabled);
assert_eq!(config.detect.prefix.syn_threshold, 1000);
assert_eq!(config.detect.prefix.window_secs, 5);
assert_eq!(config.detect.prefix.min_unique_sources, 8);
}
#[test]
fn prefix_detect_zero_threshold_rejected_when_enabled() {
let err = Config::parse_str("[detect.prefix]\nenabled = true\nsyn_threshold = 0\n")
.expect_err("zero threshold must be rejected");
assert!(err.to_string().contains("detect.prefix.syn_threshold"));
}
#[test]
fn syn_challenge_requires_a_secret() {
let err = Config::parse_str("[xdp]\nsyn_challenge_enabled = true\n")
.expect_err("missing challenge secret must be rejected");
let message = err.to_string();
assert!(message.contains("challenge_secret_hex"));
assert!(message.contains("openssl rand -hex 8"));
}
#[test]
fn syn_challenge_rejects_every_placeholder_spelling() {
for secret in [
"a5a5a5a5a5a5a5a5",
"A5A5A5A5A5A5A5A5",
"0xA5A5A5A5A5A5A5A5",
"a5a5-5a5a-5a5a-a5a5",
"a5a55a5a5a5aa5a5",
"A5A5_5A5A_5A5A_A5A5",
"0xa5a55a5a5a5aa5a5",
] {
let toml = format!("[xdp]\nsyn_challenge_enabled = true\nchallenge_secret_hex = \"{secret}\"\n");
let err = Config::parse_str(&toml).expect_err("placeholder secret must be rejected");
assert!(
err.to_string().contains("placeholder"),
"secret '{secret}' must be rejected"
);
}
}
#[test]
fn syn_challenge_accepts_a_generated_secret() {
let config =
Config::parse_str("[xdp]\nsyn_challenge_enabled = true\nchallenge_secret_hex = \"9f3c71a0b2d84e65\"\n")
.expect("random challenge secret must be accepted");
assert_eq!(config.xdp.challenge_secret_hex.as_deref(), Some("9f3c71a0b2d84e65"));
}
#[test]
fn rejects_non_positive_rate_limit() {
for pps in ["0.0", "-1.0", "nan", "inf"] {
let toml = format!("[limits]\nrate_limit_pps = {pps}\n");
let err = Config::parse_str(&toml).expect_err("rate_limit_pps must be positive and finite");
assert!(
err.to_string().contains("limits.rate_limit_pps"),
"pps '{pps}' must be rejected"
);
}
}
#[test]
fn rejects_rate_limit_burst_below_one() {
for burst in ["0.0", "0.5", "nan"] {
let toml = format!("[limits]\nrate_limit_burst = {burst}\n");
let err = Config::parse_str(&toml).expect_err("rate_limit_burst must be at least 1");
assert!(
err.to_string().contains("limits.rate_limit_burst"),
"burst '{burst}' must be rejected"
);
}
}
#[test]
fn rejects_out_of_range_ban_duration() {
let err = Config::parse_str("[ban]\nban_duration_secs = 0\n").expect_err("zero ban duration must be rejected");
assert!(err.to_string().contains("ban.ban_duration_secs"));
let err = Config::parse_str("[ban]\nban_duration_secs = 31536001\n")
.expect_err("ban duration above one year must be rejected");
assert!(err.to_string().contains("ban.ban_duration_secs"));
assert!(Config::parse_str("[ban]\nban_duration_secs = 31536000\n").is_ok());
}
#[test]
fn rejects_out_of_range_pow_difficulty() {
for difficulty in ["0", "11"] {
let toml = format!("[pow]\nenabled = true\ndifficulty = {difficulty}\n");
let err = Config::parse_str(&toml).expect_err("pow.difficulty must stay in 1..=10");
assert!(
err.to_string().contains("pow.difficulty"),
"difficulty '{difficulty}' must be rejected"
);
}
assert!(Config::parse_str("[pow]\ndifficulty = 10\n").is_ok());
}
#[test]
fn rejects_zero_workers() {
let err = Config::parse_str("[workers]\ncount = 0\n").expect_err("zero workers must be rejected");
assert!(err.to_string().contains("workers.count"));
}