瀏覽代碼

feat(core): validate duplicated capability identifier (#10858)

having duplicate capability identifier lead to unexpected behavior because one of the capabilities gets ignored.
With this change the build script now fails when this happens.
Lucas Fernandes Nogueira 11 月之前
父節點
當前提交
f0acf504a2
共有 3 個文件被更改,包括 22 次插入0 次删除
  1. 5 0
      .changes/capability-id-already-exists.md
  2. 10 0
      crates/tauri-utils/src/acl/build.rs
  3. 7 0
      crates/tauri-utils/src/acl/mod.rs

+ 5 - 0
.changes/capability-id-already-exists.md

@@ -0,0 +1,5 @@
+---
+"tauri-utils": patch:enhance
+---
+
+Validate duplicate capability identifier.

+ 10 - 0
crates/tauri-utils/src/acl/build.rs

@@ -135,10 +135,20 @@ pub fn parse_capabilities(
   {
     match CapabilityFile::load(&path)? {
       CapabilityFile::Capability(capability) => {
+        if capabilities_map.contains_key(&capability.identifier) {
+          return Err(Error::CapabilityAlreadyExists {
+            identifier: capability.identifier,
+          });
+        }
         capabilities_map.insert(capability.identifier.clone(), capability);
       }
       CapabilityFile::List(capabilities) | CapabilityFile::NamedList { capabilities } => {
         for capability in capabilities {
+          if capabilities_map.contains_key(&capability.identifier) {
+            return Err(Error::CapabilityAlreadyExists {
+              identifier: capability.identifier,
+            });
+          }
           capabilities_map.insert(capability.identifier.clone(), capability);
         }
       }

+ 7 - 0
crates/tauri-utils/src/acl/mod.rs

@@ -126,6 +126,13 @@ pub enum Error {
     /// Permission identifier.
     permission: String,
   },
+
+  /// Capability with the given identifier already exists.
+  #[error("capability with identifier `{identifier}` already exists")]
+  CapabilityAlreadyExists {
+    /// Capability identifier.
+    identifier: String,
+  },
 }
 
 /// Allowed and denied commands inside a permission.