refactor: configure URLs instead of domains on capability remote (#8898)

This commit is contained in:
Lucas Fernandes Nogueira 2024-02-19 11:14:09 -03:00 committed by GitHub
parent 8d16a80d2f
commit f284f9c545
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 91 additions and 45 deletions

View 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.

View File

@ -1151,10 +1151,10 @@
"remote": { "remote": {
"type": "object", "type": "object",
"required": [ "required": [
"domains" "urls"
], ],
"properties": { "properties": {
"domains": { "urls": {
"description": "Remote domains this capability refers to. Can use glob patterns.", "description": "Remote domains this capability refers to. Can use glob patterns.",
"type": "array", "type": "array",
"items": { "items": {

View File

@ -99,7 +99,7 @@ pub enum CapabilityContext {
/// Capability refers to remote usage. /// Capability refers to remote usage.
Remote { Remote {
/// Remote domains this capability refers to. Can use glob patterns. /// 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 }; let prefix = quote! { ::tauri::utils::acl::capability::CapabilityContext };
tokens.append_all(match self { tokens.append_all(match self {
Self::Remote { domains } => { Self::Remote { urls } => {
let domains = vec_lit(domains, str_lit); let urls = vec_lit(urls, str_lit);
quote! { #prefix::Remote { domains: #domains } } quote! { #prefix::Remote { urls: #urls } }
} }
Self::Local => { Self::Local => {
quote! { #prefix::Local } quote! { #prefix::Local }

View File

@ -189,8 +189,8 @@ pub enum ExecutionContext {
Local, Local,
/// Remote URL is tring to use the IPC. /// Remote URL is tring to use the IPC.
Remote { Remote {
/// The domain trying to access the IPC (glob pattern). /// The URL trying to access the IPC (glob pattern).
domain: Pattern, url: Pattern,
}, },
} }
@ -212,9 +212,9 @@ mod build_ {
Self::Local => { Self::Local => {
quote! { #prefix::Local } quote! { #prefix::Local }
} }
Self::Remote { domain } => { Self::Remote { url } => {
let domain = domain.as_str(); let url = url.as_str();
quote! { #prefix::Remote { domain: #domain.parse().unwrap() } } quote! { #prefix::Remote { url: #url.parse().unwrap() } }
} }
}); });
} }

View File

@ -349,11 +349,11 @@ fn resolve_command(
CapabilityContext::Local => { CapabilityContext::Local => {
vec![ExecutionContext::Local] vec![ExecutionContext::Local]
} }
CapabilityContext::Remote { domains } => domains CapabilityContext::Remote { urls } => urls
.iter() .iter()
.map(|domain| ExecutionContext::Remote { .map(|url| ExecutionContext::Remote {
domain: Pattern::new(domain) url: Pattern::new(url)
.unwrap_or_else(|e| panic!("invalid glob pattern for remote domain {domain}: {e}")), .unwrap_or_else(|e| panic!("invalid glob pattern for remote URL {url}: {e}")),
}) })
.collect(), .collect(),
}; };

View File

@ -35,8 +35,8 @@ pub enum Origin {
Local, Local,
/// Remote origin. /// Remote origin.
Remote { Remote {
/// Remote origin domain. /// Remote URL.
domain: String, url: String,
}, },
} }
@ -44,7 +44,7 @@ impl Display for Origin {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::Local => write!(f, "local"), 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 { fn matches(&self, context: &ExecutionContext) -> bool {
match (self, context) { match (self, context) {
(Self::Local, ExecutionContext::Local) => true, (Self::Local, ExecutionContext::Local) => true,
( (Self::Remote { url }, ExecutionContext::Remote { url: url_pattern }) => {
Self::Remote { domain }, url_pattern.matches(url)
ExecutionContext::Remote { }
domain: domain_pattern,
},
) => domain_pattern.matches(domain),
_ => false, _ => false,
} }
} }
@ -292,7 +289,7 @@ impl RuntimeAuthority {
.map(|(cmd, resolved)| { .map(|(cmd, resolved)| {
let context = match &cmd.context { let context = match &cmd.context {
ExecutionContext::Local => "[local]".to_string(), ExecutionContext::Local => "[local]".to_string(),
ExecutionContext::Remote { domain } => format!("[remote: {}]", domain.as_str()), ExecutionContext::Remote { url } => format!("[remote: {}]", url.as_str()),
}; };
format!( format!(
"- context: {context}, referenced by: {}", "- context: {context}, referenced by: {}",
@ -634,11 +631,11 @@ mod tests {
#[test] #[test]
fn remote_domain_matches() { fn remote_domain_matches() {
let domain = "tauri.app"; let url = "https://tauri.app";
let command = CommandKey { let command = CommandKey {
name: "my-command".into(), name: "my-command".into(),
context: ExecutionContext::Remote { context: ExecutionContext::Remote {
domain: Pattern::new(domain).unwrap(), url: Pattern::new(url).unwrap(),
}, },
}; };
let window = "main"; let window = "main";
@ -666,9 +663,7 @@ mod tests {
&command.name, &command.name,
window, window,
webview, webview,
&Origin::Remote { &Origin::Remote { url: url.into() }
domain: domain.into()
}
), ),
Some(&resolved_cmd) Some(&resolved_cmd)
); );
@ -676,11 +671,11 @@ mod tests {
#[test] #[test]
fn remote_domain_glob_pattern_matches() { fn remote_domain_glob_pattern_matches() {
let domain = "tauri.*"; let url = "http://tauri.*";
let command = CommandKey { let command = CommandKey {
name: "my-command".into(), name: "my-command".into(),
context: ExecutionContext::Remote { context: ExecutionContext::Remote {
domain: Pattern::new(domain).unwrap(), url: Pattern::new(url).unwrap(),
}, },
}; };
let window = "main"; let window = "main";
@ -709,7 +704,7 @@ mod tests {
window, window,
webview, webview,
&Origin::Remote { &Origin::Remote {
domain: domain.replace('*', "studio") url: url.replace('*', "studio")
} }
), ),
Some(&resolved_cmd) Some(&resolved_cmd)
@ -748,7 +743,7 @@ mod tests {
window, window,
webview, webview,
&Origin::Remote { &Origin::Remote {
domain: "tauri.app".into() url: "https://tauri.app".into()
} }
) )
.is_none()); .is_none());

View File

@ -1113,10 +1113,7 @@ fn main() {
Origin::Local Origin::Local
} else { } else {
Origin::Remote { Origin::Remote {
domain: current_url url: current_url.to_string(),
.domain()
.map(|d| d.to_string())
.unwrap_or_default(),
} }
}; };
let resolved_acl = manager let resolved_acl = manager

View File

@ -3,4 +3,4 @@ description = "app capability"
windows = ["main"] windows = ["main"]
permissions = ["fs:read", "fs:allow-app"] permissions = ["fs:read", "fs:allow-app"]
[context.remote] [context.remote]
domains = ["tauri.app"] urls = ["https://tauri.app"]

View File

@ -7,9 +7,33 @@ Resolved {
CommandKey { CommandKey {
name: "plugin:fs|read_dir", name: "plugin:fs|read_dir",
context: Remote { context: Remote {
domain: Pattern { url: Pattern {
original: "tauri.app", original: "https://tauri.app",
tokens: [ tokens: [
Char(
'h',
),
Char(
't',
),
Char(
't',
),
Char(
'p',
),
Char(
's',
),
Char(
':',
),
Char(
'/',
),
Char(
'/',
),
Char( Char(
't', 't',
), ),
@ -68,9 +92,33 @@ Resolved {
CommandKey { CommandKey {
name: "plugin:fs|read_file", name: "plugin:fs|read_file",
context: Remote { context: Remote {
domain: Pattern { url: Pattern {
original: "tauri.app", original: "https://tauri.app",
tokens: [ tokens: [
Char(
'h',
),
Char(
't',
),
Char(
't',
),
Char(
'p',
),
Char(
's',
),
Char(
':',
),
Char(
'/',
),
Char(
'/',
),
Char( Char(
't', 't',
), ),

View File

@ -1151,10 +1151,10 @@
"remote": { "remote": {
"type": "object", "type": "object",
"required": [ "required": [
"domains" "urls"
], ],
"properties": { "properties": {
"domains": { "urls": {
"description": "Remote domains this capability refers to. Can use glob patterns.", "description": "Remote domains this capability refers to. Can use glob patterns.",
"type": "array", "type": "array",
"items": { "items": {