Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/core-scope-is-forbidden.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
tauri: 'minor:feat'
---

Added `Scope::is_forbidden` to check if a path was explicitly forbidden.
52 changes: 38 additions & 14 deletions crates/tauri/src/scope/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,21 +339,12 @@ impl Scope {
}

/// Determines if the given path is allowed on this scope.
///
/// Returns `false` if the path was explicitly forbidden or neither allowed nor forbidden.
///
/// May return `false` if the path points to a broken symlink.
pub fn is_allowed<P: AsRef<Path>>(&self, path: P) -> bool {
let path = path.as_ref();
let path = if path.is_symlink() {
match std::fs::read_link(path) {
Ok(p) => p,
Err(_) => return false,
}
} else {
path.to_path_buf()
};
let path = if !path.exists() {
crate::Result::Ok(path)
} else {
std::fs::canonicalize(path).map_err(Into::into)
};
let path = try_resolve_symlink_and_canonicalize(path);

if let Ok(path) = path {
let path: PathBuf = path.components().collect();
Expand All @@ -380,6 +371,39 @@ impl Scope {
false
}
}

/// Determines if the given path is explicitly forbidden on this scope.
///
/// May return `true` if the path points to a broken symlink.
pub fn is_forbidden<P: AsRef<Path>>(&self, path: P) -> bool {
let path = try_resolve_symlink_and_canonicalize(path);

if let Ok(path) = path {
let path: PathBuf = path.components().collect();
self
.forbidden_patterns
.lock()
.unwrap()
.iter()
.any(|p| p.matches_path_with(&path, self.match_options))
} else {
true
}
}
}

fn try_resolve_symlink_and_canonicalize<P: AsRef<Path>>(path: P) -> crate::Result<PathBuf> {
let path = path.as_ref();
let path = if path.is_symlink() {
std::fs::read_link(path)?
} else {
path.to_path_buf()
};
if !path.exists() {
crate::Result::Ok(path)
} else {
std::fs::canonicalize(path).map_err(Into::into)
}
}

fn escaped_pattern(p: &str) -> Result<Pattern, glob::PatternError> {
Expand Down