Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -97,7 +97,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 All @@ -116,9 +116,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 @@ -337,11 +337,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 @@ -34,16 +34,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 @@ -52,12 +52,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 @@ -204,7 +201,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 @@ -540,11 +537,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 All @@ -569,21 +566,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 All @@ -609,7 +604,7 @@ mod tests {
window,
webview,
&Origin::Remote {
domain: domain.replace('*', "studio")
url: url.replace('*', "studio")
}
),
Some(&resolved_cmd)
Expand Down Expand Up @@ -645,7 +640,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
Loading