just testing some smaller workarounds before pulling out the big guns

pull/2070/head
fabianlars 8 months ago
parent b42064dc70
commit caac44d6ee
No known key found for this signature in database

@ -4,14 +4,13 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
path::{Path, PathBuf, MAIN_SEPARATOR}, path::{Path, PathBuf},
sync::{ sync::{
atomic::{AtomicU32, Ordering}, atomic::{AtomicU32, Ordering},
Mutex, Mutex,
}, },
}; };
use glob::Pattern;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -58,8 +57,8 @@ impl Scope {
{ {
let mut allowed = self.allowed.lock().unwrap(); let mut allowed = self.allowed.lock().unwrap();
let p = path.to_string_lossy(); let p = path.to_string_lossy();
allowed.push(escaped_pattern(&p)); allowed.push(escape(&p));
allowed.push(escaped_pattern_with(&p, if recursive { "**" } else { "*" })); allowed.push(PathBuf::from(if recursive { "**" } else { "*" }));
} }
self.emit(Event::PathAllowed(path.to_path_buf())); self.emit(Event::PathAllowed(path.to_path_buf()));
@ -74,7 +73,7 @@ impl Scope {
self.allowed self.allowed
.lock() .lock()
.unwrap() .unwrap()
.push(escaped_pattern(&path.to_string_lossy())); .push(escape(&path.to_string_lossy()));
self.emit(Event::PathAllowed(path.to_path_buf())); self.emit(Event::PathAllowed(path.to_path_buf()));
} }
@ -88,8 +87,8 @@ impl Scope {
{ {
let mut denied = self.denied.lock().unwrap(); let mut denied = self.denied.lock().unwrap();
let p = path.to_string_lossy(); let p = path.to_string_lossy();
denied.push(escaped_pattern(&p)); denied.push(escape(&p));
denied.push(escaped_pattern_with(&p, if recursive { "**" } else { "*" })); denied.push(PathBuf::from(if recursive { "**" } else { "*" }));
} }
self.emit(Event::PathForbidden(path.to_path_buf())); self.emit(Event::PathForbidden(path.to_path_buf()));
@ -104,7 +103,7 @@ impl Scope {
self.denied self.denied
.lock() .lock()
.unwrap() .unwrap()
.push(escaped_pattern(&path.to_string_lossy())); .push(escape(&path.to_string_lossy()));
self.emit(Event::PathForbidden(path.to_path_buf())); self.emit(Event::PathForbidden(path.to_path_buf()));
} }
@ -139,14 +138,26 @@ impl Scope {
} }
} }
fn escaped_pattern(p: &str) -> Result<Pattern, glob::PatternError> { // taken from https://github.com/rust-lang/glob/blob/master/src/lib.rs#L717C5-L737C6
Pattern::new(&Pattern::escape(p)) /// Escape metacharacters within the given string by surrounding them in
} /// brackets. The resulting string will, when compiled into a `Pattern`,
/// match the input string and nothing else.
fn escaped_pattern_with(p: &str, append: &str) -> Result<Pattern, glob::PatternError> { pub fn escape(s: &str) -> PathBuf {
if p.ends_with(MAIN_SEPARATOR) { let mut escaped = String::new();
Pattern::new(&format!("{}{append}", Pattern::escape(p))) for c in s.chars() {
} else { match c {
Pattern::new(&format!("{}{}{append}", Pattern::escape(p), MAIN_SEPARATOR)) // note that ! does not need escaping because it is only special
// inside brackets
/* disabled to not break paths '?' | */
'*' | '[' | ']' => {
escaped.push('[');
escaped.push(c);
escaped.push(']');
}
c => {
escaped.push(c);
}
}
} }
PathBuf::from(escaped)
} }

Loading…
Cancel
Save