mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-10 16:27:05 +02:00
feat(http): refactor and improvements (#428)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
@@ -2,10 +2,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use crate::config::HttpAllowlistScope;
|
||||
use glob::Pattern;
|
||||
use reqwest::Url;
|
||||
|
||||
use crate::config::HttpAllowlistScope;
|
||||
|
||||
/// Scope for filesystem access.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Scope {
|
||||
@@ -20,7 +21,7 @@ impl Scope {
|
||||
.0
|
||||
.iter()
|
||||
.map(|url| {
|
||||
glob::Pattern::new(url.as_str()).unwrap_or_else(|_| {
|
||||
glob::Pattern::new(url).unwrap_or_else(|_| {
|
||||
panic!("scoped URL is not a valid glob pattern: `{url}`")
|
||||
})
|
||||
})
|
||||
@@ -30,9 +31,10 @@ impl Scope {
|
||||
|
||||
/// Determines if the given URL is allowed on this scope.
|
||||
pub fn is_allowed(&self, url: &Url) -> bool {
|
||||
self.allowed_urls
|
||||
.iter()
|
||||
.any(|allowed| allowed.matches(url.as_str()))
|
||||
self.allowed_urls.iter().any(|allowed| {
|
||||
allowed.matches(url.as_str())
|
||||
|| allowed.matches(url.as_str().strip_suffix('/').unwrap_or_default())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +81,7 @@ mod tests {
|
||||
let scope = super::Scope::new(&HttpAllowlistScope(vec!["http://*".parse().unwrap()]));
|
||||
|
||||
assert!(scope.is_allowed(&"http://something.else".parse().unwrap()));
|
||||
assert!(!scope.is_allowed(&"http://something.else/path/to/file".parse().unwrap()));
|
||||
assert!(scope.is_allowed(&"http://something.else/path/to/file".parse().unwrap()));
|
||||
assert!(!scope.is_allowed(&"https://something.else".parse().unwrap()));
|
||||
|
||||
let scope = super::Scope::new(&HttpAllowlistScope(vec!["http://**".parse().unwrap()]));
|
||||
|
||||
Reference in New Issue
Block a user