-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathreal_ip.rs
159 lines (139 loc) · 4.56 KB
/
real_ip.rs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use http::{HeaderMap, HeaderValue};
use ipnetwork::IpNetwork;
use std::iter::Iterator;
use std::net::IpAddr;
use std::str::from_utf8;
use std::sync::LazyLock;
const X_FORWARDED_FOR: &str = "X-Forwarded-For";
static CLOUD_FRONT_NETWORKS: LazyLock<Vec<IpNetwork>> = LazyLock::new(|| {
let ipv4_prefixes = aws_ip_ranges::IP_RANGES
.prefixes
.iter()
.filter(|prefix| prefix.service == "CLOUDFRONT")
.map(|prefix| prefix.ip_prefix);
let ipv6_prefixes = aws_ip_ranges::IP_RANGES
.ipv6_prefixes
.iter()
.filter(|prefix| prefix.service == "CLOUDFRONT")
.map(|prefix| prefix.ipv6_prefix);
ipv4_prefixes
.chain(ipv6_prefixes)
.filter_map(|prefix| match prefix.parse() {
Ok(ip_network) => Some(ip_network),
Err(error) => {
warn!(%error, "Failed to parse AWS CloudFront CIDR");
None
}
})
.collect()
});
fn is_cloud_front_ip(ip: &IpAddr) -> bool {
CLOUD_FRONT_NETWORKS
.iter()
.any(|trusted_proxy| trusted_proxy.contains(*ip))
}
/// Extracts the client IP address from the `X-Forwarded-For` header.
///
/// This function will return the last valid non-CloudFront IP address in the
/// `X-Forwarded-For` header, if any.
pub fn process_xff_headers(headers: &HeaderMap) -> Option<IpAddr> {
headers
.get_all(X_FORWARDED_FOR)
.iter()
.flat_map(|header| {
parse_xff_header(header)
.into_iter()
.filter_map(|r| r.ok())
.filter(|ip| !is_cloud_front_ip(ip))
})
.next_back()
}
/// Parses the content of an `X-Forwarded-For` header into a
/// `Vec<Result<IpAddr, &[u8]>>`.
fn parse_xff_header(header: &HeaderValue) -> Vec<Result<IpAddr, &[u8]>> {
let bytes = header.as_bytes();
if bytes.is_empty() {
return vec![];
}
bytes
.split(|&byte| byte == b',')
.map(|bytes| parse_ip_addr(bytes))
.collect()
}
fn parse_ip_addr(bytes: &[u8]) -> Result<IpAddr, &[u8]> {
from_utf8(bytes)
.map_err(|_| bytes)?
.trim()
.parse()
.map_err(|_| bytes)
}
#[cfg(test)]
mod tests {
use super::*;
use http::HeaderValue;
#[test]
fn test_process_xff_headers() {
#[track_caller]
fn test(input: Vec<&[u8]>, expectation: Option<&str>) {
let mut headers = HeaderMap::new();
for value in input {
let value = HeaderValue::from_bytes(value).unwrap();
headers.append(X_FORWARDED_FOR, value);
}
let expectation: Option<IpAddr> = expectation.map(|ip| ip.parse().unwrap());
assert_eq!(process_xff_headers(&headers), expectation)
}
// Generic behavior
test(vec![], None);
test(vec![b""], None);
test(vec![b"1.1.1.1"], Some("1.1.1.1"));
test(vec![b"1.1.1.1, 2.2.2.2"], Some("2.2.2.2"));
test(vec![b"1.1.1.1, 2.2.2.2, 3.3.3.3"], Some("3.3.3.3"));
test(
vec![b"oh, hi,,127.0.0.1,,,,, 12.34.56.78 "],
Some("12.34.56.78"),
);
// CloudFront behavior
test(vec![b"130.176.118.147"], None);
test(vec![b"1.1.1.1, 130.176.118.147"], Some("1.1.1.1"));
test(vec![b"1.1.1.1, 2.2.2.2, 130.176.118.147"], Some("2.2.2.2"));
// Multiple headers behavior
test(vec![b"1.1.1.1, 2.2.2.2", b"3.3.3.3"], Some("3.3.3.3"));
test(
vec![b"1.1.1.1, 130.176.118.147", b"3.3.3.3"],
Some("3.3.3.3"),
);
}
#[test]
fn test_parse_xff_header() {
#[track_caller]
fn test(input: &'static [u8], expectation: Vec<Result<&str, &[u8]>>) {
let header = HeaderValue::from_bytes(input).unwrap();
let expectation: Vec<Result<IpAddr, &[u8]>> = expectation
.into_iter()
.map(|ip| ip.map(|ip| ip.parse().unwrap()))
.collect();
assert_eq!(parse_xff_header(&header), expectation)
}
test(b"", vec![]);
test(b"1.2.3.4", vec![Ok("1.2.3.4")]);
test(
b"1.2.3.4, 11.22.33.44",
vec![Ok("1.2.3.4"), Ok("11.22.33.44")],
);
test(
b"oh, hi,,127.0.0.1,,,,, 12.34.56.78 ",
vec![
Err(b"oh"),
Err(b" hi"),
Err(b""),
Ok("127.0.0.1"),
Err(b""),
Err(b""),
Err(b""),
Err(b""),
Ok("12.34.56.78"),
],
);
}
}