Skip to content

Commit 944b124

Browse files
authored
feat(core): enhance HTTP scope glob validation, closes #3507 (#3515)
1 parent 6a6f1e7 commit 944b124

File tree

6 files changed

+46
-27
lines changed

6 files changed

+46
-27
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
The HTTP scope now matches the entire URL using a glob pattern instead of only its path.

core/tauri-utils/src/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,13 @@ impl Allowlist for DialogAllowlistConfig {
10911091

10921092
/// HTTP API scope definition.
10931093
/// It is a list of URLs that can be accessed by the webview when using the HTTP APIs.
1094-
/// The URL path is matched against the request URL using a glob pattern.
1094+
/// The scoped URL is matched against the request URL using a glob pattern.
1095+
///
1096+
/// # Examples
1097+
///
1098+
/// - "https://*": allows all HTTPS urls
1099+
/// - "https://*.github.com/tauri-apps/tauri": allows any subdomain of "github.com" with the "tauri-apps/api" path
1100+
/// - "https://myapi.service.com/users/*": allows access to any URLs that begins with "https://myapi.service.com/users/"
10951101
#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
10961102
#[cfg_attr(feature = "schema", derive(JsonSchema))]
10971103
pub struct HttpAllowlistScope(pub Vec<Url>);

core/tauri/src/scope/http.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,36 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5+
use glob::Pattern;
56
use tauri_utils::config::HttpAllowlistScope;
6-
use url::Url;
77

88
/// Scope for filesystem access.
99
#[derive(Debug, Clone)]
1010
pub struct Scope {
11-
allowed_urls: Vec<Url>,
11+
allowed_urls: Vec<Pattern>,
1212
}
1313

1414
impl Scope {
1515
/// Creates a new scope from the allowlist's `http` scope configuration.
1616
pub fn for_http_api(scope: &HttpAllowlistScope) -> Self {
1717
Self {
18-
allowed_urls: scope.0.clone(),
18+
allowed_urls: scope
19+
.0
20+
.iter()
21+
.map(|url| {
22+
glob::Pattern::new(url.as_str())
23+
.unwrap_or_else(|_| panic!("scoped URL is not a valid glob pattern: `{}`", url))
24+
})
25+
.collect(),
1926
}
2027
}
2128

2229
/// Determines if the given URL is allowed on this scope.
23-
pub fn is_allowed(&self, url: &Url) -> bool {
24-
self.allowed_urls.iter().any(|allowed| {
25-
let origin_matches = allowed.scheme() == url.scheme()
26-
&& allowed.host() == url.host()
27-
&& allowed.port() == url.port();
28-
let allowed_path_pattern = glob::Pattern::new(allowed.path())
29-
.unwrap_or_else(|_| panic!("invalid glob pattern on URL `{}` path", allowed));
30-
origin_matches && allowed_path_pattern.matches(url.path())
31-
})
30+
pub fn is_allowed(&self, url: &url::Url) -> bool {
31+
self
32+
.allowed_urls
33+
.iter()
34+
.any(|allowed| allowed.matches(url.as_str()))
3235
}
3336
}
3437

@@ -73,5 +76,10 @@ mod tests {
7376
assert!(scope.is_allowed(&"http://localhost:8080/assets/file.png".parse().unwrap()));
7477

7578
assert!(!scope.is_allowed(&"http://localhost:8080/file.jpeg".parse().unwrap()));
79+
80+
let scope = super::Scope::for_http_api(&HttpAllowlistScope(vec!["http://*".parse().unwrap()]));
81+
82+
assert!(scope.is_allowed(&"http://something.else".parse().unwrap()));
83+
assert!(!scope.is_allowed(&"https://something.else".parse().unwrap()));
7684
}
7785
}

core/tauri/tests/restart/Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src-tauri/Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@
10411041
"additionalProperties": false
10421042
},
10431043
"HttpAllowlistScope": {
1044-
"description": "HTTP API scope definition. It is a list of URLs that can be accessed by the webview when using the HTTP APIs. The URL path is matched against the request URL using a glob pattern.",
1044+
"description": "HTTP API scope definition. It is a list of URLs that can be accessed by the webview when using the HTTP APIs. The scoped URL is matched against the request URL using a glob pattern.\n\n# Examples\n\n- \"https://*\": allows all HTTPS urls - \"https://*.github.com/tauri-apps/tauri\": allows any subdomain of \"github.com\" with the \"tauri-apps/api\" path - \"https://myapi.service.com/users/*\": allows access to any URLs that begins with \"https://myapi.service.com/users/\"",
10451045
"type": "array",
10461046
"items": {
10471047
"type": "string",

0 commit comments

Comments
 (0)