v0.34.0_12

This commit is contained in:
Mateusz Gruszczyński committed 2026-10-18 10:11:16 +02:00
1 parent 943d51b528
commit 9f20d5195e
14 files changed
+198 -57

No files matched your search

+14 -7
View File
@@ -100,7 +100,7 @@ fn cloudflare_peer(peer: SocketAddr) -> bool {
/// Return CF-Connecting-IP only when the TCP peer itself is in Cloudflare's
/// published edge ranges. The header is otherwise untrusted user input.
pub fn cloudflare_client_ip(headers: &HeaderMap, peer: SocketAddr) -> Option<IpAddr> {
if !cloudflare_peer(peer) {
if !headers.contains_key("cf-connecting-ip") || !cloudflare_peer(peer) {
return None;
}
let mut values = headers.get_all("cf-connecting-ip").iter();
@@ -120,7 +120,7 @@ pub fn cloudflare_client_ip(headers: &HeaderMap, peer: SocketAddr) -> Option<IpA
/// Return a trusted ISO country code supplied by Cloudflare. `CF-IPCountry` is
/// accepted only from a Cloudflare edge and only as one two-letter code.
pub fn cloudflare_country(headers: &HeaderMap, peer: SocketAddr) -> Option<String> {
if !cloudflare_peer(peer) {
if !headers.contains_key("cf-ipcountry") || !cloudflare_peer(peer) {
return None;
}
let mut values = headers.get_all("cf-ipcountry").iter();
@@ -319,11 +319,18 @@ impl Compiled {
values,
});
}
let mut ips = chain(incoming).unwrap_or_default();
ips.push(peer_ip);
// Discard everything left of the first untrusted hop walking from us.
let first = ips.iter().rposition(|ip| !self.trusted(*ip)).unwrap_or(0);
ips.drain(..first);
let mut proxy_chain = chain(incoming).unwrap_or_default();
// No forwarded hops: borrow the actual peer from the stack instead of
// allocating a one-element Vec on every trusted-proxy request.
let direct_peer = [peer_ip];
let ips: &[IpAddr] = if proxy_chain.is_empty() {
&direct_peer
} else {
proxy_chain.push(peer_ip);
// Discard untrusted upstream prefixes without shifting the Vec.
let first = proxy_chain.iter().rposition(|ip| !self.trusted(*ip)).unwrap_or(0);
&proxy_chain[first..]
};
let client_ip = ips[0];
let mut scheme = if tls { "https" } else { "http" };
if trusted_peer