1818from typing import Dict , List , TypedDict
1919
2020# Local modules
21- from .localplatform .localplatform import chown , chmod
21+ from .localplatform .localplatform import chown , chmod , get_chown_plugin_path
2222from .loader import Loader , Plugins
2323from .helpers import get_ssl_context , download_remote_binary_to_path
24+ from .enums import UserType
2425from .settings import SettingsManager
2526
2627logger = getLogger ("Browser" )
@@ -60,13 +61,6 @@ def _unzip_to_plugin_dir(self, zip: BytesIO, name: str, hash: str):
6061 return False
6162 zip_file = ZipFile (zip )
6263 zip_file .extractall (self .plugin_path )
63- plugin_folder = self .find_plugin_folder (name )
64- assert plugin_folder is not None
65- plugin_dir = path .join (self .plugin_path , plugin_folder )
66-
67- if not chown (plugin_dir ) or not chmod (plugin_dir , 555 ):
68- logger .error (f"chown/chmod exited with a non-zero exit code" )
69- return False
7064 return True
7165
7266 async def _download_remote_binaries_for_plugin_with_name (self , pluginBasePath : str ):
@@ -101,8 +95,6 @@ async def _download_remote_binaries_for_plugin_with_name(self, pluginBasePath: s
10195 rv = False
10296 raise Exception (f"Error Downloading Remote Binary { binName } @{ binURL } with hash { binHash } to { path .join (pluginBinPath , binName )} " )
10397
104- chown (self .plugin_path )
105- chmod (pluginBasePath , 555 )
10698 else :
10799 rv = True
108100 logger .info (f"No Remote Binaries to Download" )
@@ -124,6 +116,25 @@ def find_plugin_folder(self, name: str) -> str | None:
124116 return folder
125117 except :
126118 logger .debug (f"skipping { folder } " )
119+
120+ def set_plugin_dir_permissions (self , plugin_dir : str ) -> bool :
121+ plugin_json_path = path .join (plugin_dir , 'plugin.json' )
122+ logger .debug (f"Checking plugin.json at { plugin_json_path } " )
123+
124+ root_plugin = False
125+
126+ if access (plugin_json_path , R_OK ):
127+ with open (plugin_json_path , "r" , encoding = "utf-8" ) as f :
128+ plugin_json = json .load (f )
129+ if "flags" in plugin_json and "root" in plugin_json ["flags" ]:
130+ root_plugin = True
131+
132+ logger .debug ("root_plugin %d, dir %s" , root_plugin , plugin_dir )
133+ if get_chown_plugin_path ():
134+ return chown (plugin_dir , UserType .EFFECTIVE_USER if root_plugin else UserType .HOST_USER , True ) and chown (plugin_dir , UserType .EFFECTIVE_USER , False ) and chmod (plugin_dir , 755 ) and chown (plugin_json_path , UserType .EFFECTIVE_USER , False ) and chmod (plugin_json_path , 755 )
135+ else :
136+ logger .debug ("chown disabled by environment" )
137+ return True
127138
128139 async def uninstall_plugin (self , name : str ):
129140 if self .loader .watcher :
@@ -266,6 +277,7 @@ async def _install(self, artifact: str, name: str, version: str, hash: str):
266277 plugin_dir = path .join (self .plugin_path , plugin_folder )
267278 await self .loader .ws .emit ("loader/plugin_download_info" , 95 , "Store.download_progress_info.download_remote" )
268279 ret = await self ._download_remote_binaries_for_plugin_with_name (plugin_dir )
280+ chown_ret = self .set_plugin_dir_permissions (plugin_dir )
269281 if ret :
270282 logger .info (f"Installed { name } (Version: { version } )" )
271283 if name in self .loader .plugins :
@@ -278,6 +290,9 @@ async def _install(self, artifact: str, name: str, version: str, hash: str):
278290 self .settings .setSetting ("pluginOrder" , current_plugin_order )
279291 logger .debug ("Plugin %s was added to the pluginOrder setting" , name )
280292 await self .loader .import_plugin (path .join (plugin_dir , "main.py" ), plugin_folder )
293+ elif not chown_ret :
294+ logger .error ("Could not chown plugin" )
295+ return
281296 else :
282297 logger .error ("Could not download remote binaries" )
283298 return
0 commit comments