|
@@ -202,7 +202,21 @@ impl FromStr for RemoteUrlPattern {
|
|
|
type Err = urlpattern::quirks::Error;
|
|
|
|
|
|
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
|
|
|
- let init = urlpattern::UrlPatternInit::parse_constructor_string::<regex::Regex>(s, None)?;
|
|
|
+ let mut init = urlpattern::UrlPatternInit::parse_constructor_string::<regex::Regex>(s, None)?;
|
|
|
+ if init.search.as_ref().map(|p| p.is_empty()).unwrap_or(true) {
|
|
|
+ init.search.replace("*".to_string());
|
|
|
+ }
|
|
|
+ if init.hash.as_ref().map(|p| p.is_empty()).unwrap_or(true) {
|
|
|
+ init.hash.replace("*".to_string());
|
|
|
+ }
|
|
|
+ if init
|
|
|
+ .pathname
|
|
|
+ .as_ref()
|
|
|
+ .map(|p| p.is_empty() || p == "/")
|
|
|
+ .unwrap_or(true)
|
|
|
+ {
|
|
|
+ init.pathname.replace("*".to_string());
|
|
|
+ }
|
|
|
let pattern = urlpattern::UrlPattern::parse(init)?;
|
|
|
Ok(Self(Arc::new(pattern), s.to_string()))
|
|
|
}
|
|
@@ -251,6 +265,46 @@ pub enum ExecutionContext {
|
|
|
},
|
|
|
}
|
|
|
|
|
|
+#[cfg(test)]
|
|
|
+mod tests {
|
|
|
+ use crate::acl::RemoteUrlPattern;
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn url_pattern_domain_wildcard() {
|
|
|
+ let pattern: RemoteUrlPattern = "http://*".parse().unwrap();
|
|
|
+
|
|
|
+ assert!(pattern.test(&"http://tauri.app/path".parse().unwrap()));
|
|
|
+ assert!(pattern.test(&"http://tauri.app/path?q=1".parse().unwrap()));
|
|
|
+
|
|
|
+ assert!(pattern.test(&"http://localhost/path".parse().unwrap()));
|
|
|
+ assert!(pattern.test(&"http://localhost/path?q=1".parse().unwrap()));
|
|
|
+
|
|
|
+ let pattern: RemoteUrlPattern = "http://*.tauri.app".parse().unwrap();
|
|
|
+
|
|
|
+ assert!(!pattern.test(&"http://tauri.app/path".parse().unwrap()));
|
|
|
+ assert!(!pattern.test(&"http://tauri.app/path?q=1".parse().unwrap()));
|
|
|
+ assert!(pattern.test(&"http://api.tauri.app/path".parse().unwrap()));
|
|
|
+ assert!(pattern.test(&"http://api.tauri.app/path?q=1".parse().unwrap()));
|
|
|
+ assert!(!pattern.test(&"http://localhost/path".parse().unwrap()));
|
|
|
+ assert!(!pattern.test(&"http://localhost/path?q=1".parse().unwrap()));
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn url_pattern_path_wildcard() {
|
|
|
+ let pattern: RemoteUrlPattern = "http://localhost/*".parse().unwrap();
|
|
|
+ assert!(pattern.test(&"http://localhost/path".parse().unwrap()));
|
|
|
+ assert!(pattern.test(&"http://localhost/path?q=1".parse().unwrap()));
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn url_pattern_scheme_wildcard() {
|
|
|
+ let pattern: RemoteUrlPattern = "*://localhost".parse().unwrap();
|
|
|
+ assert!(pattern.test(&"http://localhost/path".parse().unwrap()));
|
|
|
+ assert!(pattern.test(&"https://localhost/path?q=1".parse().unwrap()));
|
|
|
+ assert!(pattern.test(&"custom://localhost/path".parse().unwrap()));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
#[cfg(feature = "build")]
|
|
|
mod build_ {
|
|
|
use std::convert::identity;
|