Skip to content

Commit d7f56fe

Browse files
authored
feat(acl): allow a permission to apply to a subset of target platforms (#9014)
* feat(acl): allow a permission to apply to a subset of target platforms * fix cli
1 parent d7d03c7 commit d7f56fe

File tree

20 files changed

+358
-11
lines changed

20 files changed

+358
-11
lines changed

.changes/permission-platforms.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch:feat
3+
"tauri-utils": patch:feat
4+
---
5+
6+
Allow defining a permission that only applies to a set of target platforms via the `platforms` configuration option.

core/tauri-config-schema/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@
11231123
}
11241124
},
11251125
"platforms": {
1126-
"description": "Target platforms this capability applies. By default all platforms applies.",
1126+
"description": "Target platforms this capability applies. By default all platforms are affected by this capability.",
11271127
"default": [
11281128
"linux",
11291129
"macOS",

core/tauri-utils/src/acl/capability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct Capability {
7474
pub webviews: Vec<String>,
7575
/// List of permissions attached to this capability. Must include the plugin name as prefix in the form of `${plugin-name}:${permission-name}`.
7676
pub permissions: Vec<PermissionEntry>,
77-
/// Target platforms this capability applies. By default all platforms applies.
77+
/// Target platforms this capability applies. By default all platforms are affected by this capability.
7878
#[serde(default = "default_platforms", skip_serializing_if = "Vec::is_empty")]
7979
pub platforms: Vec<Target>,
8080
}

core/tauri-utils/src/acl/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use serde::{Deserialize, Serialize};
99
use std::num::NonZeroU64;
1010
use thiserror::Error;
1111

12+
use crate::platform::Target;
13+
1214
pub use self::{identifier::*, value::*};
1315

1416
/// Known filename of the permission schema JSON file
@@ -172,6 +174,20 @@ pub struct Permission {
172174
/// Allowed or denied scoped when using this permission.
173175
#[serde(default, skip_serializing_if = "Scopes::is_empty")]
174176
pub scope: Scopes,
177+
178+
/// Target platforms this permission applies. By default all platforms are affected by this permission.
179+
#[serde(default = "default_platforms", skip_serializing_if = "Vec::is_empty")]
180+
pub platforms: Vec<Target>,
181+
}
182+
183+
fn default_platforms() -> Vec<Target> {
184+
vec![
185+
Target::Linux,
186+
Target::MacOS,
187+
Target::Windows,
188+
Target::Android,
189+
Target::Ios,
190+
]
175191
}
176192

177193
/// A set of direct permissions grouped together under a new name.
@@ -252,14 +268,17 @@ mod build_ {
252268
let description = opt_str_lit(self.description.as_ref());
253269
let commands = &self.commands;
254270
let scope = &self.scope;
271+
let platforms = vec_lit(&self.platforms, identity);
272+
255273
literal_struct!(
256274
tokens,
257275
::tauri::utils::acl::Permission,
258276
version,
259277
identifier,
260278
description,
261279
commands,
262-
scope
280+
scope,
281+
platforms
263282
)
264283
}
265284
}

core/tauri-utils/src/acl/resolved.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ impl Resolved {
112112
with_resolved_permissions(
113113
capability,
114114
acl,
115+
target,
115116
|ResolvedPermission {
116117
key,
117118
permission_name,
@@ -273,6 +274,7 @@ struct ResolvedPermission<'a> {
273274
fn with_resolved_permissions<F: FnMut(ResolvedPermission<'_>)>(
274275
capability: &Capability,
275276
acl: &BTreeMap<String, Manifest>,
277+
target: Target,
276278
mut f: F,
277279
) -> Result<(), Error> {
278280
for permission_entry in &capability.permissions {
@@ -281,7 +283,10 @@ fn with_resolved_permissions<F: FnMut(ResolvedPermission<'_>)>(
281283

282284
let key = permission_id.get_prefix().unwrap_or(APP_ACL_KEY);
283285

284-
let permissions = get_permissions(key, permission_name, acl)?;
286+
let permissions = get_permissions(key, permission_name, acl)?
287+
.into_iter()
288+
.filter(|p| p.platforms.contains(&target))
289+
.collect::<Vec<_>>();
285290

286291
let mut resolved_scope = Scopes::default();
287292
let mut commands = Commands::default();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
identifier = "run-app"
2+
description = "app capability"
3+
windows = ["main"]
4+
permissions = [
5+
"os:allow-apt-linux",
6+
"os:allow-library-folder-macos",
7+
"os:deny-webview-folder-windows",
8+
"os:open-browser",
9+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["os"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[[permission]]
2+
identifier = "allow-apt-linux"
3+
platforms = ["linux"]
4+
description = "Allows spawning the apt command on Linux"
5+
commands.allow = ["spawn"]
6+
[[permission.scope.allow]]
7+
command = "apt"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
[[permission]]
3+
identifier = "allow-library-folder-macos"
4+
platforms = ["macOS"]
5+
description = "Allows access to the $HOME/Library folder on maOS"
6+
[[permission.scope.allow]]
7+
path = "$HOME/Library/**"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[[permission]]
2+
identifier = "allow-servo-linux"
3+
platforms = ["linux"]
4+
description = "Allows starting servo on Linux"
5+
commands.allow = ["spawn"]
6+
[[permission.scope.allow]]
7+
command = "servo"
8+
9+
[[permission]]
10+
identifier = "allow-edge-windows"
11+
platforms = ["windows"]
12+
description = "Allows starting edge on Windows"
13+
commands.allow = ["spawn"]
14+
[[permission.scope.allow]]
15+
command = "edge"
16+
17+
[[permission]]
18+
identifier = "allow-safari-macos"
19+
platforms = ["macOS"]
20+
description = "Allows starting safari on macOS"
21+
commands.allow = ["spawn"]
22+
[[permission.scope.allow]]
23+
command = "safari"
24+
25+
[[set]]
26+
identifier = "open-browser"
27+
description = "allows opening a URL on the platform browser"
28+
permissions = ["allow-servo-linux", "allow-edge-windows", "allow-safari-macos"]

0 commit comments

Comments
 (0)