[edge] Listeners: a request header must be complete within 60 s (--header-timeout), pipelined requests are answered in order, and --proxy-socket serves the front proxy on a 0660 Unix socket whose X-Forwarded-For is believed

This commit is contained in:
2026-11-10 20:46:28 +01:00
parent 7a2aee20eb
commit 39dd8f0f47
6 changed files with 82 additions and 22 deletions
+20 -15
View File
@@ -75,9 +75,6 @@ pub fn hostname(host_header: &str) -> String {
name.to_ascii_lowercase()
}
/// 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 {
@@ -86,16 +83,21 @@ fn canonical(ip: IpAddr) -> IpAddr {
}
}
/// 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.
///
/// A request on the proxy socket (`--proxy-socket`) comes from the front proxy by
/// construction: only the socket's owner and group can connect to it, so its peer is trusted
/// and its `X-Forwarded-For` names the client. With no usable entry there is no address.
pub fn client_ip(session: &Session, trusted: &[IpAddr]) -> Option<IpAddr> {
let peer = canonical(
session
.client_addr()
.and_then(|a| a.as_inet())
.map(|a| a.ip())?,
);
let addr = session.client_addr()?;
let peer = addr.as_inet().map(|a| canonical(a.ip()));
let trusted_ip = |ip: &IpAddr| trusted.iter().any(|t| canonical(*t) == *ip);
if !trusted_ip(&peer) {
return Some(peer);
if let Some(p) = peer
&& !trusted_ip(&p)
{
return Some(p);
}
let mut last = peer;
for v in session
@@ -105,16 +107,16 @@ pub fn client_ip(session: &Session, trusted: &[IpAddr]) -> Option<IpAddr> {
.iter()
.rev()
{
let Ok(s) = v.to_str() else { return Some(last) };
let Ok(s) = v.to_str() else { return last };
for part in s.rsplit(',') {
match part.trim().parse::<IpAddr>().map(canonical) {
Ok(ip) if trusted_ip(&ip) => last = ip,
Ok(ip) if trusted_ip(&ip) => last = Some(ip),
Ok(ip) => return Some(ip),
Err(_) => return Some(last),
Err(_) => return last,
}
}
}
Some(last)
last
}
/// A response with an empty body, like Caddy's `static_response` and error responses.
@@ -165,6 +167,9 @@ impl ProxyHttp for EdgeProxy {
async fn early_request_filter(&self, session: &mut Session, _ctx: &mut Ctx) -> Result<()> {
// The header is in: the header deadline (slowloris) no longer applies.
crate::deadline::header_read();
// Pipelined requests are answered in order, as Caddy does (RFC 9112 §9.3.2); without
// this Pingora answers the first and closes the connection.
session.downstream_session.set_pipelining_enabled(true);
Ok(())
}