Browse Source

feat(schema): make acl permissions schema unique (#9999)

* Make acl permissions schema unique

* Add change file

* use a hashset

* fix cli

* Revert "use a hashset"

This reverts commit 778d316f34c0ae0c66d3ab8be5123f2d4f07367e.

* Revert "fix cli"

This reverts commit cfc0e391822df29019b21230d66a7b6f73362263.

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Tony 1 year ago
parent
commit
878198777e

+ 5 - 0
.changes/permission-schema-unique-item.md

@@ -0,0 +1,5 @@
+---
+"tauri-utils": patch:enhance
+---
+
+Mark ACL `permissions` array with unique items

+ 2 - 1
core/tauri-acl-schema/capability-schema.json

@@ -52,7 +52,8 @@
       "type": "array",
       "items": {
         "$ref": "#/definitions/PermissionEntry"
-      }
+      },
+      "uniqueItems": true
     },
     "platforms": {
       "description": "Limit which target platforms this capability applies to.\n\n By default all platforms are targeted.\n\n ## Example\n\n `[\"macOS\",\"windows\"]`",

+ 2 - 1
core/tauri-config-schema/schema.json

@@ -1126,7 +1126,8 @@
           "type": "array",
           "items": {
             "$ref": "#/definitions/PermissionEntry"
-          }
+          },
+          "uniqueItems": true
         },
         "platforms": {
           "description": "Limit which target platforms this capability applies to.\n\n By default all platforms are targeted.\n\n ## Example\n\n `[\"macOS\",\"windows\"]`",

+ 16 - 0
core/tauri-utils/src/acl/capability.rs

@@ -157,6 +157,7 @@ pub struct Capability {
   ///    "allow": [{ "path": "$HOME/test.txt" }]
   ///  }
   /// ```
+  #[cfg_attr(feature = "schema", schemars(schema_with = "unique_permission"))]
   pub permissions: Vec<PermissionEntry>,
   /// Limit which target platforms this capability applies to.
   ///
@@ -169,6 +170,21 @@ pub struct Capability {
   pub platforms: Option<Vec<Target>>,
 }
 
+#[cfg(feature = "schema")]
+fn unique_permission(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
+  use schemars::schema;
+  schema::SchemaObject {
+    instance_type: Some(schema::InstanceType::Array.into()),
+    array: Some(Box::new(schema::ArrayValidation {
+      unique_items: Some(true),
+      items: Some(gen.subschema_for::<PermissionEntry>().into()),
+      ..Default::default()
+    })),
+    ..Default::default()
+  }
+  .into()
+}
+
 fn default_capability_local() -> bool {
   true
 }

+ 2 - 1
tooling/cli/schema.json

@@ -1126,7 +1126,8 @@
           "type": "array",
           "items": {
             "$ref": "#/definitions/PermissionEntry"
-          }
+          },
+          "uniqueItems": true
         },
         "platforms": {
           "description": "Limit which target platforms this capability applies to.\n\n By default all platforms are targeted.\n\n ## Example\n\n `[\"macOS\",\"windows\"]`",