-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork.rs
More file actions
117 lines (105 loc) · 3.98 KB
/
network.rs
File metadata and controls
117 lines (105 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std::net::IpAddr;
/// Figures out the real client IP. The thing is -- we only trust XFF/X-Real-IP
/// when the peer is loopback, i.e. there's a local reverse proxy in front of us.
/// Otherwise a remote client could just spoof those headers.
pub fn extract_client_ip(
headers: &axum::http::HeaderMap,
peer_addr: Option<std::net::SocketAddr>,
) -> Option<String> {
let peer_ip = peer_addr.map(|a| a.ip());
let peer_is_loopback = peer_ip.is_some_and(|ip| ip.is_loopback());
if peer_is_loopback {
// First entry in XFF is the original client
if let Some(xff) = headers.get("x-forwarded-for").and_then(|v| v.to_str().ok()) {
if let Some(first) = xff.split(',').next() {
let trimmed = first.trim();
if !trimmed.is_empty() && trimmed.parse::<IpAddr>().is_ok() {
return Some(trimmed.to_string());
}
}
}
if let Some(real_ip) = headers.get("x-real-ip").and_then(|v| v.to_str().ok()) {
let trimmed = real_ip.trim();
if !trimmed.is_empty() && trimmed.parse::<IpAddr>().is_ok() {
return Some(trimmed.to_string());
}
}
}
peer_ip.map(|ip| ip.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extract_client_ip_xff_from_loopback() {
let mut headers = axum::http::HeaderMap::new();
headers.insert(
"x-forwarded-for",
"203.0.113.1, 70.41.3.18".parse().unwrap(),
);
let loopback: std::net::SocketAddr = "127.0.0.1:12345".parse().unwrap();
assert_eq!(
extract_client_ip(&headers, Some(loopback)),
Some("203.0.113.1".to_string())
);
}
#[test]
fn extract_client_ip_xff_ignored_from_remote() {
let mut headers = axum::http::HeaderMap::new();
headers.insert(
"x-forwarded-for",
"203.0.113.1, 70.41.3.18".parse().unwrap(),
);
let remote: std::net::SocketAddr = "198.51.100.1:12345".parse().unwrap();
// XFF gets ignored when peer isn't loopback
assert_eq!(
extract_client_ip(&headers, Some(remote)),
Some("198.51.100.1".to_string())
);
}
#[test]
fn extract_client_ip_real_ip_from_loopback() {
let mut headers = axum::http::HeaderMap::new();
headers.insert("x-real-ip", "198.51.100.42".parse().unwrap());
let loopback: std::net::SocketAddr = "127.0.0.1:12345".parse().unwrap();
assert_eq!(
extract_client_ip(&headers, Some(loopback)),
Some("198.51.100.42".to_string())
);
}
#[test]
fn extract_client_ip_fallback_to_connect() {
let headers = axum::http::HeaderMap::new();
let addr: std::net::SocketAddr = "127.0.0.1:12345".parse().unwrap();
assert_eq!(
extract_client_ip(&headers, Some(addr)),
Some("127.0.0.1".to_string())
);
}
#[test]
fn extract_client_ip_none() {
let headers = axum::http::HeaderMap::new();
assert_eq!(extract_client_ip(&headers, None), None);
}
#[test]
fn extract_client_ip_invalid_xff_rejected() {
let mut headers = axum::http::HeaderMap::new();
headers.insert("x-forwarded-for", "not-an-ip, 10.0.0.1".parse().unwrap());
let loopback: std::net::SocketAddr = "127.0.0.1:12345".parse().unwrap();
// Bogus IP in XFF -- falls through to the peer address
assert_eq!(
extract_client_ip(&headers, Some(loopback)),
Some("127.0.0.1".to_string())
);
}
#[test]
fn extract_client_ip_invalid_real_ip_rejected() {
let mut headers = axum::http::HeaderMap::new();
headers.insert("x-real-ip", "garbage".parse().unwrap());
let loopback: std::net::SocketAddr = "127.0.0.1:12345".parse().unwrap();
assert_eq!(
extract_client_ip(&headers, Some(loopback)),
Some("127.0.0.1".to_string())
);
}
}