mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-07-14 19:10:28 +03:00
refactor: configure URLs instead of domains on capability remote (#8898)
This commit is contained in:
parent
8d16a80d2f
commit
f284f9c545
6
.changes/refactor-capability-remote-option.md
Normal file
6
.changes/refactor-capability-remote-option.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"tauri-utils": patch:breaking
|
||||
"tauri": patch:breaking
|
||||
---
|
||||
|
||||
Changed the capability `remote` configuration to take a list of `urls` instead of `domains` for more flexibility.
|
@ -1151,10 +1151,10 @@
|
||||
"remote": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"domains"
|
||||
"urls"
|
||||
],
|
||||
"properties": {
|
||||
"domains": {
|
||||
"urls": {
|
||||
"description": "Remote domains this capability refers to. Can use glob patterns.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
|
@ -99,7 +99,7 @@ pub enum CapabilityContext {
|
||||
/// Capability refers to remote usage.
|
||||
Remote {
|
||||
/// Remote domains this capability refers to. Can use glob patterns.
|
||||
domains: Vec<String>,
|
||||
urls: Vec<String>,
|
||||
},
|
||||
}
|
||||
|
||||
@ -159,9 +159,9 @@ mod build {
|
||||
let prefix = quote! { ::tauri::utils::acl::capability::CapabilityContext };
|
||||
|
||||
tokens.append_all(match self {
|
||||
Self::Remote { domains } => {
|
||||
let domains = vec_lit(domains, str_lit);
|
||||
quote! { #prefix::Remote { domains: #domains } }
|
||||
Self::Remote { urls } => {
|
||||
let urls = vec_lit(urls, str_lit);
|
||||
quote! { #prefix::Remote { urls: #urls } }
|
||||
}
|
||||
Self::Local => {
|
||||
quote! { #prefix::Local }
|
||||
|
@ -189,8 +189,8 @@ pub enum ExecutionContext {
|
||||
Local,
|
||||
/// Remote URL is tring to use the IPC.
|
||||
Remote {
|
||||
/// The domain trying to access the IPC (glob pattern).
|
||||
domain: Pattern,
|
||||
/// The URL trying to access the IPC (glob pattern).
|
||||
url: Pattern,
|
||||
},
|
||||
}
|
||||
|
||||
@ -212,9 +212,9 @@ mod build_ {
|
||||
Self::Local => {
|
||||
quote! { #prefix::Local }
|
||||
}
|
||||
Self::Remote { domain } => {
|
||||
let domain = domain.as_str();
|
||||
quote! { #prefix::Remote { domain: #domain.parse().unwrap() } }
|
||||
Self::Remote { url } => {
|
||||
let url = url.as_str();
|
||||
quote! { #prefix::Remote { url: #url.parse().unwrap() } }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -349,11 +349,11 @@ fn resolve_command(
|
||||
CapabilityContext::Local => {
|
||||
vec![ExecutionContext::Local]
|
||||
}
|
||||
CapabilityContext::Remote { domains } => domains
|
||||
CapabilityContext::Remote { urls } => urls
|
||||
.iter()
|
||||
.map(|domain| ExecutionContext::Remote {
|
||||
domain: Pattern::new(domain)
|
||||
.unwrap_or_else(|e| panic!("invalid glob pattern for remote domain {domain}: {e}")),
|
||||
.map(|url| ExecutionContext::Remote {
|
||||
url: Pattern::new(url)
|
||||
.unwrap_or_else(|e| panic!("invalid glob pattern for remote URL {url}: {e}")),
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
|
@ -35,8 +35,8 @@ pub enum Origin {
|
||||
Local,
|
||||
/// Remote origin.
|
||||
Remote {
|
||||
/// Remote origin domain.
|
||||
domain: String,
|
||||
/// Remote URL.
|
||||
url: String,
|
||||
},
|
||||
}
|
||||
|
||||
@ -44,7 +44,7 @@ impl Display for Origin {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Local => write!(f, "local"),
|
||||
Self::Remote { domain } => write!(f, "remote: {domain}"),
|
||||
Self::Remote { url } => write!(f, "remote: {url}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -53,12 +53,9 @@ impl Origin {
|
||||
fn matches(&self, context: &ExecutionContext) -> bool {
|
||||
match (self, context) {
|
||||
(Self::Local, ExecutionContext::Local) => true,
|
||||
(
|
||||
Self::Remote { domain },
|
||||
ExecutionContext::Remote {
|
||||
domain: domain_pattern,
|
||||
},
|
||||
) => domain_pattern.matches(domain),
|
||||
(Self::Remote { url }, ExecutionContext::Remote { url: url_pattern }) => {
|
||||
url_pattern.matches(url)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@ -292,7 +289,7 @@ impl RuntimeAuthority {
|
||||
.map(|(cmd, resolved)| {
|
||||
let context = match &cmd.context {
|
||||
ExecutionContext::Local => "[local]".to_string(),
|
||||
ExecutionContext::Remote { domain } => format!("[remote: {}]", domain.as_str()),
|
||||
ExecutionContext::Remote { url } => format!("[remote: {}]", url.as_str()),
|
||||
};
|
||||
format!(
|
||||
"- context: {context}, referenced by: {}",
|
||||
@ -634,11 +631,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn remote_domain_matches() {
|
||||
let domain = "tauri.app";
|
||||
let url = "https://tauri.app";
|
||||
let command = CommandKey {
|
||||
name: "my-command".into(),
|
||||
context: ExecutionContext::Remote {
|
||||
domain: Pattern::new(domain).unwrap(),
|
||||
url: Pattern::new(url).unwrap(),
|
||||
},
|
||||
};
|
||||
let window = "main";
|
||||
@ -666,9 +663,7 @@ mod tests {
|
||||
&command.name,
|
||||
window,
|
||||
webview,
|
||||
&Origin::Remote {
|
||||
domain: domain.into()
|
||||
}
|
||||
&Origin::Remote { url: url.into() }
|
||||
),
|
||||
Some(&resolved_cmd)
|
||||
);
|
||||
@ -676,11 +671,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn remote_domain_glob_pattern_matches() {
|
||||
let domain = "tauri.*";
|
||||
let url = "http://tauri.*";
|
||||
let command = CommandKey {
|
||||
name: "my-command".into(),
|
||||
context: ExecutionContext::Remote {
|
||||
domain: Pattern::new(domain).unwrap(),
|
||||
url: Pattern::new(url).unwrap(),
|
||||
},
|
||||
};
|
||||
let window = "main";
|
||||
@ -709,7 +704,7 @@ mod tests {
|
||||
window,
|
||||
webview,
|
||||
&Origin::Remote {
|
||||
domain: domain.replace('*', "studio")
|
||||
url: url.replace('*', "studio")
|
||||
}
|
||||
),
|
||||
Some(&resolved_cmd)
|
||||
@ -748,7 +743,7 @@ mod tests {
|
||||
window,
|
||||
webview,
|
||||
&Origin::Remote {
|
||||
domain: "tauri.app".into()
|
||||
url: "https://tauri.app".into()
|
||||
}
|
||||
)
|
||||
.is_none());
|
||||
|
@ -1113,10 +1113,7 @@ fn main() {
|
||||
Origin::Local
|
||||
} else {
|
||||
Origin::Remote {
|
||||
domain: current_url
|
||||
.domain()
|
||||
.map(|d| d.to_string())
|
||||
.unwrap_or_default(),
|
||||
url: current_url.to_string(),
|
||||
}
|
||||
};
|
||||
let resolved_acl = manager
|
||||
|
@ -3,4 +3,4 @@ description = "app capability"
|
||||
windows = ["main"]
|
||||
permissions = ["fs:read", "fs:allow-app"]
|
||||
[context.remote]
|
||||
domains = ["tauri.app"]
|
||||
urls = ["https://tauri.app"]
|
||||
|
@ -7,9 +7,33 @@ Resolved {
|
||||
CommandKey {
|
||||
name: "plugin:fs|read_dir",
|
||||
context: Remote {
|
||||
domain: Pattern {
|
||||
original: "tauri.app",
|
||||
url: Pattern {
|
||||
original: "https://tauri.app",
|
||||
tokens: [
|
||||
Char(
|
||||
'h',
|
||||
),
|
||||
Char(
|
||||
't',
|
||||
),
|
||||
Char(
|
||||
't',
|
||||
),
|
||||
Char(
|
||||
'p',
|
||||
),
|
||||
Char(
|
||||
's',
|
||||
),
|
||||
Char(
|
||||
':',
|
||||
),
|
||||
Char(
|
||||
'/',
|
||||
),
|
||||
Char(
|
||||
'/',
|
||||
),
|
||||
Char(
|
||||
't',
|
||||
),
|
||||
@ -68,9 +92,33 @@ Resolved {
|
||||
CommandKey {
|
||||
name: "plugin:fs|read_file",
|
||||
context: Remote {
|
||||
domain: Pattern {
|
||||
original: "tauri.app",
|
||||
url: Pattern {
|
||||
original: "https://tauri.app",
|
||||
tokens: [
|
||||
Char(
|
||||
'h',
|
||||
),
|
||||
Char(
|
||||
't',
|
||||
),
|
||||
Char(
|
||||
't',
|
||||
),
|
||||
Char(
|
||||
'p',
|
||||
),
|
||||
Char(
|
||||
's',
|
||||
),
|
||||
Char(
|
||||
':',
|
||||
),
|
||||
Char(
|
||||
'/',
|
||||
),
|
||||
Char(
|
||||
'/',
|
||||
),
|
||||
Char(
|
||||
't',
|
||||
),
|
||||
|
@ -1151,10 +1151,10 @@
|
||||
"remote": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"domains"
|
||||
"urls"
|
||||
],
|
||||
"properties": {
|
||||
"domains": {
|
||||
"urls": {
|
||||
"description": "Remote domains this capability refers to. Can use glob patterns.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
|
Loading…
Reference in New Issue
Block a user