From e1b0c9a1ad0436919d2c38fe0a234a9405016dc7 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Mon, 19 Sep 2022 16:33:58 +0200 Subject: [PATCH] also fix the public api --- core/tauri/src/scope/fs.rs | 13 +++++++++---- core/tauri/src/scope/mod.rs | 15 ++++----------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/core/tauri/src/scope/fs.rs b/core/tauri/src/scope/fs.rs index 44f4e2e25b0..6cec83c32a4 100644 --- a/core/tauri/src/scope/fs.rs +++ b/core/tauri/src/scope/fs.rs @@ -139,7 +139,7 @@ impl Scope { /// After this function has been called, the frontend will be able to use the Tauri API to read /// the directory and all of its files and subdirectories. pub fn allow_directory>(&self, path: P, recursive: bool) -> crate::Result<()> { - let path = path.as_ref().to_path_buf(); + let path = escape_pattern_in_path(path); { let mut list = self.allowed_patterns.lock().unwrap(); @@ -156,7 +156,7 @@ impl Scope { /// /// After this function has been called, the frontend will be able to use the Tauri API to read the contents of this file. pub fn allow_file>(&self, path: P) -> crate::Result<()> { - let path = path.as_ref(); + let path = escape_pattern_in_path(path); push_pattern(&mut self.allowed_patterns.lock().unwrap(), &path)?; self.trigger(Event::PathAllowed(path.to_path_buf())); Ok(()) @@ -166,7 +166,7 @@ impl Scope { /// /// **Note:** this takes precedence over allowed paths, so its access gets denied **always**. pub fn forbid_directory>(&self, path: P, recursive: bool) -> crate::Result<()> { - let path = path.as_ref().to_path_buf(); + let path = escape_pattern_in_path(path); { let mut list = self.forbidden_patterns.lock().unwrap(); @@ -183,7 +183,7 @@ impl Scope { /// /// **Note:** this takes precedence over allowed paths, so its access gets denied **always**. pub fn forbid_file>(&self, path: P) -> crate::Result<()> { - let path = path.as_ref(); + let path = escape_pattern_in_path(path); push_pattern(&mut self.forbidden_patterns.lock().unwrap(), &path)?; self.trigger(Event::PathForbidden(path.to_path_buf())); Ok(()) @@ -224,3 +224,8 @@ impl Scope { } } } + +fn escape_pattern_in_path>(p: P) -> PathBuf { + let p = p.as_ref().to_string_lossy(); + PathBuf::from(glob::Pattern::escape(&p)) +} diff --git a/core/tauri/src/scope/mod.rs b/core/tauri/src/scope/mod.rs index 3f8e4f626d5..d1055e71250 100644 --- a/core/tauri/src/scope/mod.rs +++ b/core/tauri/src/scope/mod.rs @@ -30,24 +30,17 @@ pub(crate) struct Scopes { impl Scopes { #[allow(dead_code)] pub(crate) fn allow_directory(&self, path: &Path, recursive: bool) -> crate::Result<()> { - let path = path.to_string_lossy(); - let escaped_path = glob::Pattern::escape(&path); - - self.fs.allow_directory(&escaped_path, recursive)?; + self.fs.allow_directory(path, recursive)?; #[cfg(protocol_asset)] - self - .asset_protocol - .allow_directory(&escaped_path, recursive)?; + self.asset_protocol.allow_directory(path, recursive)?; Ok(()) } #[allow(dead_code)] pub(crate) fn allow_file(&self, path: &Path) -> crate::Result<()> { - let path = path.to_string_lossy(); - let escaped_path = glob::Pattern::escape(&path); - self.fs.allow_file(&escaped_path)?; + self.fs.allow_file(path)?; #[cfg(protocol_asset)] - self.asset_protocol.allow_file(&escaped_path)?; + self.asset_protocol.allow_file(path)?; Ok(()) } }