Fix/preserve ipc reg recovery (#17278)

* fix(ipc): preserve the host privacy recovery record during settings updates

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(ipc): keep Windows config mirrors from overwriting host recovery

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(ipc): keep display recovery in the Windows host profile

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(ipc): preserve Windows recovery across sync and offline edits

Keep the original options for a round trip to an older host and filter the separate profile during persistence. Read and write single-option edits over the same authenticated connection. Use locked profile updates when the host pipe is absent.

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(ipc): keep Windows offline option edits and GUI acknowledgements consistent

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(ipc): limit recovery protection to configuration updates

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(ipc): preserve host recovery for LocalSystem clients

Signed-off-by: fufesou <linlong1266@gmail.com>

* test(ipc): cover LocalSystem host profile selection

Signed-off-by: fufesou <linlong1266@gmail.com>

* test(ipc): simplify recovery configuration checks

Signed-off-by: fufesou <linlong1266@gmail.com>

* chore: update hbb_common for configuration setter layout

Signed-off-by: fufesou <linlong1266@gmail.com>

* refactor(ipc): share the list of preserved configuration keys

Signed-off-by: fufesou <linlong1266@gmail.com>

* update hbb_common

Signed-off-by: fufesou <linlong1266@gmail.com>

---------

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou authored and GitHub committed 2026-11-29 22:28:40 +08:00
1 parent 130b84119b
commit 64fb67d4f3
8 files changed
+141 -9

No files matched your search

+3
View File
@@ -16,6 +16,7 @@ pub const OPTION_ENABLE_REMOTE_PRINTER: &str = "enable-remote-printer";
pub const OPTION_DISABLE_CLIPBOARD: &str = "disable_clipboard";
pub const OPTION_LOCK_AFTER_SESSION_END: &str = "lock_after_session_end";
pub const OPTION_PRIVACY_MODE: &str = "privacy_mode";
pub const OPTION_REG_RECOVERY: &str = "reg_recovery";
pub const OPTION_TOUCH_MODE: &str = "touch-mode";
pub const OPTION_SYNC_INIT_CLIPBOARD: &str = "sync-init-clipboard";
pub const OPTION_THEME: &str = "theme";
@@ -188,6 +189,8 @@ pub const OPTION_DISABLE_GROUP_PANEL: &str = "disable-group-panel";
pub const OPTION_DISABLE_DISCOVERY_PANEL: &str = "disable-discovery-panel";
pub const OPTION_PRE_ELEVATE_SERVICE: &str = "pre-elevate-service";
pub const IPC_PRESERVED_KEYS: &[&str] = &[OPTION_REG_RECOVERY];
// DEFAULT_DISPLAY_SETTINGS, OVERWRITE_DISPLAY_SETTINGS
pub const KEYS_DISPLAY_SETTINGS: &[&str] = &[
OPTION_VIEW_ONLY,
+27 -4
View File
@@ -1,5 +1,8 @@
#[path = "ipc/auth.rs"]
mod ipc_auth;
#[cfg(all(windows, test))]
#[path = "ipc/config_tests.rs"]
pub(crate) mod config_tests;
#[cfg(windows)]
#[path = "ipc/windows.rs"]
mod ipc_windows;
@@ -1086,7 +1089,8 @@ async fn handle(data: Data, stream: &mut Connection) {
},
Data::Options(value) => match value {
None => {
let v = Config::get_options();
let mut v = Config::get_options();
remove_preserved_keys(&mut v);
allow_err!(stream.send(&Data::Options(Some(v))).await);
}
Some(value) => {
@@ -1165,6 +1169,8 @@ async fn handle(data: Data, stream: &mut Connection) {
};
#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
let config2 = Config2::get();
let mut config2 = config2;
remove_preserved_keys(&mut config2.options);
allow_err!(
stream
.send(&Data::SyncConfig(Some((Config::get(), config2).into())))
@@ -2026,11 +2032,25 @@ pub async fn get_rendezvous_server(ms_timeout: u64) -> (String, Vec<String>) {
}
}
fn remove_preserved_keys(options: &mut HashMap<String, String>) {
for &key in keys::IPC_PRESERVED_KEYS {
options.remove(key);
}
}
fn cache_ipc_options(options: HashMap<String, String>) {
if crate::trusted_devices::config::uses_windows_host_profile() {
Config::set_options_preserving_keys(options, keys::IPC_PRESERVED_KEYS);
} else {
Config::set_options(options);
}
}
async fn get_options_(ms_timeout: u64) -> ResultType<HashMap<String, String>> {
let mut c = connect(ms_timeout, "").await?;
c.send(&Data::Options(None)).await?;
if let Some(Data::Options(Some(value))) = c.next_timeout(ms_timeout).await? {
Config::set_options(value.clone());
cache_ipc_options(value.clone());
Ok(value)
} else {
Ok(Config::get_options())
@@ -2065,7 +2085,8 @@ pub fn set_option(key: &str, value: &str) {
}
#[tokio::main(flavor = "current_thread")]
pub async fn set_options(value: HashMap<String, String>) -> ResultType<()> {
pub async fn set_options(mut value: HashMap<String, String>) -> ResultType<()> {
remove_preserved_keys(&mut value);
let _nat = CheckTestNatType::new();
let affected = crate::trusted_devices::config::changes_authentication(&value);
let connection = connect(1000, "").await;
@@ -2086,8 +2107,10 @@ pub async fn set_options(value: HashMap<String, String>) -> ResultType<()> {
_ if affected => bail!("The host did not confirm the configuration update"),
_ => {}
}
cache_ipc_options(value);
} else {
Config::set_options_preserving_keys(value, keys::IPC_PRESERVED_KEYS);
}
Config::set_options(value);
Ok(())
}
+69
View File
@@ -0,0 +1,69 @@
use super::*;
const RECORD: &str = "reg_recovery";
const SETTING: &str = "ipc-recovery-test-setting";
const TIMEOUT_MS: u64 = 1_000;
fn assert_synced(record: Option<&str>) {
for cfg in [Config2::get(), config::load_path(Config2::file())] {
assert_eq!(cfg.options.get(RECORD).map(String::as_str), record);
assert_eq!(cfg.options[SETTING], "gui");
}
}
async fn check_writes(client: &mut ConnectionTmpl<ConnClient>, base: &Config2) -> ResultType<()> {
for supplied in [None, Some(""), Some("active"), Some("old")] {
let mut cfg = base.clone();
if let Some(value) = supplied {
cfg.options.insert(RECORD.into(), value.into());
}
let options = Data::Options(Some(cfg.options.clone()));
let full = Data::SyncConfig(Some((Config::get(), cfg).into()));
for host in [Some("active"), None] {
Config::set_option(RECORD.into(), host.unwrap_or_default().into());
for request in [&options, &full] {
Config::set_option(SETTING.into(), "local".into());
client.send(request).await?;
let reply = client.next_timeout(TIMEOUT_MS).await?;
assert!(matches!(
(request, reply),
(Data::Options(_), Some(Data::Options(None)))
| (Data::SyncConfig(_), Some(Data::SyncConfig(None)))
));
assert_synced(host);
}
}
}
Ok(())
}
pub(crate) async fn ipc_preserves_reg_recovery() -> ResultType<()> {
let listener = tokio::spawn(start_with_listener("", new_listener("").await?));
let mut client = connect(TIMEOUT_MS, "").await?;
crate::common::set_server_running(true);
Config::set_option(RECORD.into(), "active".into());
let mut options = get_options_(TIMEOUT_MS).await?;
options.insert(SETTING.into(), "gui".into());
tokio::task::spawn_blocking(move || set_options(options)).await??;
client.send(&Data::SyncConfig(None)).await?;
let Some(Data::SyncConfig(Some(configs))) = client.next_timeout(TIMEOUT_MS).await? else {
bail!("expected full configuration");
};
let (config, snapshot) = *configs;
assert!(!snapshot.options.contains_key(RECORD));
for hosting in [true, false] {
crate::common::set_server_running(hosting);
let expected = (hosting || crate::platform::is_root()).then_some("active");
assert!(!get_options_(TIMEOUT_MS).await?.contains_key(RECORD));
assert_synced(expected);
Config::set_option(RECORD.into(), "active".into());
Config::set_option(SETTING.into(), "local".into());
crate::trusted_devices::config::sync_config(config.clone(), snapshot.clone())?;
assert_synced(expected);
}
check_writes(&mut client, &snapshot).await?;
Config::set_option(RECORD.into(), String::new());
Config::set_option(SETTING.into(), String::new());
listener.abort();
Ok(())
}
+1 -1
View File
@@ -31,7 +31,7 @@ use winapi::{
pub(super) const PRIVACY_MODE_IMPL: &str = super::PRIVACY_MODE_IMPL_WIN_VIRTUAL_DISPLAY;
const CONFIG_KEY_REG_RECOVERY: &str = "reg_recovery";
const CONFIG_KEY_REG_RECOVERY: &str = base::config::keys::OPTION_REG_RECOVERY;
const STATE_CHECK_LOG_INTERVAL: Duration = Duration::from_secs(60);
fn check_active_displays(displays: impl Iterator<Item = DISPLAY_DEVICEW>) -> ResultType<()> {
+11 -3
View File
@@ -1,4 +1,5 @@
use super::policy;
pub(crate) use super::policy::uses_windows_host_profile;
pub use super::policy::{changes_authentication, update_options};
use base::config::keys;
use hbb_common::{
@@ -47,7 +48,11 @@ pub async fn update_auth<T: Send + 'static>(
pub fn sync_config(value: Config, options: Config2) -> ResultType<()> {
let _storage = super::storage()?;
Config::set(value);
Config2::set(options.clone());
if uses_windows_host_profile() {
Config2::set_preserving_keys(options.clone(), keys::IPC_PRESERVED_KEYS);
} else {
Config2::set(options.clone());
}
snapshot()?;
// Explicit configuration sync carries the source's grants and removals.
options.sync_trusted_devices()
@@ -226,7 +231,7 @@ pub(super) fn change<T>(affected: bool, apply: impl FnOnce() -> ResultType<T>) -
pub fn set_options(options: HashMap<String, String>) -> ResultType<()> {
policy::change_options(effective_options(options.clone()), || {
Config::set_options(options);
Config::set_options_preserving_keys(options, keys::IPC_PRESERVED_KEYS);
Ok(())
})
}
@@ -270,7 +275,10 @@ pub fn set_config(value: Config) -> ResultType<bool> {
pub fn set_config2(value: Config2) -> ResultType<bool> {
policy::change_options(effective_options(value.options.clone()), || {
Ok(Config2::set(value))
Ok(Config2::set_preserving_keys(
value,
keys::IPC_PRESERVED_KEYS,
))
})
}
+25
View File
@@ -8,6 +8,19 @@ pub const NEVER_EXPIRE: u32 = 0;
pub const DAY_MILLIS: i64 = 24 * 60 * 60 * 1000;
const AUTH_OPTIONS: [&str; 2] = ["2fa", keys::OPTION_ENABLE_TRUSTED_DEVICES];
pub(crate) fn uses_windows_host_profile() -> bool {
#[cfg(windows)]
return uses_host_profile(crate::common::is_server_running(), crate::platform::is_root);
#[cfg(not(windows))]
false
}
#[cfg(any(windows, test))]
fn uses_host_profile(server_running: bool, is_local_system: fn() -> bool) -> bool {
// LocalSystem CLI commands share the service's configuration file.
server_running || is_local_system()
}
pub fn days(options: &HashMap<String, String>) -> u32 {
match options
.get(keys::OPTION_TRUSTED_DEVICE_EXPIRATION)
@@ -101,3 +114,15 @@ pub fn expires_at(time: i64, days: u32) -> i64 {
}
time.saturating_add(i64::from(days) * DAY_MILLIS)
}
#[cfg(test)]
mod tests {
use super::uses_host_profile;
#[test]
fn windows_host_profile_includes_local_system_clients() {
assert!(!uses_host_profile(false, || false));
assert!(uses_host_profile(false, || true));
assert!(uses_host_profile(true, || false));
}
}
+4
View File
@@ -79,6 +79,10 @@ fn legacy_proof(hwid: &[u8], info: &DeviceInfo, supports_v2: bool) -> host::Logi
#[hbb_common::tokio::test]
async fn password_and_configuration_revocation_gate_real_persisted_grants() {
let mut options = configure();
#[cfg(windows)]
crate::ipc::config_tests::ipc_preserves_reg_recovery()
.await
.unwrap();
configuration::malformed_host_field_preserves_authentication();
configuration::legacy_settings_survive_saving().await;
configuration::expiration_changes_preserve_grants().await;