fix(edge): IPv6 bind, configurable relay idle, per-gateway budget, PROXY identity
- bind.address is parsed as IpAddr (no ":::port"), the socket domain follows the family, and "::" binds dual-stack. IPv6 was impossible before. - limits.relay_idle_timeout_secs (default 300, 1..=86400) replaces the hardcoded relay constant; the deploy examples document it. - The connection budget is an Arc<Semaphore> owned by Gateway instead of a process-wide OnceLock, so tests and multiple gateways stay independent. - PROXY header identity is validated: unspecified, loopback, link-local, multicast, broadcast and addresses inside trusted_cidrs are rejected, and IPv4-mapped IPv6 is normalised first. An invalid header falls back to the transport peer. Bans targeting trusted_cidrs are suppressed, closing the ban-amplification path through a trusted load balancer. - TrustedProxyCidr::contains no longer overflows its mask for IPv6 prefixes shorter than /96.
This commit is contained in:
parent
9baae25acb
commit
324ef12b27
15 changed files with 530 additions and 76 deletions
|
|
@ -238,6 +238,30 @@ fn rejects_rate_limit_burst_below_one() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relay_idle_timeout_defaults_to_300_and_accepts_the_full_range() {
|
||||
let config = Config::parse_str("").expect("empty config must parse");
|
||||
assert_eq!(config.limits.relay_idle_timeout_secs, 300);
|
||||
|
||||
for secs in [1u64, 60, 300, 86_400] {
|
||||
let toml = format!("[limits]\nrelay_idle_timeout_secs = {secs}\n");
|
||||
let config = Config::parse_str(&toml).expect("in-range idle timeout must parse");
|
||||
assert_eq!(config.limits.relay_idle_timeout_secs, secs);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_out_of_range_relay_idle_timeout() {
|
||||
for secs in [0u64, 86_401] {
|
||||
let toml = format!("[limits]\nrelay_idle_timeout_secs = {secs}\n");
|
||||
let err = Config::parse_str(&toml).expect_err("idle timeout outside 1..=86400 must be rejected");
|
||||
assert!(
|
||||
err.to_string().contains("limits.relay_idle_timeout_secs"),
|
||||
"idle timeout '{secs}' 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");
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
use rampart::config::Config;
|
||||
use rampart::engine::challenge::DifficultyAdjuster;
|
||||
use rampart::engine::listener::AcceptBackoff;
|
||||
use rampart::engine::listener::{AcceptBackoff, build_listener, parse_listen_addr};
|
||||
use rampart::engine::tunnel::Gateway;
|
||||
use rampart::filter::blacklist::Blacklist;
|
||||
use rampart::filter::rate_limit::RateLimiter;
|
||||
|
|
@ -11,7 +11,7 @@ use rampart::protocol::ProtocolRegistry;
|
|||
use rampart::traffic::reputation::IpReputation;
|
||||
use std::collections::HashSet;
|
||||
use std::io;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
|
|
@ -263,6 +263,45 @@ async fn accept_backoff_sleeps_instead_of_spinning() {
|
|||
);
|
||||
}
|
||||
|
||||
/// The listen address is parsed as an IP literal and the socket domain follows
|
||||
/// the address family, so IPv6 loopback binds instead of forcing IPv4.
|
||||
#[tokio::test]
|
||||
async fn binds_ipv4_and_ipv6_loopback() {
|
||||
let v4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0);
|
||||
let listener = build_listener(v4).expect("bind IPv4 loopback");
|
||||
assert!(listener.local_addr().expect("local addr").is_ipv4());
|
||||
|
||||
let v6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 0);
|
||||
let listener = build_listener(v6).expect("bind IPv6 loopback");
|
||||
assert!(listener.local_addr().expect("local addr").is_ipv6());
|
||||
}
|
||||
|
||||
/// `::` must parse as a wildcard IPv6 listen address instead of being
|
||||
/// concatenated into the invalid `":::port"` form.
|
||||
#[test]
|
||||
fn parses_ipv6_wildcard_listen_address() {
|
||||
let addr = parse_listen_addr("::", 25565).expect("IPv6 wildcard must parse");
|
||||
assert_eq!(addr, SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 25565));
|
||||
assert!(addr.is_ipv6());
|
||||
}
|
||||
|
||||
/// The relay idle budget is configurable through `limits` instead of being a
|
||||
/// hardcoded constant.
|
||||
#[test]
|
||||
fn relay_idle_timeout_comes_from_limits_config() {
|
||||
let mut config = Config::default();
|
||||
config.limits.relay_idle_timeout_secs = 1;
|
||||
let gateway = test_gateway(config, HashSet::new());
|
||||
assert_eq!(gateway.relay_idle_timeout(), Duration::from_secs(1));
|
||||
|
||||
let default_gateway = test_gateway(Config::default(), HashSet::new());
|
||||
assert_eq!(
|
||||
default_gateway.relay_idle_timeout(),
|
||||
Duration::from_secs(300),
|
||||
"the default idle budget is 300s"
|
||||
);
|
||||
}
|
||||
|
||||
/// A silent pair is closed by the relay idle timeout instead of pinning two
|
||||
/// descriptors forever.
|
||||
#[tokio::test]
|
||||
|
|
|
|||
239
tests/proxy_identity.rs
Normal file
239
tests/proxy_identity.rs
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
//! Client identity behind a trusted PROXY protocol proxy: header address
|
||||
//! validation, fallback to the transport peer and suppression of bans for
|
||||
//! addresses inside `proxy_protocol.trusted_cidrs`.
|
||||
|
||||
#![cfg(feature = "protocol-minecraft")]
|
||||
|
||||
use rampart::config::{Config, ProxyProtocolConfig, TrustedProxyCidr};
|
||||
use rampart::engine::challenge::DifficultyAdjuster;
|
||||
use rampart::engine::tunnel::{ClientIpRejection, Gateway, GatewayOutcome, validate_client_ip};
|
||||
use rampart::filter::blacklist::Blacklist;
|
||||
use rampart::filter::rate_limit::RateLimiter;
|
||||
use rampart::protocol::ProtocolRegistry;
|
||||
use rampart::traffic::reputation::IpReputation;
|
||||
use std::collections::HashSet;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
/// Builds a gateway with no XDP/ClickHouse writers and an empty protocol
|
||||
/// registry, so every connection stops at the admission filters.
|
||||
fn test_gateway(config: Config, whitelist: HashSet<IpAddr>) -> Arc<Gateway> {
|
||||
Arc::new(Gateway::new(
|
||||
Arc::new(config.clone()),
|
||||
Arc::new(RateLimiter::new(
|
||||
config.limits.rate_limit_pps,
|
||||
config.limits.rate_limit_burst,
|
||||
)),
|
||||
Arc::new(Blacklist::new()),
|
||||
Arc::new(Mutex::new(DifficultyAdjuster::default())),
|
||||
Arc::new(whitelist),
|
||||
Arc::new(IpReputation::new()),
|
||||
None,
|
||||
None,
|
||||
Arc::new(AtomicU64::new(0)),
|
||||
Arc::new(ProtocolRegistry::new()),
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
/// Encodes a PROXY protocol v2 TCP-over-IPv4 header.
|
||||
fn proxy_v2_header(source: Ipv4Addr, destination: Ipv4Addr) -> Vec<u8> {
|
||||
const SIGNATURE: [u8; 12] = *b"\r\n\r\n\0\r\nQUIT\n";
|
||||
let mut header = Vec::with_capacity(28);
|
||||
header.extend_from_slice(&SIGNATURE);
|
||||
header.push(0x21);
|
||||
header.push(0x11);
|
||||
header.extend_from_slice(&12u16.to_be_bytes());
|
||||
header.extend_from_slice(&source.octets());
|
||||
header.extend_from_slice(&destination.octets());
|
||||
header.extend_from_slice(&40_000u16.to_be_bytes());
|
||||
header.extend_from_slice(&25565u16.to_be_bytes());
|
||||
header
|
||||
}
|
||||
|
||||
/// Runs one connection through the gateway after sending a PROXY v2 header that
|
||||
/// claims `claimed` as the client address. The transport peer passed to the
|
||||
/// gateway is `peer`, which lets the test model a trusted proxy explicitly.
|
||||
async fn handle_with_header(gateway: &Gateway, peer: SocketAddr, claimed: Ipv4Addr) -> GatewayOutcome {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind loopback");
|
||||
let local = listener.local_addr().expect("local addr");
|
||||
let (client, accepted) = tokio::join!(TcpStream::connect(local), listener.accept());
|
||||
let mut client = client.expect("connect");
|
||||
let server = accepted.expect("accept").0;
|
||||
let destination = match local.ip() {
|
||||
IpAddr::V4(ip) => ip,
|
||||
IpAddr::V6(_) => Ipv4Addr::LOCALHOST,
|
||||
};
|
||||
client
|
||||
.write_all(&proxy_v2_header(claimed, destination))
|
||||
.await
|
||||
.expect("write PROXY header");
|
||||
let outcome = gateway.handle(server, peer).await;
|
||||
drop(client);
|
||||
outcome
|
||||
}
|
||||
|
||||
/// Runs one connection without a PROXY header.
|
||||
async fn handle_plain(gateway: &Gateway, peer: SocketAddr) -> GatewayOutcome {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind loopback");
|
||||
let local = listener.local_addr().expect("local addr");
|
||||
let (_client, server) = tokio::join!(TcpStream::connect(local), listener.accept());
|
||||
gateway.handle(server.expect("accept").0, peer).await
|
||||
}
|
||||
|
||||
/// Parses a trusted proxy network for the test config.
|
||||
fn cidr(value: &str) -> TrustedProxyCidr {
|
||||
value.parse().expect("valid trusted proxy CIDR")
|
||||
}
|
||||
|
||||
/// A trusted proxy may state a public client address, and that address is the
|
||||
/// one used for admission decisions and bans.
|
||||
#[tokio::test]
|
||||
async fn trusted_proxy_header_ip_is_used_for_identity_and_bans() {
|
||||
let mut config = Config::default();
|
||||
config.limits.rate_limit_pps = 0.0;
|
||||
config.limits.rate_limit_burst = 0.0;
|
||||
config.proxy_protocol.ingress_enabled = true;
|
||||
config.proxy_protocol.trusted_cidrs = vec![cidr("127.0.0.1/32")];
|
||||
let gateway = test_gateway(config, HashSet::new());
|
||||
|
||||
let claimed = Ipv4Addr::new(198, 51, 100, 7);
|
||||
let peer = SocketAddr::from((Ipv4Addr::LOCALHOST, 51_100));
|
||||
for _ in 0..5 {
|
||||
let outcome = handle_with_header(&gateway, peer, claimed).await;
|
||||
assert!(outcome.result.is_ok(), "a rate-limited connection is dropped silently");
|
||||
assert_eq!(
|
||||
outcome.client_ip,
|
||||
Some(IpAddr::V4(claimed)),
|
||||
"the header IP, not the proxy IP, is the client identity"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
gateway.blacklist.is_blocked(IpAddr::V4(claimed)),
|
||||
"repeated rate-limit hits must ban the header IP"
|
||||
);
|
||||
assert!(
|
||||
!gateway.blacklist.is_blocked(peer.ip()),
|
||||
"the trusted proxy itself must stay unbanned"
|
||||
);
|
||||
}
|
||||
|
||||
/// Header addresses that cannot be a real client are rejected and the
|
||||
/// transport peer becomes the identity.
|
||||
#[test]
|
||||
fn rejects_header_addresses_that_cannot_be_a_client() {
|
||||
let config = ProxyProtocolConfig {
|
||||
trusted_cidrs: vec![cidr("10.0.0.0/8")],
|
||||
..ProxyProtocolConfig::default()
|
||||
};
|
||||
let cases = [
|
||||
("0.0.0.0", ClientIpRejection::Unspecified),
|
||||
("::", ClientIpRejection::Unspecified),
|
||||
("127.0.0.1", ClientIpRejection::Loopback),
|
||||
("::1", ClientIpRejection::Loopback),
|
||||
("169.254.10.1", ClientIpRejection::LinkLocal),
|
||||
("fe80::1", ClientIpRejection::LinkLocal),
|
||||
("224.0.0.1", ClientIpRejection::Multicast),
|
||||
("ff02::1", ClientIpRejection::Multicast),
|
||||
("255.255.255.255", ClientIpRejection::Broadcast),
|
||||
("::ffff:127.0.0.1", ClientIpRejection::Loopback),
|
||||
("::ffff:169.254.10.1", ClientIpRejection::LinkLocal),
|
||||
];
|
||||
for (address, expected) in cases {
|
||||
let ip = address.parse().expect("valid test address");
|
||||
assert_eq!(validate_client_ip(&config, ip), Err(expected), "address {address}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A proxy must not be able to claim an address of the proxy network itself.
|
||||
#[test]
|
||||
fn rejects_header_address_inside_trusted_cidrs() {
|
||||
let config = ProxyProtocolConfig {
|
||||
trusted_cidrs: vec![cidr("198.51.100.0/24"), cidr("2001:db8:2::/48")],
|
||||
..ProxyProtocolConfig::default()
|
||||
};
|
||||
assert_eq!(
|
||||
validate_client_ip(&config, "198.51.100.7".parse().expect("ipv4")),
|
||||
Err(ClientIpRejection::TrustedProxy)
|
||||
);
|
||||
assert_eq!(
|
||||
validate_client_ip(&config, "2001:db8:2::7".parse().expect("ipv6")),
|
||||
Err(ClientIpRejection::TrustedProxy)
|
||||
);
|
||||
assert_eq!(
|
||||
validate_client_ip(&config, "::ffff:198.51.100.7".parse().expect("mapped ipv4")),
|
||||
Err(ClientIpRejection::TrustedProxy),
|
||||
"an IPv4-mapped address must match the IPv4 proxy network"
|
||||
);
|
||||
assert!(
|
||||
validate_client_ip(&config, "203.0.113.7".parse().expect("ipv4")).is_ok(),
|
||||
"an address outside the proxy networks is a valid client identity"
|
||||
);
|
||||
}
|
||||
|
||||
/// An invalid header address falls back to the transport peer, and the peer
|
||||
/// (itself a trusted proxy) is never banned.
|
||||
#[tokio::test]
|
||||
async fn invalid_header_ip_falls_back_to_peer_and_suppresses_bans() {
|
||||
let mut config = Config::default();
|
||||
config.limits.rate_limit_pps = 0.0;
|
||||
config.limits.rate_limit_burst = 0.0;
|
||||
config.proxy_protocol.ingress_enabled = true;
|
||||
config.proxy_protocol.trusted_cidrs = vec![cidr("127.0.0.1/32")];
|
||||
let gateway = test_gateway(config, HashSet::new());
|
||||
|
||||
let peer = SocketAddr::from((Ipv4Addr::LOCALHOST, 51_101));
|
||||
for _ in 0..8 {
|
||||
let outcome = handle_with_header(&gateway, peer, Ipv4Addr::LOCALHOST).await;
|
||||
assert!(outcome.result.is_ok());
|
||||
assert_eq!(
|
||||
outcome.client_ip,
|
||||
Some(peer.ip()),
|
||||
"a rejected header IP must fall back to the transport peer"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
gateway.blacklist.is_empty(),
|
||||
"a trusted proxy address must never be banned"
|
||||
);
|
||||
}
|
||||
|
||||
/// A header claiming an address inside `trusted_cidrs` is rejected, and the
|
||||
/// fallback peer (also a trusted proxy) is not banned either.
|
||||
#[tokio::test]
|
||||
async fn header_inside_trusted_cidrs_is_rejected_and_never_banned() {
|
||||
let mut config = Config::default();
|
||||
config.limits.rate_limit_pps = 0.0;
|
||||
config.limits.rate_limit_burst = 0.0;
|
||||
config.proxy_protocol.ingress_enabled = true;
|
||||
config.proxy_protocol.trusted_cidrs = vec![cidr("127.0.0.1/32"), cidr("198.51.100.0/24")];
|
||||
let gateway = test_gateway(config, HashSet::new());
|
||||
|
||||
let claimed = Ipv4Addr::new(198, 51, 100, 7);
|
||||
let peer = SocketAddr::from((Ipv4Addr::LOCALHOST, 51_102));
|
||||
for _ in 0..5 {
|
||||
let outcome = handle_with_header(&gateway, peer, claimed).await;
|
||||
assert_eq!(
|
||||
outcome.client_ip,
|
||||
Some(peer.ip()),
|
||||
"a proxy must not claim another proxy address as the client"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
gateway.blacklist.is_empty(),
|
||||
"neither the claimed proxy address nor the fallback peer may be banned"
|
||||
);
|
||||
}
|
||||
|
||||
/// Plain traffic without PROXY protocol keeps using the transport peer.
|
||||
#[tokio::test]
|
||||
async fn plain_connection_uses_the_transport_peer() {
|
||||
let gateway = test_gateway(Config::default(), HashSet::new());
|
||||
let peer = SocketAddr::from(([198, 51, 100, 20], 51_103));
|
||||
let outcome = handle_plain(&gateway, peer).await;
|
||||
assert_eq!(outcome.client_ip, Some(peer.ip()));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue