Skip to content

Commit cfb6734

Browse files
committed
config: Return Result from parse_cidr_block() instead of panicking
1 parent 29b1217 commit cfb6734

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

src/config.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::{anyhow, Context};
12
use ipnetwork::IpNetwork;
23

34
use crate::publish_rate_limit::PublishRateLimit;
@@ -94,7 +95,11 @@ impl Default for Server {
9495
match env_optional::<String>("WEB_PAGE_OFFSET_CIDR_BLOCKLIST") {
9596
None => vec![],
9697
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(),
98103
};
99104

100105
let base = Base::from_environment();
@@ -167,25 +172,21 @@ pub(crate) fn domain_name() -> String {
167172
/// * at least 16 for IPv4 based CIDRs.
168173
/// * at least 64 for IPv6 based CIDRs
169174
///
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)."));
188187
}
188+
189+
Ok(cidr)
189190
}
190191

191192
fn blocked_traffic() -> Vec<(String, Vec<String>)> {
@@ -230,37 +231,37 @@ fn parse_traffic_patterns_splits_on_comma_and_looks_for_equal_sign() {
230231

231232
#[test]
232233
fn parse_cidr_block_list_successfully() {
233-
assert_eq!(
234+
assert_ok_eq!(
234235
parse_cidr_block("127.0.0.1/24"),
235236
"127.0.0.1/24".parse::<IpNetwork>().unwrap()
236237
);
237-
assert_eq!(
238+
assert_ok_eq!(
238239
parse_cidr_block("192.168.0.1/31"),
239240
"192.168.0.1/31".parse::<IpNetwork>().unwrap()
240241
);
241242
}
242243

243244
#[test]
244-
#[should_panic]
245245
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"));
247247
}
248248

249249
#[test]
250-
#[should_panic]
251250
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+
));
253254
}
254255

255256
#[test]
256257
fn parse_ipv6_based_cidr_blocks() {
257-
assert_eq!(
258+
assert_ok_eq!(
258259
parse_cidr_block("2002::1234:abcd:ffff:c0a8:101/64"),
259260
"2002::1234:abcd:ffff:c0a8:101/64"
260261
.parse::<IpNetwork>()
261262
.unwrap()
262263
);
263-
assert_eq!(
264+
assert_ok_eq!(
264265
parse_cidr_block("2001:0db8:0123:4567:89ab:cdef:1234:5678/92"),
265266
"2001:0db8:0123:4567:89ab:cdef:1234:5678/92"
266267
.parse::<IpNetwork>()

0 commit comments

Comments
 (0)