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
|
|
@ -1,7 +1,11 @@
|
|||
//! PoW roundtrip tests: solver output must satisfy the verifier, replays and
|
||||
//! garbage must not.
|
||||
//! garbage must not, and the nonce exchange must survive TCP framing quirks.
|
||||
|
||||
use rampart::engine::challenge::{Challenge, solve};
|
||||
use rampart::engine::challenge::{Challenge, enforce, solve};
|
||||
use std::net::IpAddr;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::time::{Duration, sleep};
|
||||
|
||||
/// Fixed 32-byte token: sha256(hex(token) || "garbage") starts with "ebc1",
|
||||
/// so "garbage" provably fails the difficulty-2 check (first two hex chars
|
||||
|
|
@ -9,6 +13,8 @@ use rampart::engine::challenge::{Challenge, solve};
|
|||
/// per run, since any digest lands in the accepted prefix with p=(4/16)^2.
|
||||
const FIXED_TOKEN: [u8; 32] = *b"rampart-pow-fixed-token-test-v1!";
|
||||
|
||||
const PEER: IpAddr = IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1));
|
||||
|
||||
#[test]
|
||||
fn solver_output_passes_verifier() {
|
||||
let mut challenge = Challenge::generate(3);
|
||||
|
|
@ -25,3 +31,104 @@ fn verifier_rejects_garbage_and_replay() {
|
|||
assert!(challenge.verify(&nonce), "first use passes");
|
||||
assert!(!challenge.verify(&nonce), "replay is rejected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verifier_rejects_oversized_nonce() {
|
||||
let mut challenge = Challenge::with_token(FIXED_TOKEN, 2);
|
||||
let big_nonce = "0".repeat(65);
|
||||
assert!(!challenge.verify(&big_nonce));
|
||||
}
|
||||
|
||||
/// Connects a server/client loopback pair; the first value is the server side.
|
||||
async fn pair() -> (TcpStream, TcpStream) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind loopback");
|
||||
let addr = listener.local_addr().expect("local address");
|
||||
let accept = tokio::spawn(async move { listener.accept().await.expect("accept").0 });
|
||||
let client = TcpStream::connect(addr).await.expect("connect loopback");
|
||||
let server = accept.await.expect("accept task");
|
||||
(server, client)
|
||||
}
|
||||
|
||||
/// Reads one newline-terminated server line byte by byte.
|
||||
async fn read_line(stream: &mut TcpStream) -> String {
|
||||
let mut line = Vec::new();
|
||||
loop {
|
||||
let mut byte = [0u8; 1];
|
||||
let n = stream.read(&mut byte).await.expect("read line");
|
||||
if n == 0 || byte[0] == b'\n' {
|
||||
break;
|
||||
}
|
||||
line.push(byte[0]);
|
||||
}
|
||||
String::from_utf8(line).expect("challenge is hex")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nonce_coalesced_with_handshake_is_preserved() {
|
||||
let (mut server, mut client) = pair().await;
|
||||
let server_task = tokio::spawn(async move {
|
||||
let passed = enforce(&mut server, PEER, 2).await.expect("enforce");
|
||||
let mut tail = [0u8; 5];
|
||||
server
|
||||
.read_exact(&mut tail)
|
||||
.await
|
||||
.expect("handshake bytes after the nonce");
|
||||
(passed, tail)
|
||||
});
|
||||
|
||||
let challenge = read_line(&mut client).await;
|
||||
let nonce = solve(&challenge, 2).expect("solved");
|
||||
// One write: nonce delimiter and the first handshake bytes in one segment.
|
||||
client
|
||||
.write_all(format!("{nonce}\nHELLO").as_bytes())
|
||||
.await
|
||||
.expect("write nonce and handshake");
|
||||
let (passed, tail) = server_task.await.expect("server task");
|
||||
assert!(passed);
|
||||
assert_eq!(&tail, b"HELLO", "bytes after the delimiter must reach the handshake");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nonce_split_across_segments_is_accepted() {
|
||||
let (mut server, mut client) = pair().await;
|
||||
let server_task = tokio::spawn(async move { enforce(&mut server, PEER, 2).await.expect("enforce") });
|
||||
|
||||
let challenge = read_line(&mut client).await;
|
||||
let nonce = solve(&challenge, 2).expect("solved");
|
||||
let split = nonce.len() / 2;
|
||||
client
|
||||
.write_all(&nonce.as_bytes()[..split])
|
||||
.await
|
||||
.expect("first segment");
|
||||
sleep(Duration::from_millis(20)).await;
|
||||
client
|
||||
.write_all(&nonce.as_bytes()[split..])
|
||||
.await
|
||||
.expect("second segment");
|
||||
client.write_all(b"\n").await.expect("delimiter");
|
||||
|
||||
assert!(server_task.await.expect("server task"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn eof_terminated_nonce_is_still_accepted() {
|
||||
let (mut server, mut client) = pair().await;
|
||||
let server_task = tokio::spawn(async move { enforce(&mut server, PEER, 2).await.expect("enforce") });
|
||||
|
||||
let challenge = read_line(&mut client).await;
|
||||
let nonce = solve(&challenge, 2).expect("solved");
|
||||
client.write_all(nonce.as_bytes()).await.expect("write nonce");
|
||||
client.shutdown().await.expect("close write half");
|
||||
|
||||
assert!(server_task.await.expect("server task"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn oversized_nonce_without_delimiter_is_rejected() {
|
||||
let (mut server, mut client) = pair().await;
|
||||
let server_task = tokio::spawn(async move { enforce(&mut server, PEER, 2).await.expect("enforce") });
|
||||
|
||||
let _challenge = read_line(&mut client).await;
|
||||
client.write_all(&[b'0'; 65]).await.expect("oversized nonce");
|
||||
assert!(!server_task.await.expect("server task"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue