Skip to content

Commit a4099d8

Browse files
authored
fix(cli): never overwrite an existing capability in permission add (#16098)
1 parent fdd56da commit a4099d8

2 files changed

Lines changed: 98 additions & 10 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
`tauri permission add` (and `tauri add`) no longer overwrites an existing `capabilities/desktop.json` or `capabilities/mobile.json` when adding a desktop-only or mobile-only plugin permission and no platform-restricted capability exists; the new capability now gets an unused file name and identifier (e.g. `desktop-2.json`). An explicitly passed capability is now used even if its platforms do not match the plugin, with a warning, instead of being silently ignored.

‎crates/tauri-cli/src/acl/permission/add.rs‎

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use std::path::Path;
5+
use std::path::{Path, PathBuf};
66

77
use clap::Parser;
88

@@ -122,6 +122,31 @@ fn capability_from_path<P: AsRef<Path>>(path: P) -> Option<TomlOrJson> {
122122
}
123123
}
124124

125+
/// Finds an identifier and a file path for a new `target_name` capability
126+
/// that do not collide with an existing capability, so no user file is overwritten.
127+
fn new_capability_name(
128+
capabilities_dir: &Path,
129+
target_name: &str,
130+
existing_identifiers: &[&str],
131+
) -> (String, PathBuf) {
132+
(1..)
133+
.map(|n| {
134+
let suffix = if n == 1 {
135+
String::new()
136+
} else {
137+
format!("-{n}")
138+
};
139+
(
140+
format!("{target_name}-capability{suffix}"),
141+
capabilities_dir.join(format!("{target_name}{suffix}.json")),
142+
)
143+
})
144+
.find(|(identifier, path)| {
145+
!path.exists() && !existing_identifiers.contains(&identifier.as_str())
146+
})
147+
.expect("unbounded iterator always finds a name")
148+
}
149+
125150
#[derive(Debug, Parser)]
126151
#[clap(about = "Add a permission to capabilities")]
127152
pub struct Options {
@@ -151,7 +176,7 @@ pub fn command(options: Options) -> Result<()> {
151176
.split_once(':')
152177
.and_then(|(plugin, _permission)| known_plugins.get(&plugin));
153178

154-
let capabilities_iter = std::fs::read_dir(&capabilities_dir)
179+
let all_capabilities = std::fs::read_dir(&capabilities_dir)
155180
.fs_context(
156181
"failed to read capabilities directory",
157182
capabilities_dir.clone(),
@@ -160,11 +185,17 @@ pub fn command(options: Options) -> Result<()> {
160185
.filter(|e| e.file_type().map(|e| e.is_file()).unwrap_or_default())
161186
.filter_map(|e| {
162187
let path = e.path();
163-
capability_from_path(&path).and_then(|capability| match &options.capability {
164-
Some(c) => (c == capability.identifier()).then_some((capability, path)),
165-
None => Some((capability, path)),
166-
})
167-
});
188+
capability_from_path(&path).map(|capability| (capability, path))
189+
})
190+
.collect::<Vec<_>>();
191+
192+
let capabilities_iter = all_capabilities
193+
.iter()
194+
.filter(|(capability, _path)| match &options.capability {
195+
Some(c) => c == capability.identifier(),
196+
None => true,
197+
})
198+
.cloned();
168199

169200
let (desktop_only, mobile_only) = known_plugin
170201
.map(|p| (p.desktop_only, p.mobile_only))
@@ -191,7 +222,30 @@ pub fn command(options: Options) -> Result<()> {
191222
None
192223
};
193224

194-
let capabilities = if let Some((expected_platforms, target_name)) = expected_capability_config {
225+
let capabilities = if let Some(requested) = &options.capability {
226+
// the user asked for this capability explicitly, so use it even if its platforms do not match
227+
let capabilities = capabilities_iter.collect::<Vec<_>>();
228+
if capabilities.is_empty() {
229+
crate::error::bail!("Could not find capability `{}`", requested);
230+
}
231+
if let Some((expected_platforms, _target_name)) = &expected_capability_config {
232+
for (capability, path) in &capabilities {
233+
let platforms_match = capability.platforms().is_some_and(|platforms| {
234+
platforms
235+
.iter()
236+
.all(|p| expected_platforms.contains(&p.to_string()))
237+
});
238+
if !platforms_match {
239+
log::warn!(
240+
"Capability `{requested}` at {} is not restricted to the platforms {expected_platforms:?} that `{}` supports",
241+
dunce::simplified(path).display(),
242+
options.identifier
243+
);
244+
}
245+
}
246+
}
247+
capabilities
248+
} else if let Some((expected_platforms, target_name)) = expected_capability_config {
195249
let mut capabilities = capabilities_iter
196250
.filter(|(capability, _path)| {
197251
capability.platforms().is_some_and(|platforms| {
@@ -204,8 +258,12 @@ pub fn command(options: Options) -> Result<()> {
204258
.collect::<Vec<_>>();
205259

206260
if capabilities.is_empty() {
207-
let identifier = format!("{target_name}-capability");
208-
let capability_path = capabilities_dir.join(target_name).with_extension("json");
261+
let existing_identifiers = all_capabilities
262+
.iter()
263+
.map(|(capability, _path)| capability.identifier())
264+
.collect::<Vec<_>>();
265+
let (identifier, capability_path) =
266+
new_capability_name(&capabilities_dir, target_name, &existing_identifiers);
209267
log::info!(
210268
"Capability matching platforms {expected_platforms:?} not found, creating {}",
211269
capability_path.display()
@@ -280,3 +338,27 @@ pub fn command(options: Options) -> Result<()> {
280338

281339
Ok(())
282340
}
341+
342+
#[cfg(test)]
343+
mod tests {
344+
use super::new_capability_name;
345+
346+
#[test]
347+
fn new_capability_never_overwrites_an_existing_file() {
348+
let dir = tempfile::tempdir().unwrap();
349+
350+
let (identifier, path) = new_capability_name(dir.path(), "desktop", &[]);
351+
assert_eq!(identifier, "desktop-capability");
352+
assert_eq!(path, dir.path().join("desktop.json"));
353+
354+
std::fs::write(dir.path().join("desktop.json"), "{}").unwrap();
355+
let (identifier, path) = new_capability_name(dir.path(), "desktop", &[]);
356+
assert_eq!(identifier, "desktop-capability-2");
357+
assert_eq!(path, dir.path().join("desktop-2.json"));
358+
359+
// an identifier used by another file is skipped too
360+
let (identifier, path) = new_capability_name(dir.path(), "desktop", &["desktop-capability-2"]);
361+
assert_eq!(identifier, "desktop-capability-3");
362+
assert_eq!(path, dir.path().join("desktop-3.json"));
363+
}
364+
}

0 commit comments

Comments
 (0)