From 5b2aed39a581ffa35ea6e84ac9d52c2fa2f769ab Mon Sep 17 00:00:00 2001 From: FabianLars Date: Thu, 9 Mar 2023 19:32:31 +0100 Subject: [PATCH] unescape when reading to try to fix existing files --- plugins/persisted-scope/src/lib.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/plugins/persisted-scope/src/lib.rs b/plugins/persisted-scope/src/lib.rs index f4ea57b5..0f927315 100644 --- a/plugins/persisted-scope/src/lib.rs +++ b/plugins/persisted-scope/src/lib.rs @@ -57,7 +57,6 @@ pub fn init() -> TauriPlugin { // We can't plainly filter for `*` because `*` is valid in paths on unix. let initial_fs_allowed = initial_scope(fs_scope.allowed_patterns()); let initial_fs_forbidden = initial_scope(fs_scope.forbidden_patterns()); - #[cfg(feature = "protocol-asset")] let initial_asset_allowed = initial_scope(asset_protocol_scope.allowed_patterns()); #[cfg(feature = "protocol-asset")] @@ -68,12 +67,18 @@ pub fn init() -> TauriPlugin { #[cfg(feature = "protocol-asset")] let _ = asset_protocol_scope.forbid_file(&scope_state_path); + // Ideally we would unescape the patterns only when saving OR reading, + // but we're trying to fix broken .persisted-scope files seemlessly. + let ac = AhoCorasick::new_auto_configured(PATTERNS); + if scope_state_path.exists() { let scope: Scope = tauri::api::file::read_binary(&scope_state_path) .map_err(Error::from) .and_then(|scope| bincode::deserialize(&scope).map_err(Into::into)) .unwrap_or_default(); for allowed in &scope.allowed_paths { + let allowed = ac.replace_all(allowed, REPLACE_WITH); + if !initial_fs_allowed.contains(&allowed) { let _ = fs_scope.allow_file(&allowed); } @@ -83,7 +88,8 @@ pub fn init() -> TauriPlugin { } } for forbidden in &scope.forbidden_patterns { - // forbid the path as is + let forbidden = ac.replace_all(forbidden, REPLACE_WITH); + if !initial_fs_forbidden.contains(&forbidden) { let _ = fs_scope.forbid_file(&forbidden); } @@ -94,9 +100,6 @@ pub fn init() -> TauriPlugin { } } - // We could also "fix" the paths on app start if we notice any runtime performance problems. - let ac = AhoCorasick::new_auto_configured(PATTERNS); - fs_scope.listen(move |event| { let fs_scope = app.fs_scope(); if let FsScopeEvent::PathAllowed(_) = event {