chore: rustfmt the workspace and make clippy -D warnings pass
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,7 +7,10 @@
|
||||
//! their /64. State is in memory and per hub process: it resets on restart and
|
||||
//! is not shared between replicas.
|
||||
use axum::http::HeaderMap;
|
||||
use governor::{clock::DefaultClock, state::keyed::DefaultKeyedStateStore, state::InMemoryState, state::NotKeyed, Quota, RateLimiter};
|
||||
use governor::{
|
||||
clock::DefaultClock, state::keyed::DefaultKeyedStateStore, state::InMemoryState,
|
||||
state::NotKeyed, Quota, RateLimiter,
|
||||
};
|
||||
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
||||
use std::num::NonZeroU32;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
@@ -82,7 +85,11 @@ impl Limits {
|
||||
let mut proxies = Vec::new();
|
||||
if let Ok(list) = std::env::var("TESSERA_TRUSTED_PROXIES") {
|
||||
for item in list.split(',').map(str::trim).filter(|s| !s.is_empty()) {
|
||||
proxies.push(item.parse::<IpAddr>().map_err(|_| anyhow::anyhow!("TESSERA_TRUSTED_PROXIES: invalid IP {item}"))?);
|
||||
proxies.push(
|
||||
item.parse::<IpAddr>().map_err(|_| {
|
||||
anyhow::anyhow!("TESSERA_TRUSTED_PROXIES: invalid IP {item}")
|
||||
})?,
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(Self::new(LimitConfig::default(), proxies))
|
||||
@@ -91,7 +98,9 @@ impl Limits {
|
||||
/// The rate-limit key for a request. `None` peer (should not happen) maps to
|
||||
/// one strict shared bucket instead of bypassing limits.
|
||||
pub fn source(&self, peer: Option<SocketAddr>, headers: &HeaderMap) -> IpAddr {
|
||||
let Some(peer) = peer else { return IpAddr::V6(Ipv6Addr::UNSPECIFIED) };
|
||||
let Some(peer) = peer else {
|
||||
return IpAddr::V6(Ipv6Addr::UNSPECIFIED);
|
||||
};
|
||||
let mut client = peer.ip();
|
||||
if self.trusted_proxies.contains(&client) {
|
||||
let forwarded: Vec<&str> = headers
|
||||
@@ -182,7 +191,10 @@ mod tests {
|
||||
}
|
||||
|
||||
fn limits(proxies: &[&str]) -> Limits {
|
||||
Limits::new(LimitConfig::default(), proxies.iter().map(|p| p.parse().unwrap()).collect())
|
||||
Limits::new(
|
||||
LimitConfig::default(),
|
||||
proxies.iter().map(|p| p.parse().unwrap()).collect(),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -202,14 +214,30 @@ mod tests {
|
||||
fn forwarded_for_is_ignored_unless_the_peer_is_a_trusted_proxy() {
|
||||
let l = limits(&["10.0.0.1"]);
|
||||
let spoof = headers("198.51.100.9");
|
||||
assert_eq!(l.source(peer("203.0.113.7"), &spoof), "203.0.113.7".parse::<IpAddr>().unwrap(), "untrusted peer cannot choose its key");
|
||||
assert_eq!(l.source(peer("10.0.0.1"), &spoof), "198.51.100.9".parse::<IpAddr>().unwrap());
|
||||
assert_eq!(
|
||||
l.source(peer("203.0.113.7"), &spoof),
|
||||
"203.0.113.7".parse::<IpAddr>().unwrap(),
|
||||
"untrusted peer cannot choose its key"
|
||||
);
|
||||
assert_eq!(
|
||||
l.source(peer("10.0.0.1"), &spoof),
|
||||
"198.51.100.9".parse::<IpAddr>().unwrap()
|
||||
);
|
||||
// client-supplied prefix is ignored: walk right to left, skipping trusted hops
|
||||
let chain = headers("1.1.1.1, 198.51.100.9, 10.0.0.1");
|
||||
assert_eq!(l.source(peer("10.0.0.1"), &chain), "198.51.100.9".parse::<IpAddr>().unwrap());
|
||||
assert_eq!(
|
||||
l.source(peer("10.0.0.1"), &chain),
|
||||
"198.51.100.9".parse::<IpAddr>().unwrap()
|
||||
);
|
||||
// junk or missing header falls back to the proxy itself
|
||||
assert_eq!(l.source(peer("10.0.0.1"), &headers("not-an-ip")), "10.0.0.1".parse::<IpAddr>().unwrap());
|
||||
assert_eq!(l.source(peer("10.0.0.1"), &HeaderMap::new()), "10.0.0.1".parse::<IpAddr>().unwrap());
|
||||
assert_eq!(
|
||||
l.source(peer("10.0.0.1"), &headers("not-an-ip")),
|
||||
"10.0.0.1".parse::<IpAddr>().unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
l.source(peer("10.0.0.1"), &HeaderMap::new()),
|
||||
"10.0.0.1".parse::<IpAddr>().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -220,8 +248,14 @@ mod tests {
|
||||
let c = l.source(peer("2001:db8:1:3::1"), &HeaderMap::new());
|
||||
assert_eq!(a, b);
|
||||
assert_ne!(a, c);
|
||||
assert_eq!(l.source(peer("::ffff:192.0.2.5"), &HeaderMap::new()), "192.0.2.5".parse::<IpAddr>().unwrap());
|
||||
assert_eq!(l.source(None, &HeaderMap::new()), IpAddr::V6(Ipv6Addr::UNSPECIFIED));
|
||||
assert_eq!(
|
||||
l.source(peer("::ffff:192.0.2.5"), &HeaderMap::new()),
|
||||
"192.0.2.5".parse::<IpAddr>().unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
l.source(None, &HeaderMap::new()),
|
||||
IpAddr::V6(Ipv6Addr::UNSPECIFIED)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user