feat(http): refactor and improvements (#428)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir
2023-08-07 14:33:00 +03:00
committed by GitHub
parent 7f2e2dd5b8
commit 7d9df7297a
14 changed files with 747 additions and 1011 deletions
+8 -6
View File
@@ -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()]));