Skip to content

Commit

Permalink
perf: use canonicalized path for temp_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-jerry committed May 26, 2024
1 parent fb8fc56 commit 106f612
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
2 changes: 1 addition & 1 deletion core/tauri/src/api/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub fn resolve_path<P: AsRef<Path>>(
BaseDirectory::App => app_config_dir(config),
#[allow(deprecated)]
BaseDirectory::Log => app_log_dir(config),
BaseDirectory::Temp => Some(temp_dir()),
BaseDirectory::Temp => temp_dir().canonicalize().ok(),
BaseDirectory::AppConfig => app_config_dir(config),
BaseDirectory::AppData => app_data_dir(config),
BaseDirectory::AppLocalData => app_local_data_dir(config),
Expand Down
55 changes: 25 additions & 30 deletions core/tauri/src/scope/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,39 +228,34 @@ impl Scope {
/// Determines if the given path is allowed on this scope.
pub fn is_allowed<P: AsRef<Path>>(&self, path: P) -> bool {
let path = path.as_ref();

// https://github.com/tauri-apps/tauri/issues/6256
// check both raw path and canonicalized path
if self.check_is_allowed(path) {
return true;
}

if path.exists() {
return std::fs::canonicalize(path).map_or(false, |path| self.check_is_allowed(path));
}

false
}

fn check_is_allowed<P: AsRef<Path>>(&self, path: P) -> bool {
let path: PathBuf = path.as_ref().components().collect();
let forbidden = self
.forbidden_patterns
.lock()
.unwrap()
.iter()
.any(|p| p.matches_path_with(&path, self.match_options));

if forbidden {
false
let path = if !path.exists() {
crate::Result::Ok(path.to_path_buf())
} else {
let allowed = self
.allowed_patterns
std::fs::canonicalize(path).map_err(Into::into)
};

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

if forbidden {
false
} else {
let allowed = self
.allowed_patterns
.lock()
.unwrap()
.iter()
.any(|p| p.matches_path_with(&path, self.match_options));
allowed
}
} else {
false
}
}
}
Expand Down Expand Up @@ -406,9 +401,9 @@ mod tests {
};

let scope = new_scope();
scope.allow_directory(temp_dir(), true).unwrap();
scope.allow_directory(temp_dir().canonicalize().unwrap(), true).unwrap();

let test_temp_file = temp_dir().join("tauri_test_file");
let test_temp_file = temp_dir().canonicalize().unwrap().join("tauri_test_file");
if test_temp_file.exists() {
remove_file(test_temp_file.clone()).unwrap();
}
Expand Down

0 comments on commit 106f612

Please sign in to comment.