From e2b17fd06cbc7939662a2dfc112cfcac3ba5576e Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 19 Oct 2026 01:21:02 -0700 Subject: [PATCH] fix: recover config saves while preserving valid legacy trust Signed-off-by: fufesou --- src/config.rs | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index b82b2a296..105675a9f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -522,11 +522,18 @@ impl Config2 { fn store(&self) { let mut config = self.clone(); - let stored = match try_load_path(Self::file()).and_then(|stored: Self| { - stored.trusted_devices_v2()?; - Ok(stored) - }) { - Ok(stored) => stored, + let stored = match try_load_path_for_store::(Self::file()) { + Ok(mut stored) => { + if stored.trusted_devices_v2().is_err() { + crate::throttled_log!( + CONFIG_STORE_LOG_INTERVAL, + warn, + "Invalid trusted-device grants field; saving settings without V2 trust credentials" + ); + stored.trusted_devices_v2 = None; + } + stored + } Err(err) => { crate::throttled_log!( CONFIG_STORE_LOG_INTERVAL, @@ -637,6 +644,32 @@ fn try_load_path( } } +fn try_load_path_for_store( + path: PathBuf, +) -> crate::ResultType { + match confy::load_path(path) { + Ok(config) => Ok(config), + Err(error) + if matches!(&error, confy::ConfyError::BadTomlData(_)) + || matches!(&error, confy::ConfyError::ReadConfigurationFileError(error) + if error.kind() == std::io::ErrorKind::InvalidData) => + { + crate::throttled_log!( + CONFIG_STORE_LOG_INTERVAL, + warn, + "Invalid configuration file; saving settings without trust credentials" + ); + Ok(T::default()) + } + Err(confy::ConfyError::GeneralLoadError(error)) + if error.kind() == std::io::ErrorKind::NotFound => + { + Ok(T::default()) + } + Err(error) => Err(error.into()), + } +} + pub fn load_path( file: PathBuf, ) -> T { @@ -1776,7 +1809,7 @@ impl PeerConfig { } let _lock = PEER_CONFIG_WRITE_LOCK.lock().unwrap(); // A stale settings snapshot must not overwrite a newly issued credential. - config.trusted_device_v2 = match try_load_path::(Self::path(id)) { + config.trusted_device_v2 = match try_load_path_for_store::(Self::path(id)) { Ok(stored) => stored.trusted_device_v2, Err(err) => { crate::throttled_log!(