Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): manually simplify patterns for fs scope #11730

Merged
merged 1 commit into from
Nov 19, 2024
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
7 changes: 5 additions & 2 deletions crates/tauri/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl<R: Runtime> AppManager<R> {
pub fn get_asset(
&self,
mut path: String,
use_https_schema: bool,
_use_https_schema: bool,
) -> Result<Asset, Box<dyn std::error::Error>> {
let assets = &self.assets;
if path.ends_with('/') {
Expand Down Expand Up @@ -440,7 +440,10 @@ impl<R: Runtime> AppManager<R> {
let default_src = csp_map
.entry("default-src".into())
.or_insert_with(Default::default);
default_src.push(crate::pattern::format_real_schema(schema, use_https_schema));
default_src.push(crate::pattern::format_real_schema(
schema,
_use_https_schema,
));
}

csp_header.replace(Csp::DirectiveMap(csp_map).to_string());
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/src/menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ macro_rules! gen_wrappers {
// SAFETY: inner was created on main thread and is being dropped on main thread
let inner = $crate::UnsafeSend(inner);
let _ = self.app_handle.run_on_main_thread(move || {
drop(inner);
drop(inner.take());
});
}
}
Expand Down
63 changes: 59 additions & 4 deletions crates/tauri/src/scope/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,27 @@ fn push_pattern<P: AsRef<Path>, F: Fn(&str) -> Result<Pattern, glob::PatternErro
// - `C:\\SomeDir`
#[cfg(windows)]
{
use std::path::Component;
use std::path::{Component, Path, Prefix};

let mut components = path.components();

let is_unc = match components.next() {
Some(Component::Prefix(p)) => match p.kind() {
Prefix::VerbatimDisk(..) => true,
_ => false, // Other kinds of UNC paths
},
_ => false, // relative or empty
};

if is_unc {
// we remove UNC manually, instead of `dunce::simplified` because
// `path` could have `*` in it and that's not allowed on Windows and
// `dunce::simplified` will check that and return `path` as is without simplification
let simplified = path
.to_str()
.and_then(|s| s.get(4..))
.map_or(path.as_path(), Path::new);

if matches!(path.components().next(), Some(Component::Prefix(_))) {
let simplified = dunce::simplified(&path);
let simplified_str = simplified.to_string_lossy();
if simplified_str != path_str {
list.insert(f(&simplified_str)?);
Expand Down Expand Up @@ -356,6 +373,7 @@ impl Scope {
.unwrap()
.iter()
.any(|p| p.matches_path_with(&path, self.match_options));

allowed
}
} else {
Expand All @@ -382,7 +400,11 @@ fn escaped_pattern_with(p: &str, append: &str) -> Result<Pattern, glob::PatternE

#[cfg(test)]
mod tests {
use super::Scope;
use std::collections::HashSet;

use glob::Pattern;

use super::{push_pattern, Scope};

fn new_scope() -> Scope {
Scope {
Expand Down Expand Up @@ -600,4 +622,37 @@ mod tests {
assert!(!scope.is_allowed("Q:Cargo.toml"));
}
}

#[test]
fn push_pattern_generated_paths() {
macro_rules! assert_pattern {
($patterns:ident, $pattern:literal) => {
assert!($patterns.contains(&Pattern::new($pattern).unwrap()))
};
}

let mut patterns = HashSet::new();

#[cfg(not(windows))]
{
push_pattern(&mut patterns, "/path/to/dir/", Pattern::new).expect("failed to push pattern");
push_pattern(&mut patterns, "/path/to/dir/**", Pattern::new).expect("failed to push pattern");

assert_pattern!(patterns, "/path/to/dir");
assert_pattern!(patterns, "/path/to/dir/**");
}

#[cfg(windows)]
{
push_pattern(&mut patterns, "C:\\path\\to\\dir", Pattern::new)
.expect("failed to push pattern");
push_pattern(&mut patterns, "C:\\path\\to\\dir\\**", Pattern::new)
.expect("failed to push pattern");

assert_pattern!(patterns, "C:\\path\\to\\dir");
assert_pattern!(patterns, "C:\\path\\to\\dir\\**");
assert_pattern!(patterns, "\\\\?\\C:\\path\\to\\dir");
assert_pattern!(patterns, "\\\\?\\C:\\path\\to\\dir\\**");
}
}
}