Skip to content

Commit 61ef88f

Browse files
authored
real_ip: Replace hardcoded CIDR list with aws-ip-ranges crate (#7532)
1 parent 13f8392 commit 61ef88f

File tree

3 files changed

+32
-138
lines changed

3 files changed

+32
-138
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ slow-tests = []
3434
anyhow = "=1.0.75"
3535
async-trait = "=0.1.74"
3636
aws-credential-types = { version = "=0.57.2", features = ["hardcoded-credentials"] }
37+
aws-ip-ranges = "=0.2.0"
3738
aws-sdk-cloudfront = "=0.36.0"
3839
axum = { version = "=0.6.20", features = ["headers", "macros", "matched-path"] }
3940
axum-extra = { version = "=0.8.0", features = ["cookie-signed"] }

src/real_ip.rs

Lines changed: 20 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -7,146 +7,28 @@ use std::str::from_utf8;
77

88
const X_FORWARDED_FOR: &str = "X-Forwarded-For";
99

10-
const CLOUD_FRONT_STRS: &[&str] = &[
11-
// CloudFront IP addresses from http://d7uri8nf7uskq.cloudfront.net/tools/list-cloudfront-ips
12-
// Last updated: 2022-03-26
13-
"3.10.17.128/25",
14-
"3.11.53.0/24",
15-
"3.35.130.128/25",
16-
"3.101.158.0/23",
17-
"3.128.93.0/24",
18-
"3.134.215.0/24",
19-
"3.231.2.0/25",
20-
"3.234.232.224/27",
21-
"3.236.48.0/23",
22-
"3.236.169.192/26",
23-
"13.32.0.0/15",
24-
"13.35.0.0/16",
25-
"13.48.32.0/24",
26-
"13.54.63.128/26",
27-
"13.59.250.0/26",
28-
"13.113.196.64/26",
29-
"13.113.203.0/24",
30-
"13.124.199.0/24",
31-
"13.210.67.128/26",
32-
"13.224.0.0/14",
33-
"13.228.69.0/24",
34-
"13.233.177.192/26",
35-
"13.249.0.0/16",
36-
"15.158.0.0/16",
37-
"15.188.184.0/24",
38-
"15.207.13.128/25",
39-
"15.207.213.128/25",
40-
"18.64.0.0/14",
41-
"18.154.0.0/15",
42-
"18.160.0.0/15",
43-
"18.164.0.0/15",
44-
"18.172.0.0/15",
45-
"18.192.142.0/23",
46-
"18.200.212.0/23",
47-
"18.216.170.128/25",
48-
"18.229.220.192/26",
49-
"18.238.0.0/15",
50-
"18.244.0.0/15",
51-
"34.195.252.0/24",
52-
"34.216.51.0/25",
53-
"34.223.12.224/27",
54-
"34.223.80.192/26",
55-
"34.226.14.0/24",
56-
"35.158.136.0/24",
57-
"35.162.63.192/26",
58-
"35.167.191.128/26",
59-
"36.103.232.0/25",
60-
"36.103.232.128/26",
61-
"44.227.178.0/24",
62-
"44.234.90.252/30",
63-
"44.234.108.128/25",
64-
"52.15.127.128/26",
65-
"52.46.0.0/18",
66-
"52.47.139.0/24",
67-
"52.52.191.128/26",
68-
"52.56.127.0/25",
69-
"52.57.254.0/24",
70-
"52.66.194.128/26",
71-
"52.78.247.128/26",
72-
"52.82.128.0/19",
73-
"52.84.0.0/15",
74-
"52.124.128.0/17",
75-
"52.199.127.192/26",
76-
"52.212.248.0/26",
77-
"52.220.191.0/26",
78-
"52.222.128.0/17",
79-
"54.182.0.0/16",
80-
"54.192.0.0/16",
81-
"54.230.0.0/17",
82-
"54.230.128.0/18",
83-
"54.230.200.0/21",
84-
"54.230.208.0/20",
85-
"54.230.224.0/19",
86-
"54.233.255.128/26",
87-
"54.239.128.0/18",
88-
"54.239.192.0/19",
89-
"54.240.128.0/18",
90-
"58.254.138.0/25",
91-
"58.254.138.128/26",
92-
"64.252.64.0/18",
93-
"64.252.128.0/18",
94-
"65.8.0.0/16",
95-
"65.9.0.0/17",
96-
"65.9.128.0/18",
97-
"70.132.0.0/18",
98-
"71.152.0.0/17",
99-
"99.79.169.0/24",
100-
"99.84.0.0/16",
101-
"99.86.0.0/16",
102-
"108.138.0.0/15",
103-
"108.156.0.0/14",
104-
"116.129.226.0/25",
105-
"116.129.226.128/26",
106-
"118.193.97.64/26",
107-
"118.193.97.128/25",
108-
"119.147.182.0/25",
109-
"119.147.182.128/26",
110-
"120.52.12.64/26",
111-
"120.52.22.96/27",
112-
"120.52.39.128/27",
113-
"120.52.153.192/26",
114-
"120.232.236.0/25",
115-
"120.232.236.128/26",
116-
"120.253.240.192/26",
117-
"120.253.241.160/27",
118-
"120.253.245.128/26",
119-
"120.253.245.192/27",
120-
"130.176.0.0/17",
121-
"130.176.128.0/18",
122-
"130.176.192.0/19",
123-
"130.176.224.0/20",
124-
"143.204.0.0/16",
125-
"144.220.0.0/16",
126-
"180.163.57.0/25",
127-
"180.163.57.128/26",
128-
"204.246.164.0/22",
129-
"204.246.168.0/22",
130-
"204.246.172.0/24",
131-
"204.246.173.0/24",
132-
"204.246.174.0/23",
133-
"204.246.176.0/20",
134-
"205.251.200.0/21",
135-
"205.251.208.0/20",
136-
"205.251.249.0/24",
137-
"205.251.250.0/23",
138-
"205.251.252.0/23",
139-
"205.251.254.0/24",
140-
"216.137.32.0/19",
141-
"223.71.11.0/27",
142-
"223.71.71.96/27",
143-
"223.71.71.128/25",
144-
];
145-
14610
static CLOUD_FRONT_NETWORKS: Lazy<Vec<IpNetwork>> = Lazy::new(|| {
147-
CLOUD_FRONT_STRS
11+
let ipv4_prefixes = aws_ip_ranges::IP_RANGES
12+
.prefixes
13+
.iter()
14+
.filter(|prefix| prefix.service == "CLOUDFRONT")
15+
.map(|prefix| prefix.ip_prefix);
16+
17+
let ipv6_prefixes = aws_ip_ranges::IP_RANGES
18+
.ipv6_prefixes
14819
.iter()
149-
.map(|s| s.parse().unwrap())
20+
.filter(|prefix| prefix.service == "CLOUDFRONT")
21+
.map(|prefix| prefix.ipv6_prefix);
22+
23+
ipv4_prefixes
24+
.chain(ipv6_prefixes)
25+
.filter_map(|prefix| match prefix.parse() {
26+
Ok(ip_network) => Some(ip_network),
27+
Err(error) => {
28+
warn!(%error, "Failed to parse AWS CloudFront CIDR");
29+
None
30+
}
31+
})
15032
.collect()
15133
});
15234

0 commit comments

Comments
 (0)