From fdffc37bb067d14389350937effa3269b52b656d Mon Sep 17 00:00:00 2001 From: Lukas <103962359+L4XB@users.noreply.github.com> Date: Fri, 6 Nov 2026 03:00:54 +0100 Subject: [PATCH] fix(server): ignore spaces around IP whitelist entries (#16692) * fix(server): ignore spaces around IP whitelist entries * fix(server): parse the IP and ID whitelists the same way, and fail closed Both checks now read their option through one parser that accepts the separators the settings dialogs accept (commas, semicolons, whitespace) and drops empty entries. The IP whitelist split only at commas, so an entry with a space around it, or a list from custom client settings separated by semicolons or newlines, matched no address and locked the listed ones out. A list is off only when the option is empty or holds only commas, which is how the dialogs store a cleared list. Any other value without entries, such as a lone space, now blocks every peer for the ID whitelist too, where it used to let everyone in. * fix(flutter): skip empty entries in the IP whitelist dialog A leading or trailing comma gave an empty entry that the dialog rejected as an invalid IP. Drop empty entries before the check, as the ID whitelist dialog already does. * fix(flutter): reject whitelist input that holds only separators Dropping empty entries made the IP dialog save `,` or `;` as a cleared whitelist, which turned the restriction off; before, that input failed the IP check, and the server reads the same text as a list that allows no one. Both dialogs now split through whitelistInputEntries, which returns null for separator-only input, and show "Invalid IP" or "Invalid ID" for it. Empty input and the Clear button still clear the whitelist. * fix(flutter): keep a whitelist that lets no one in when its dialog is saved unchanged A stored whitelist such as " , " is on but names no entry, so the server lets no one in. Its dialog shows it as blank lines, and OK pressed without an edit trimmed those to an empty field and saved a cleared whitelist, lifting the restriction. Both dialogs now take the shown text from whitelistDialogText and, when the field is unchanged and whitelistLetsNoOneIn says the stored value lets no one in, close without saving. Clearing the field and the Clear button still clear the whitelist. The tests cover the load, unchanged OK and save path for both cleared values (',' and ''). * style(flutter): drop a doubled blank line in whitelist_input.dart * fix(ui): keep a whitelist that lets no one in when the Sciter dialog is saved unchanged The Sciter IP whitelist dialog shows " , " as blank lines, trimmed the submitted text before comparing it with what it showed, and so saved an unchanged deny-all whitelist as a cleared one. The submitted text is now compared first, as the Flutter dialogs do. * test(flutter): name the round-trip test after the helper it checks * fix(server): narrow the change to ignoring spaces around IP whitelist entries As the review suggests, only the original fix stays: IP whitelist entries are trimmed, so "192.168.1.0/24, 10.0.0.1" admits 10.0.0.1. The ID whitelist keeps master's parsing and its "empty means allow" behavior. The Flutter dialogs and the Sciter UI are back to master, and the shared parse_whitelist and the dialog helpers are gone. The trim lives in ip_whitelist_entries so that one regression test can pin it. A whitespace-only IP whitelist still blocks everyone, as on master. * Refactor IP whitelist entries handling and comments Updated comments for clarity and adjusted whitespace handling in IP whitelist entries. --------- Co-authored-by: RustDesk <71636191+rustdesk@users.noreply.github.com> --- src/server/connection.rs | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) 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());