|
| 1 | +use anyhow::{anyhow, Context}; |
1 | 2 | use ipnetwork::IpNetwork;
|
2 | 3 |
|
3 | 4 | use crate::publish_rate_limit::PublishRateLimit;
|
@@ -94,7 +95,11 @@ impl Default for Server {
|
94 | 95 | match env_optional::<String>("WEB_PAGE_OFFSET_CIDR_BLOCKLIST") {
|
95 | 96 | None => vec![],
|
96 | 97 | Some(s) if s.is_empty() => vec![],
|
97 |
| - Some(s) => s.split(',').map(parse_cidr_block).collect(), |
| 98 | + Some(s) => s |
| 99 | + .split(',') |
| 100 | + .map(parse_cidr_block) |
| 101 | + .collect::<Result<_, _>>() |
| 102 | + .unwrap(), |
98 | 103 | };
|
99 | 104 |
|
100 | 105 | let base = Base::from_environment();
|
@@ -167,25 +172,21 @@ pub(crate) fn domain_name() -> String {
|
167 | 172 | /// * at least 16 for IPv4 based CIDRs.
|
168 | 173 | /// * at least 64 for IPv6 based CIDRs
|
169 | 174 | ///
|
170 |
| -fn parse_cidr_block(block: &str) -> IpNetwork { |
171 |
| - let network = block.parse(); |
172 |
| - match network { |
173 |
| - Ok(cidr) => { |
174 |
| - let host_prefix = match cidr { |
175 |
| - IpNetwork::V4(_) => 16, |
176 |
| - IpNetwork::V6(_) => 64, |
177 |
| - }; |
178 |
| - if cidr.prefix() < host_prefix { |
179 |
| - panic!( |
180 |
| - "WEB_PAGE_OFFSET_CIDR_BLOCKLIST only allows CIDR blocks with a host prefix \ |
181 |
| - of at least 16 bits (IPv4) or 64 bits (IPv6)." |
182 |
| - ); |
183 |
| - } else { |
184 |
| - cidr |
185 |
| - } |
186 |
| - } |
187 |
| - Err(_) => panic!("WEB_PAGE_OFFSET_CIDR_BLOCKLIST must contain IPv4 or IPv6 CIDR blocks."), |
| 175 | +fn parse_cidr_block(block: &str) -> anyhow::Result<IpNetwork> { |
| 176 | + let cidr = block |
| 177 | + .parse() |
| 178 | + .context("WEB_PAGE_OFFSET_CIDR_BLOCKLIST must contain IPv4 or IPv6 CIDR blocks.")?; |
| 179 | + |
| 180 | + let host_prefix = match cidr { |
| 181 | + IpNetwork::V4(_) => 16, |
| 182 | + IpNetwork::V6(_) => 64, |
| 183 | + }; |
| 184 | + |
| 185 | + if cidr.prefix() < host_prefix { |
| 186 | + return Err(anyhow!("WEB_PAGE_OFFSET_CIDR_BLOCKLIST only allows CIDR blocks with a host prefix of at least 16 bits (IPv4) or 64 bits (IPv6).")); |
188 | 187 | }
|
| 188 | + |
| 189 | + Ok(cidr) |
189 | 190 | }
|
190 | 191 |
|
191 | 192 | fn blocked_traffic() -> Vec<(String, Vec<String>)> {
|
@@ -230,37 +231,37 @@ fn parse_traffic_patterns_splits_on_comma_and_looks_for_equal_sign() {
|
230 | 231 |
|
231 | 232 | #[test]
|
232 | 233 | fn parse_cidr_block_list_successfully() {
|
233 |
| - assert_eq!( |
| 234 | + assert_ok_eq!( |
234 | 235 | parse_cidr_block("127.0.0.1/24"),
|
235 | 236 | "127.0.0.1/24".parse::<IpNetwork>().unwrap()
|
236 | 237 | );
|
237 |
| - assert_eq!( |
| 238 | + assert_ok_eq!( |
238 | 239 | parse_cidr_block("192.168.0.1/31"),
|
239 | 240 | "192.168.0.1/31".parse::<IpNetwork>().unwrap()
|
240 | 241 | );
|
241 | 242 | }
|
242 | 243 |
|
243 | 244 | #[test]
|
244 |
| -#[should_panic] |
245 | 245 | fn parse_cidr_blocks_panics_when_host_ipv4_prefix_is_too_low() {
|
246 |
| - parse_cidr_block("127.0.0.1/8"); |
| 246 | + assert_err!(parse_cidr_block("127.0.0.1/8")); |
247 | 247 | }
|
248 | 248 |
|
249 | 249 | #[test]
|
250 |
| -#[should_panic] |
251 | 250 | fn parse_cidr_blocks_panics_when_host_ipv6_prefix_is_too_low() {
|
252 |
| - parse_cidr_block("2001:0db8:0123:4567:89ab:cdef:1234:5678/56"); |
| 251 | + assert_err!(parse_cidr_block( |
| 252 | + "2001:0db8:0123:4567:89ab:cdef:1234:5678/56" |
| 253 | + )); |
253 | 254 | }
|
254 | 255 |
|
255 | 256 | #[test]
|
256 | 257 | fn parse_ipv6_based_cidr_blocks() {
|
257 |
| - assert_eq!( |
| 258 | + assert_ok_eq!( |
258 | 259 | parse_cidr_block("2002::1234:abcd:ffff:c0a8:101/64"),
|
259 | 260 | "2002::1234:abcd:ffff:c0a8:101/64"
|
260 | 261 | .parse::<IpNetwork>()
|
261 | 262 | .unwrap()
|
262 | 263 | );
|
263 |
| - assert_eq!( |
| 264 | + assert_ok_eq!( |
264 | 265 | parse_cidr_block("2001:0db8:0123:4567:89ab:cdef:1234:5678/92"),
|
265 | 266 | "2001:0db8:0123:4567:89ab:cdef:1234:5678/92"
|
266 | 267 | .parse::<IpNetwork>()
|
|
0 commit comments