diff --git a/src/server/connection.rs b/src/server/connection.rs index e9ea313dd..83435fc45 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -1421,11 +1421,7 @@ impl Connection { } async fn check_whitelist(&mut self, addr: &SocketAddr) -> bool { - let whitelist: Vec = Config::get_option("whitelist") - .split(",") - .filter(|x| !x.is_empty()) - .map(|x| x.to_owned()) - .collect(); + let whitelist = ip_whitelist_entries(&Config::get_option("whitelist")); if !whitelist.is_empty() && whitelist .iter() @@ -7215,6 +7211,17 @@ fn wildcard_match(pattern: &str, text: &str) -> bool { pi == p.len() } +// A space around an entry ("10.0.0.1, 10.0.0.2") is ignored: `IpCidr` rejects " 10.0.0.2", so +// that address was blocked. Entries that are empty after the trim are dropped, so a +// whitespace-only value turns the list off, as in `check_id_whitelist` and the dialogs. +fn ip_whitelist_entries(option: &str) -> Vec { + option + .split(',') + .map(|x| x.trim().to_owned()) + .filter(|x| !x.is_empty()) + .collect() +} + #[cfg(test)] mod test { #[allow(unused)] @@ -7228,6 +7235,24 @@ mod test { UNAUTHORIZED_CONNS.lock().unwrap().len() } + #[test] + fn test_ip_whitelist_entries_ignore_spaces_around_them() { + let entries = ip_whitelist_entries("192.168.1.0/24, 10.0.0.1 "); + assert_eq!(entries, vec!["192.168.1.0/24", "10.0.0.1"]); + let ip: IpAddr = "10.0.0.1".parse().unwrap(); + assert!(entries + .iter() + .any(|x| IpCidr::from_str(x).map_or(false, |y| y.contains(ip)))); + } + + #[test] + fn test_ip_whitelist_entries_whitespace_only_is_off() { + for option in ["", ",", " ", " , "] { + assert!(ip_whitelist_entries(option).is_empty(), "{option:?}"); + } + assert_eq!(ip_whitelist_entries("10.0.0.1, "), vec!["10.0.0.1"]); + } + #[test] fn test_unauthorized_admission_is_per_address() { let _serial = UNAUTHORIZED_TESTS.lock().unwrap_or_else(|e| e.into_inner());