Skip to content

Commit

Permalink
refactor: configure URLs instead of domains on capability remote (#8898)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Feb 19, 2024
1 parent 8d16a80 commit f284f9c
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 45 deletions.
6 changes: 6 additions & 0 deletions .changes/refactor-capability-remote-option.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
8 changes: 4 additions & 4 deletions core/tauri-utils/src/acl/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
},
}

Expand Down Expand Up @@ -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 }
Expand Down
10 changes: 5 additions & 5 deletions core/tauri-utils/src/acl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand All @@ -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() } }
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions core/tauri-utils/src/acl/resolved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
Expand Down
33 changes: 14 additions & 19 deletions core/tauri/src/ipc/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ pub enum Origin {
Local,
/// Remote origin.
Remote {
/// Remote origin domain.
domain: String,
/// Remote URL.
url: String,
},
}

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}"),
}
}
}
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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: {}",
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -666,21 +663,19 @@ mod tests {
&command.name,
window,
webview,
&Origin::Remote {
domain: domain.into()
}
&Origin::Remote { url: url.into() }
),
Some(&resolved_cmd)
);
}

#[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";
Expand Down Expand Up @@ -709,7 +704,7 @@ mod tests {
window,
webview,
&Origin::Remote {
domain: domain.replace('*', "studio")
url: url.replace('*', "studio")
}
),
Some(&resolved_cmd)
Expand Down Expand Up @@ -748,7 +743,7 @@ mod tests {
window,
webview,
&Origin::Remote {
domain: "tauri.app".into()
url: "https://tauri.app".into()
}
)
.is_none());
Expand Down
5 changes: 1 addition & 4 deletions core/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ description = "app capability"
windows = ["main"]
permissions = ["fs:read", "fs:allow-app"]
[context.remote]
domains = ["tauri.app"]
urls = ["https://tauri.app"]
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
Expand Down Expand Up @@ -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',
),
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit f284f9c

Please sign in to comment.