0.34.6_repo_cleanup

This commit is contained in:
Mateusz Gruszczyński committed 2026-10-22 09:31:15 +02:00
1 parent ce1a90397c
commit 133d2f0f91
273 files changed
+5969 -6926

No files matched your search

-31
View File
@@ -1,5 +1,3 @@
//! Single trust boundary for TCP, TLS and QUIC. Incoming proxy headers are never
//! authority for routing, listener selection or identity without a trusted peer.
use anyhow::{ensure, Context as _, Result};
use http::{HeaderMap, HeaderValue, Uri};
use ipnet::IpNet;
@@ -13,7 +11,6 @@ use std::{
#[serde(default, deny_unknown_fields)]
pub struct Settings {
pub trusted_proxies: Vec<String>,
/// Enable only when the trusted last hop sanitizes X-Forwarded-Proto.
pub trust_forwarded_proto: bool,
}
#[derive(Debug)]
@@ -97,8 +94,6 @@ fn cloudflare_peer(peer: SocketAddr) -> bool {
.any(|network| network.contains(&peer))
}
/// 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) {
return None;
@@ -117,8 +112,6 @@ pub fn cloudflare_client_ip(headers: &HeaderMap, peer: SocketAddr) -> Option<IpA
.map(canonical)
}
/// 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) {
return None;
@@ -202,9 +195,6 @@ impl Compiled {
pub fn trusted(&self, ip: IpAddr) -> bool {
self.networks.iter().any(|net| net.contains(&canonical(ip)))
}
/// Whether this peer may define the externally visible request scheme.
/// Keeping this separate from `trusted` preserves the explicit
/// `trust_forwarded_proto` opt-in used by the public proxy path.
pub fn trusted_forwarded_proto(&self, ip: IpAddr) -> bool {
self.trust_proto && self.trusted(ip)
}
@@ -220,9 +210,6 @@ impl Compiled {
self.context_with_original_url(incoming, peer, local, tls, authority, uri, true)
}
/// The absolute original URL is only consumed by the optional Tinyauth
/// verifier. Ordinary proxy requests need the exact forwarding metadata,
/// but do not need to allocate this extra diagnostic URL.
pub fn context_with_original_url(
&self,
incoming: &HeaderMap,
@@ -239,9 +226,6 @@ impl Compiled {
)
}
/// Only the host/path validation and formatting data from the same request
/// and accepted connection are reused. Dynamic trust and XFF are evaluated
/// on every request, including after an in-place configuration reload.
#[allow(clippy::too_many_arguments)]
pub(crate) fn context_with_peer(
&self,
@@ -268,9 +252,6 @@ impl Compiled {
} else {
String::new()
};
// Repeated HTTP/1.1 requests generally share Host and TLS state.
// Cache only the untrusted-peer metadata for this accepted socket;
// X-Forwarded-Uri, original URL and all trust decisions remain per-request.
let target_header: HeaderValue = target.parse()?;
let mut values = if let Some(values) =
cached.and_then(|peer| peer.cached_untrusted(authority, tls))
@@ -321,7 +302,6 @@ impl Compiled {
}
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 client_ip = ips[0];
@@ -340,7 +320,6 @@ impl Compiled {
};
}
let authority_text = authority.to_str()?;
// Validate before inserting authority into Forwarded's quoted host field.
if !validated_host { crate::config::request_host(authority_text)?; }
let original_target = uri.path_and_query().map(|v| v.as_str()).unwrap_or("/");
let original_url = if include_original_url {
@@ -400,8 +379,6 @@ impl Compiled {
}
}
impl Context {
/// Replace the effective client with a previously authenticated edge header.
/// Existing proxy-chain metadata is retained behind the visitor address.
pub fn override_client_ip(&mut self, client: IpAddr) -> Result<()> {
let client = canonical(client);
let client_text = client.to_string();
@@ -442,10 +419,6 @@ impl Context {
self.apply_clean(headers, true);
}
fn apply_clean(&self, headers: &mut HeaderMap, include_client_ip: bool) {
// HeaderMap::insert replaces all values for a known canonical name.
// Remove unknown/spoofed forwarding names while retaining the valid ones.
// The normal request has no extra spoofed header names. Allocate
// a removal list only for requests which actually need sanitization.
let mut remove = None;
for name in headers.keys() {
let text = name.as_str();
@@ -472,10 +445,6 @@ impl Context {
}
}
/// Apply trusted forwarding metadata to an application request. When client-IP
/// forwarding is disabled, preserve scheme/host/port/URI metadata but withhold
/// the visitor address so frameworks such as ProxyFix see the RustProxy socket peer.
/// Authentication verifiers use `apply` directly and still receive the real client IP.
pub fn apply_upstream(&self, headers: &mut HeaderMap, forward_client_ip: bool) {
self.apply_clean(headers, forward_client_ip);
}