From f284f9c545deeb77d15b6e8b1d0d05f49c40634c Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Mon, 19 Feb 2024 11:14:09 -0300 Subject: [PATCH] refactor: configure URLs instead of domains on capability remote (#8898) --- .changes/refactor-capability-remote-option.md | 6 ++ core/tauri-config-schema/schema.json | 4 +- core/tauri-utils/src/acl/capability.rs | 8 +-- core/tauri-utils/src/acl/mod.rs | 10 ++-- core/tauri-utils/src/acl/resolved.rs | 8 +-- core/tauri/src/ipc/authority.rs | 33 +++++------ core/tauri/src/webview/mod.rs | 5 +- .../file-explorer-remote/cap.toml | 2 +- ...cl_tests__tests__file-explorer-remote.snap | 56 +++++++++++++++++-- tooling/cli/schema.json | 4 +- 10 files changed, 91 insertions(+), 45 deletions(-) create mode 100644 .changes/refactor-capability-remote-option.md diff --git a/.changes/refactor-capability-remote-option.md b/.changes/refactor-capability-remote-option.md new file mode 100644 index 000000000000..2572cc10a39d --- /dev/null +++ b/.changes/refactor-capability-remote-option.md @@ -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. diff --git a/core/tauri-config-schema/schema.json b/core/tauri-config-schema/schema.json index 3df59bd8eca9..591a6a77fea0 100644 --- a/core/tauri-config-schema/schema.json +++ b/core/tauri-config-schema/schema.json @@ -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": { diff --git a/core/tauri-utils/src/acl/capability.rs b/core/tauri-utils/src/acl/capability.rs index d0f192638c45..48f62b4a68e4 100644 --- a/core/tauri-utils/src/acl/capability.rs +++ b/core/tauri-utils/src/acl/capability.rs @@ -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, + urls: Vec, }, } @@ -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 } diff --git a/core/tauri-utils/src/acl/mod.rs b/core/tauri-utils/src/acl/mod.rs index 80ad2b64edaf..e354ef9eaebe 100644 --- a/core/tauri-utils/src/acl/mod.rs +++ b/core/tauri-utils/src/acl/mod.rs @@ -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() } } } }); } diff --git a/core/tauri-utils/src/acl/resolved.rs b/core/tauri-utils/src/acl/resolved.rs index 0a4bd8c2e41c..05b8f8185f03 100644 --- a/core/tauri-utils/src/acl/resolved.rs +++ b/core/tauri-utils/src/acl/resolved.rs @@ -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(), }; diff --git a/core/tauri/src/ipc/authority.rs b/core/tauri/src/ipc/authority.rs index 653f5173df9e..dc36e7fcb50c 100644 --- a/core/tauri/src/ipc/authority.rs +++ b/core/tauri/src/ipc/authority.rs @@ -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()); diff --git a/core/tauri/src/webview/mod.rs b/core/tauri/src/webview/mod.rs index 0b86035cb256..412fd983089b 100644 --- a/core/tauri/src/webview/mod.rs +++ b/core/tauri/src/webview/mod.rs @@ -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 diff --git a/core/tests/acl/fixtures/capabilities/file-explorer-remote/cap.toml b/core/tests/acl/fixtures/capabilities/file-explorer-remote/cap.toml index ebc8353d3d1f..b027d6e0aa00 100644 --- a/core/tests/acl/fixtures/capabilities/file-explorer-remote/cap.toml +++ b/core/tests/acl/fixtures/capabilities/file-explorer-remote/cap.toml @@ -3,4 +3,4 @@ description = "app capability" windows = ["main"] permissions = ["fs:read", "fs:allow-app"] [context.remote] -domains = ["tauri.app"] +urls = ["https://tauri.app"] diff --git a/core/tests/acl/fixtures/snapshots/acl_tests__tests__file-explorer-remote.snap b/core/tests/acl/fixtures/snapshots/acl_tests__tests__file-explorer-remote.snap index f28578025e9d..255da73751c5 100644 --- a/core/tests/acl/fixtures/snapshots/acl_tests__tests__file-explorer-remote.snap +++ b/core/tests/acl/fixtures/snapshots/acl_tests__tests__file-explorer-remote.snap @@ -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', ), diff --git a/tooling/cli/schema.json b/tooling/cli/schema.json index 3df59bd8eca9..591a6a77fea0 100644 --- a/tooling/cli/schema.json +++ b/tooling/cli/schema.json @@ -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": {