neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
TRUSTED_PROXIES defaults to loopback and the private ranges
d2a7016 · gmorell · 2026-12-03 03:55
Message
{commit_body(@commit)}
Files changed
modified
README.md
+8
−5
@@ -230,10 +230,13 @@ left is whatever the client sent. With no `X-Forwarded-For` it falls back to
| 230 | 230 | `X-Real-IP`. An untrusted peer's headers are ignored. This is angler's |
| 231 | 231 | `TRUSTED_PROXIES` and gitgud's rule. |
| 232 | 232 | |
| 233 | −The default here is to trust nothing, unlike angler and gitgud, whose | |
| 234 | −defaults are loopback and the private ranges. Behind nginx on the same host | |
| 235 | −set `127.0.0.1/32`; behind a cluster ingress, its pod range (often | |
| 236 | −`10.0.0.0/8`). Otherwise every client looks like the proxy. | |
| 233 | +Unset, it trusts loopback and the private ranges (`127.0.0.0/8`, `::1/128`, | |
| 234 | +`10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`, `fc00::/7`), as angler and | |
| 235 | +gitgud do: right for nginx on the same host or a cluster ingress. Narrow it to | |
| 236 | +your proxy (e.g. `127.0.0.1/32`, or the ingress pod range) if other machines | |
| 237 | +on those private networks can reach the app directly, since they could | |
| 238 | +otherwise claim any address. `TRUSTED_PROXIES=none` trusts nothing (exposed | |
| 239 | +directly to the internet, no proxy). | |
| 237 | 240 | |
| 238 | 241 | ## Configuration (environment) |
| 239 | 242 |
@@ -252,7 +255,7 @@ set `127.0.0.1/32`; behind a cluster ingress, its pod range (often
| 252 | 255 | | `FLAG_HIDE_RATIO` | `0.1` | Auto-hide a post when distinct counted flaggers ÷ readers of it reaches this (0–1]; see Moderation | |
| 253 | 256 | | `FLAG_HIDE_MIN_FLAGS` | `2` | …and at least this many flaggers (one person never hides a post) | |
| 254 | 257 | | `FLAG_HIDE_ENOUGH_FLAGS` | `10` | …or this many, whatever the readership | |
| 255 | −| `TRUSTED_PROXIES` | _(none)_ | Comma-separated CIDRs of reverse proxies whose `X-Forwarded-For` / `X-Real-IP` is believed, e.g. `127.0.0.1/32` (nginx on the host), `10.0.0.0/8` (a cluster ingress). See Client addresses | | |
| 258 | +| `TRUSTED_PROXIES` | loopback + private ranges | Comma-separated CIDRs of reverse proxies whose `X-Forwarded-For` / `X-Real-IP` is believed, e.g. `127.0.0.1/32` (nginx on the host), `10.0.0.0/8` (a cluster ingress); `none` trusts nothing. See Client addresses | | |
| 256 | 259 | | `IP_RETENTION_DAYS` | `180` | How long client addresses are kept | |
| 257 | 260 | | `MAX_POST_CHARS` | `32000` | Longer posts are refused (`413 too_long`) | |
| 258 | 261 | | `LAMENESS_ENABLED` | `true` | Score posts and hold junk for review (see Moderation) | |
modified
src/client_ip.rs
+43
−3
@@ -95,13 +95,33 @@ pub fn canonical(ip: IpAddr) -> IpAddr {
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | −/// `TRUSTED_PROXIES`: comma-separated CIDRs. Empty (the default) trusts | |
| 99 | −/// nothing: forwarding headers are ignored and the peer is the client. | |
| 100 | −#[derive(Debug, Clone, Default)] | |
| 98 | +/// Trusted when `TRUSTED_PROXIES` is unset: loopback and the private ranges | |
| 99 | +/// a host nginx or cluster ingress connects from (angler's and gitgud's | |
| 100 | +/// default). | |
| 101 | +pub const DEFAULT_TRUSTED: &[&str] = | |
| 102 | + &["127.0.0.0/8", "::1/128", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "fc00::/7"]; | |
| 103 | + | |
| 104 | +/// `TRUSTED_PROXIES`: comma-separated CIDRs; unset means [`DEFAULT_TRUSTED`], | |
| 105 | +/// `none` (or empty) trusts nothing: forwarding headers are ignored and the | |
| 106 | +/// peer is the client. | |
| 107 | +#[derive(Debug, Clone)] | |
| 101 | 108 | pub struct TrustedProxies(pub Vec<Cidr>); |
| 102 | 109 | |
| 110 | +impl Default for TrustedProxies { | |
| 111 | + fn default() -> Self { | |
| 112 | + Self(DEFAULT_TRUSTED.iter().map(|c| Cidr::from_str(c).expect("default CIDR")).collect()) | |
| 113 | + } | |
| 114 | +} | |
| 115 | + | |
| 103 | 116 | impl TrustedProxies { |
| 117 | + pub fn none() -> Self { | |
| 118 | + Self(Vec::new()) | |
| 119 | + } | |
| 120 | + | |
| 104 | 121 | pub fn parse(s: &str) -> Result<Self, String> { |
| 122 | + if s.trim().eq_ignore_ascii_case("none") { | |
| 123 | + return Ok(Self::none()); | |
| 124 | + } | |
| 105 | 125 | s.split(',').map(str::trim).filter(|p| !p.is_empty()).map(Cidr::from_str).collect::<Result<_, _>>().map(Self) |
| 106 | 126 | } |
| 107 | 127 |
@@ -192,6 +212,26 @@ mod tests {
| 192 | 212 | use super::*; |
| 193 | 213 | use axum::http::HeaderValue; |
| 194 | 214 | |
| 215 | + #[test] | |
| 216 | + fn defaults_trust_loopback_and_private_ranges_and_none_trusts_nothing() { | |
| 217 | + let d = TrustedProxies::default(); | |
| 218 | + for ip in ["127.0.0.1", "::1", "10.1.2.3", "172.31.0.9", "192.168.1.1", "fd00::5", "::ffff:10.0.0.1"] { | |
| 219 | + assert!(d.trusts(canonical(ip.parse().unwrap())), "{ip}"); | |
| 220 | + } | |
| 221 | + for ip in ["8.8.8.8", "172.32.0.1", "100.64.0.1", "2001:db8::1"] { | |
| 222 | + assert!(!d.trusts(ip.parse().unwrap()), "{ip}"); | |
| 223 | + } | |
| 224 | + // Behind the default, a private-range proxy's X-Forwarded-For counts… | |
| 225 | + let hdr = h(&[("x-forwarded-for", "6.6.6.6, 203.0.113.9")]); | |
| 226 | + assert_eq!(d.resolve(Some("10.0.0.2".parse().unwrap()), &hdr), Some("203.0.113.9".parse().unwrap())); | |
| 227 | + // …a public client's doesn't. | |
| 228 | + assert_eq!(d.resolve(Some("198.51.100.4".parse().unwrap()), &hdr), Some("198.51.100.4".parse().unwrap())); | |
| 229 | + let none = TrustedProxies::parse("none").unwrap(); | |
| 230 | + assert!(none.0.is_empty()); | |
| 231 | + assert_eq!(none.resolve(Some("10.0.0.2".parse().unwrap()), &hdr), Some("10.0.0.2".parse().unwrap())); | |
| 232 | + assert!(TrustedProxies::parse("").unwrap().0.is_empty()); | |
| 233 | + } | |
| 234 | + | |
| 195 | 235 | #[test] |
| 196 | 236 | fn the_resolved_header_cannot_be_forged() { |
| 197 | 237 | let mut m = HeaderMap::new(); |
modified
src/config.rs
+2
−1
@@ -48,7 +48,8 @@ pub struct Config {
| 48 | 48 | /// Trust level needed to flag a post (`MIN_TRUST_TO_FLAG`, default 1). |
| 49 | 49 | pub min_trust_to_flag: i32, |
| 50 | 50 | /// Reverse proxies whose `X-Forwarded-For` is believed |
| 51 | − /// (`TRUSTED_PROXIES`, comma-separated CIDRs; default none). | |
| 51 | + /// (`TRUSTED_PROXIES`, comma-separated CIDRs or `none`; default loopback | |
| 52 | + /// and the private ranges, [`crate::client_ip::DEFAULT_TRUSTED`]). | |
| 52 | 53 | pub trusted_proxies: crate::client_ip::TrustedProxies, |
| 53 | 54 | /// How long client addresses are kept (`IP_RETENTION_DAYS`, default 180). |
| 54 | 55 | pub ip_retention_days: i64, |
Parents: e07fdc3