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
|
|
@ -2,19 +2,85 @@
|
|||
//! parsing of headers injected by trusted ingress peers.
|
||||
|
||||
use crate::config::ProxyProtocolConfig;
|
||||
use std::net::SocketAddr;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
mod inbound;
|
||||
pub(super) use inbound::read_v2_header;
|
||||
|
||||
/// Reason a PROXY header client address is rejected as an identity.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ClientIpRejection {
|
||||
/// `0.0.0.0` or `::`: no host behind the header.
|
||||
Unspecified,
|
||||
/// The header claims a loopback address, which is never a real client.
|
||||
Loopback,
|
||||
/// The header claims a link-local address (`169.254.0.0/16`, `fe80::/10`).
|
||||
LinkLocal,
|
||||
/// The header claims a multicast address.
|
||||
Multicast,
|
||||
/// The header claims the limited IPv4 broadcast address.
|
||||
Broadcast,
|
||||
/// The header claims an address of the proxy network itself.
|
||||
TrustedProxy,
|
||||
}
|
||||
|
||||
/// Validates the client IP claimed in an inbound PROXY v2 header.
|
||||
///
|
||||
/// A trusted proxy is allowed to state who the client is, but it must not be
|
||||
/// able to name an address that can never be a real remote client (host-local,
|
||||
/// link-local, multicast, broadcast) or another trusted proxy. Without this
|
||||
/// check a single compromised or misconfigured proxy could ban arbitrary
|
||||
/// addresses cluster-wide or poison their reputation.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns the reason `ip` is not usable as a client identity.
|
||||
pub fn validate_client_ip(config: &ProxyProtocolConfig, ip: IpAddr) -> Result<(), ClientIpRejection> {
|
||||
// Collapse IPv4-mapped IPv6 first: the ban tables do the same, so leaving
|
||||
// `::ffff:127.0.0.1` as v6 would bypass the loopback and trusted-network
|
||||
// checks and then be normalized into a ban for `127.0.0.1`.
|
||||
let ip = match ip {
|
||||
IpAddr::V6(v6) => v6.to_ipv4_mapped().map_or(ip, IpAddr::V4),
|
||||
IpAddr::V4(_) => ip,
|
||||
};
|
||||
if ip.is_unspecified() {
|
||||
return Err(ClientIpRejection::Unspecified);
|
||||
}
|
||||
if ip.is_loopback() {
|
||||
return Err(ClientIpRejection::Loopback);
|
||||
}
|
||||
let link_local = match ip {
|
||||
IpAddr::V4(v4) => v4.is_link_local(),
|
||||
IpAddr::V6(v6) => v6.is_unicast_link_local(),
|
||||
};
|
||||
if link_local {
|
||||
return Err(ClientIpRejection::LinkLocal);
|
||||
}
|
||||
if ip.is_multicast() {
|
||||
return Err(ClientIpRejection::Multicast);
|
||||
}
|
||||
if ip == IpAddr::V4(Ipv4Addr::BROADCAST) {
|
||||
return Err(ClientIpRejection::Broadcast);
|
||||
}
|
||||
if config.trusted_cidrs.iter().any(|cidr| cidr.contains(ip)) {
|
||||
return Err(ClientIpRejection::TrustedProxy);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Resolves the effective client address from an inbound PROXY v2 header.
|
||||
///
|
||||
/// Only peers inside `config.trusted_cidrs` may inject a header; anyone else
|
||||
/// is rejected instead of being believed. The whole header must arrive within
|
||||
/// `config.ingress_timeout_ms` and fit in `config.max_header_bytes`.
|
||||
///
|
||||
/// A header whose client address fails [`validate_client_ip`] is not fatal:
|
||||
/// the transport peer becomes the client identity and the reason is logged once
|
||||
/// per connection at debug level. Failing closed would drop every connection
|
||||
/// behind a misbehaving proxy, while trusting the claimed address would hand
|
||||
/// the ban path to the proxy.
|
||||
///
|
||||
/// # Errors
|
||||
/// Fails when the peer is not trusted, on timeout and on any header parse error.
|
||||
pub(super) async fn resolve_client_addr(
|
||||
|
|
@ -25,12 +91,24 @@ pub(super) async fn resolve_client_addr(
|
|||
if !config.trusted_cidrs.iter().any(|cidr| cidr.contains(peer_addr.ip())) {
|
||||
anyhow::bail!("untrusted peer attempted inbound PROXY protocol");
|
||||
}
|
||||
read_v2_header(
|
||||
let claimed = read_v2_header(
|
||||
client,
|
||||
Duration::from_millis(config.ingress_timeout_ms),
|
||||
config.max_header_bytes,
|
||||
)
|
||||
.await
|
||||
.await?;
|
||||
match validate_client_ip(config, claimed.ip()) {
|
||||
Ok(()) => Ok(claimed),
|
||||
Err(reason) => {
|
||||
tracing::debug!(
|
||||
peer = %peer_addr,
|
||||
claimed = %claimed,
|
||||
reason = ?reason,
|
||||
"ignoring PROXY header client address, using the transport peer"
|
||||
);
|
||||
Ok(peer_addr)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Encodes a PROXY protocol v2 TCP header for the backend.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue