@@ -14,6 +14,30 @@ use crate::{
1414 helpers:: app_paths:: resolve_tauri_dir,
1515} ;
1616
17+ /// Parses a permission file, returning `None` if it is not a TOML or JSON file.
18+ fn parse_permission_file ( path : & Path ) -> Result < Option < ( PermissionFile , FileFormat ) > > {
19+ let parsed = match path. extension ( ) . and_then ( |o| o. to_str ( ) ) {
20+ Some ( "toml" ) => {
21+ let content = std:: fs:: read_to_string ( path)
22+ . fs_context ( "failed to read permission file" , path. to_path_buf ( ) ) ?;
23+ (
24+ toml:: from_str ( & content) . context ( "failed to deserialize permission file" ) ?,
25+ FileFormat :: Toml ,
26+ )
27+ }
28+ Some ( "json" ) => {
29+ let content =
30+ std:: fs:: read ( path) . fs_context ( "failed to read permission file" , path. to_path_buf ( ) ) ?;
31+ (
32+ serde_json:: from_slice ( & content) . context ( "failed to parse permission file as JSON" ) ?,
33+ FileFormat :: Json ,
34+ )
35+ }
36+ _ => return Ok ( None ) ,
37+ } ;
38+ Ok ( Some ( parsed) )
39+ }
40+
1741fn rm_permission_files ( identifier : & str , dir : & Path ) -> Result < ( ) > {
1842 for entry in std:: fs:: read_dir ( dir)
1943 . fs_context ( "failed to read permissions directory" , dir. to_path_buf ( ) ) ?
@@ -34,29 +58,17 @@ fn rm_permission_files(identifier: &str, dir: &Path) -> Result<()> {
3458 continue ;
3559 }
3660
37- let ( mut permission_file, format) : ( PermissionFile , FileFormat ) =
38- match path. extension ( ) . and_then ( |o| o. to_str ( ) ) {
39- Some ( "toml" ) => {
40- let content = std:: fs:: read_to_string ( & path)
41- . fs_context ( "failed to read permission file" , path. clone ( ) ) ?;
42- (
43- toml:: from_str ( & content) . context ( "failed to deserialize permission file" ) ?,
44- FileFormat :: Toml ,
45- )
46- }
47- Some ( "json" ) => {
48- let content =
49- std:: fs:: read ( & path) . fs_context ( "failed to read permission file" , path. clone ( ) ) ?;
50- (
51- serde_json:: from_slice ( & content)
52- . context ( "failed to parse permission file as JSON" ) ?,
53- FileFormat :: Json ,
54- )
55- }
56- _ => {
57- continue ;
58- }
59- } ;
61+ let ( mut permission_file, format) = match parse_permission_file ( & path) {
62+ Ok ( Some ( parsed) ) => parsed,
63+ Ok ( None ) => continue ,
64+ Err ( e) => {
65+ log:: warn!(
66+ "Skipping permission file {}: {e}" ,
67+ dunce:: simplified( & path) . display( )
68+ ) ;
69+ continue ;
70+ }
71+ } ;
6072
6173 let mut updated;
6274
@@ -175,7 +187,9 @@ fn rm_permission_from_capabilities(identifier: &str, dir: &Path) -> Result<()> {
175187
176188fn identifier_match ( identifier : & str , permission : & str ) -> bool {
177189 match identifier. split_once ( ':' ) {
178- Some ( ( plugin_name, "*" ) ) => permission. contains ( plugin_name) ,
190+ Some ( ( plugin_name, "*" ) ) => {
191+ permission == plugin_name || permission. starts_with ( & format ! ( "{plugin_name}:" ) )
192+ }
179193 _ => permission == identifier,
180194 }
181195}
@@ -190,14 +204,23 @@ pub struct Options {
190204}
191205
192206pub fn command ( options : Options ) -> Result < ( ) > {
193- let permissions_dir = std:: env:: current_dir ( )
194- . context ( "failed to resolve current directory" ) ?
195- . join ( "permissions" ) ;
196- if permissions_dir. exists ( ) {
197- rm_permission_files ( & options. identifier , & permissions_dir) ?;
207+ let tauri_dir = resolve_tauri_dir ( ) ;
208+
209+ // app-local permission identifiers are not plugin-prefixed,
210+ // so the `<plugin-name>:*` form never matches them
211+ if !is_plugin_wildcard ( & options. identifier ) {
212+ // same directory `permission new` writes to
213+ let dir = match & tauri_dir {
214+ Some ( t) => t. clone ( ) ,
215+ None => std:: env:: current_dir ( ) . context ( "failed to resolve current directory" ) ?,
216+ } ;
217+ let permissions_dir = dir. join ( "permissions" ) ;
218+ if permissions_dir. exists ( ) {
219+ rm_permission_files ( & options. identifier , & permissions_dir) ?;
220+ }
198221 }
199222
200- if let Some ( tauri_dir) = resolve_tauri_dir ( ) {
223+ if let Some ( tauri_dir) = tauri_dir {
201224 let capabilities_dir = tauri_dir. join ( "capabilities" ) ;
202225 if capabilities_dir. exists ( ) {
203226 rm_permission_from_capabilities ( & options. identifier , & capabilities_dir) ?;
@@ -206,3 +229,38 @@ pub fn command(options: Options) -> Result<()> {
206229
207230 Ok ( ( ) )
208231}
232+
233+ fn is_plugin_wildcard ( identifier : & str ) -> bool {
234+ matches ! ( identifier. split_once( ':' ) , Some ( ( _, "*" ) ) )
235+ }
236+
237+ #[ cfg( test) ]
238+ mod tests {
239+ use super :: identifier_match;
240+
241+ #[ test]
242+ fn wildcard_matches_only_the_plugin_prefix ( ) {
243+ assert ! ( identifier_match( "os:*" , "os:default" ) ) ;
244+ assert ! ( identifier_match( "os:*" , "os:allow-platform" ) ) ;
245+ assert ! ( identifier_match( "os:*" , "os" ) ) ;
246+
247+ assert ! ( !identifier_match( "os:*" , "positioner:default" ) ) ;
248+ assert ! ( !identifier_match( "os:*" , "core:window:allow-close" ) ) ;
249+ assert ! ( !identifier_match( "os:*" , "cos:default" ) ) ;
250+ assert ! ( !identifier_match( "os:*" , "osx:default" ) ) ;
251+ }
252+
253+ #[ test]
254+ fn wildcard_does_not_match_nested_plugin_names ( ) {
255+ assert ! ( identifier_match( "core:*" , "core:window:allow-close" ) ) ;
256+ assert ! ( !identifier_match( "window:*" , "core:window:allow-close" ) ) ;
257+ }
258+
259+ #[ test]
260+ fn exact_identifier_match ( ) {
261+ assert ! ( identifier_match( "fs:default" , "fs:default" ) ) ;
262+ assert ! ( !identifier_match( "fs:default" , "fs:default-extra" ) ) ;
263+ assert ! ( !identifier_match( "fs:default" , "fs:allow-read" ) ) ;
264+ assert ! ( identifier_match( "my-permission" , "my-permission" ) ) ;
265+ }
266+ }
0 commit comments