[control] Fix round 2: Caddy rereads its files on every load and the served certificate is checked, Caddy trials get a deadline outside control (caddy-guard, caddy-revert), the watch under Caddy judges upstreams as edge does, a reload of Caddy during the watch is noticed and device changes wait for the apply, stricter root key scan, IPv4-mapped trusted proxies

This commit is contained in:
2026-11-08 12:04:13 +01:00
parent ba3a060b2e
commit 23a283ece9
12 changed files with 884 additions and 159 deletions
+26 -7
View File
@@ -78,12 +78,23 @@ pub fn hostname(host_header: &str) -> String {
/// The client's address: the peer, or, when the peer is a trusted proxy, the rightmost
/// `X-Forwarded-For` entry that is not itself trusted (the same right-to-left rule as Caddy's
/// `trusted_proxies_strict`). Anything a client could forge to the left is ignored.
/// An IPv4 address carried as IPv6 (`::ffff:127.0.0.1`) is the IPv4 address.
fn canonical(ip: IpAddr) -> IpAddr {
match ip {
IpAddr::V6(v6) => v6.to_ipv4_mapped().map_or(ip, IpAddr::V4),
v4 => v4,
}
}
pub fn client_ip(session: &Session, trusted: &[IpAddr]) -> Option<IpAddr> {
let peer = session
.client_addr()
.and_then(|a| a.as_inet())
.map(|a| a.ip())?;
if !trusted.contains(&peer) {
let peer = canonical(
session
.client_addr()
.and_then(|a| a.as_inet())
.map(|a| a.ip())?,
);
let trusted_ip = |ip: &IpAddr| trusted.iter().any(|t| canonical(*t) == *ip);
if !trusted_ip(&peer) {
return Some(peer);
}
let mut last = peer;
@@ -96,8 +107,8 @@ pub fn client_ip(session: &Session, trusted: &[IpAddr]) -> Option<IpAddr> {
{
let Ok(s) = v.to_str() else { return Some(last) };
for part in s.rsplit(',') {
match part.trim().parse::<IpAddr>() {
Ok(ip) if trusted.contains(&ip) => last = ip,
match part.trim().parse::<IpAddr>().map(canonical) {
Ok(ip) if trusted_ip(&ip) => last = ip,
Ok(ip) => return Some(ip),
Err(_) => return Some(last),
}
@@ -506,6 +517,14 @@ pub fn contains(hay: &[u8], needle: &[u8]) -> bool {
mod tests {
use super::*;
#[test]
fn an_ipv4_mapped_address_is_the_ipv4_address() {
let m = IpAddr::V6(std::net::Ipv4Addr::LOCALHOST.to_ipv6_mapped());
assert_eq!(canonical(m), IpAddr::V4(std::net::Ipv4Addr::LOCALHOST));
let v6 = IpAddr::V6(std::net::Ipv6Addr::LOCALHOST);
assert_eq!(canonical(v6), v6);
}
#[test]
fn hostname_strips_port_and_case() {
assert_eq!(hostname("App.Test:8443"), "app.test");