Skip to content
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
5 changes: 5 additions & 0 deletions .changes/fix-capability-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-utils": patch:bug
---

Fix capability filtering via `tauri.conf.json > app > security > capabilities` not working when generating allowed commands.
2 changes: 1 addition & 1 deletion crates/tauri-build/src/acl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ pub fn build(out_dir: &Path, target: Target, attributes: &Attributes) -> super::
permissions_map.insert(APP_ACL_KEY.to_string(), app_acl.permission_files);
}

tauri_utils::acl::build::generate_allowed_commands(out_dir, permissions_map)?;
tauri_utils::acl::build::generate_allowed_commands(out_dir, Some(capabilities), permissions_map)?;

Ok(())
}
2 changes: 2 additions & 0 deletions crates/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ impl Attributes {

/// Set the glob pattern to be used to find the capabilities.
///
/// **WARNING:** The `removeUnusedCommands` option does not work with a custom capabilities path.
///
/// **Note:** You must emit [rerun-if-changed] instructions for your capabilities directory.
///
/// [rerun-if-changed]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed
Expand Down
9 changes: 8 additions & 1 deletion crates/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,16 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
};

let capabilities_file_path = out_dir.join(CAPABILITIES_FILE_NAME);
let capabilities_from_files = if capabilities_file_path.exists() {
let capabilities_json =
std::fs::read_to_string(&capabilities_file_path).expect("failed to read capabilities");
serde_json::from_str(&capabilities_json).expect("failed to parse capabilities")
} else {
Default::default()
};
let capabilities = get_capabilities(
&config,
Some(&capabilities_file_path),
capabilities_from_files,
additional_capabilities.as_deref(),
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-plugin/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<'a> Builder<'a> {

let mut permissions_map = BTreeMap::new();
permissions_map.insert(name.clone(), permissions);
tauri_utils::acl::build::generate_allowed_commands(&out_dir, permissions_map)?;
tauri_utils::acl::build::generate_allowed_commands(&out_dir, None, permissions_map)?;

if let Some(global_scope_schema) = self.global_scope_schema {
acl::build::define_global_scope_schema(global_scope_schema, &name, &out_dir)?;
Expand Down
16 changes: 10 additions & 6 deletions crates/tauri-utils/src/acl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ pub fn generate_docs(
/// Generate allowed commands file for the `generate_handler` macro to remove never allowed commands
pub fn generate_allowed_commands(
out_dir: &Path,
capabilities_from_files: Option<BTreeMap<String, Capability>>,
permissions_map: BTreeMap<String, Vec<PermissionFile>>,
) -> Result<(), anyhow::Error> {
println!("cargo:rerun-if-env-changed={REMOVE_UNUSED_COMMANDS_ENV_VAR}");
Expand All @@ -419,11 +420,6 @@ pub fn generate_allowed_commands(
println!("cargo:rerun-if-changed={}", capabilities_path.display());
}

let mut capabilities = crate::acl::build::parse_capabilities(&format!(
"{}/**/*",
glob::Pattern::escape(&capabilities_path.to_string_lossy())
))?;

let target_triple = env::var("TARGET")?;
let target = crate::platform::Target::from_triple(&target_triple);
let (mut config, config_paths) = crate::config::parse::read_from(target, &config_directory)?;
Expand Down Expand Up @@ -460,7 +456,15 @@ pub fn generate_allowed_commands(
})
.collect();

capabilities.extend(crate::acl::get_capabilities(&config, None, None)?);
let capabilities_from_files = if let Some(capabilities) = capabilities_from_files {
capabilities
} else {
crate::acl::build::parse_capabilities(&format!(
"{}/**/*",
glob::Pattern::escape(&capabilities_path.to_string_lossy())
))?
};
let capabilities = crate::acl::get_capabilities(&config, capabilities_from_files, None)?;

let permission_entries = capabilities
.into_iter()
Expand Down
14 changes: 2 additions & 12 deletions crates/tauri-utils/src/acl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::{
collections::{BTreeMap, HashSet},
fs,
num::NonZeroU64,
path::{Path, PathBuf},
path::PathBuf,
str::FromStr,
sync::Arc,
};
Expand Down Expand Up @@ -352,19 +352,9 @@ pub fn has_app_manifest(acl: &BTreeMap<String, crate::acl::manifest::Manifest>)
/// Get the capabilities from the config file
pub fn get_capabilities(
config: &Config,
pre_built_capabilities_file_path: Option<&Path>,
mut capabilities_from_files: BTreeMap<String, Capability>,
additional_capability_files: Option<&[PathBuf]>,
) -> anyhow::Result<BTreeMap<String, Capability>> {
let mut capabilities_from_files: BTreeMap<String, Capability> = BTreeMap::new();
if let Some(capabilities_file_path) = pre_built_capabilities_file_path {
if capabilities_file_path.exists() {
let capabilities_file =
std::fs::read_to_string(capabilities_file_path).context("failed to read capabilities")?;
capabilities_from_files =
serde_json::from_str(&capabilities_file).context("failed to parse capabilities")?;
}
}

let mut capabilities = if config.app.security.capabilities.is_empty() {
capabilities_from_files
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ fn main() {
}

let permissions = define_permissions(&out_dir);
tauri_utils::acl::build::generate_allowed_commands(&out_dir, permissions).unwrap();
tauri_utils::acl::build::generate_allowed_commands(&out_dir, None, permissions).unwrap();
}

const LICENSE_HEADER: &str = r"# Copyright 2019-2024 Tauri Programme within The Commons Conservancy
Expand Down