命令作用域
作用域是定义 Tauri 命令(不)允许行为的一种细粒度方式。
作用域分为 允许 (allow) 和 拒绝 (deny) 作用域,其中 拒绝 作用域总是优先于 允许 作用域。
作用域类型需要是任何 serde 可序列化类型。这些类型通常是插件特定的。对于 Tauri 应用程序中实现的带作用域的命令,需要在应用程序中定义作用域类型,然后在命令实现中强制执行。
例如,Fs 插件允许您使用作用域来允许或拒绝某些目录和文件,而 http 插件使用作用域来过滤允许访问的 URL。
作用域被传递给命令,并且由命令本身实现处理或正确强制执行。
这些示例取自 Fs 插件权限
此插件中所有命令的作用域类型都是一个字符串,其中包含一个 glob 兼容路径。
[[permission]]identifier = "scope-applocaldata-recursive"description = '''This scope recursive access to the complete `$APPLOCALDATA` folder,including sub directories and files.'''
[[permission.scope.allow]]path = "$APPLOCALDATA/**"[[permission]]identifier = "deny-webview-data-linux"description = '''This denies read access to the`$APPLOCALDATA` folder on linux as the webview data andconfiguration values are stored here.Allowing access can lead to sensitive information disclosure andshould be well considered.'''platforms = ["linux"]
[[scope.deny]]path = "$APPLOCALDATA/**"
[[permission]]identifier = "deny-webview-data-windows"description = '''This denies read access to the`$APPLOCALDATA/EBWebView` folder on windows as the webview data andconfiguration values are stored here.Allowing access can lead to sensitive information disclosure andshould be well considered.'''platforms = ["windows"]
[[scope.deny]]path = "$APPLOCALDATA/EBWebView/**"上述作用域可用于允许访问 APPLOCALDATA 文件夹,同时阻止访问 Windows 上包含敏感 Webview 数据的 EBWebView 子文件夹。
这些可以合并成一个集合,从而减少重复配置并使其对查看应用程序配置的任何人来说更易于理解。
首先将拒绝作用域合并到 deny-default 中
[[set]]identifier = "deny-default"description = '''This denies access to dangerous Tauri relevant files andfolders by default.'''permissions = ["deny-webview-data-linux", "deny-webview-data-windows"]之后合并拒绝和允许作用域
[[set]]identifier = "scope-applocaldata-reasonable"description = '''This scope set allows access to the `APPLOCALDATA` folder andsubfolders except for linux,while it denies access to dangerous Tauri relevant files andfolders by default on windows.'''permissions = ["scope-applocaldata-recursive", "deny-default"]这些作用域可以用于所有命令,通过扩展插件的全局作用域,或者当它们与权限中启用的命令结合使用时,仅用于选定的命令。
对 APPLOCALDATA 中文件的合理只读文件访问可能如下所示
[[set]]identifier = "read-files-applocaldata"description = '''This set allows file read access to the `APPLOCALDATA` folder andsubfolders except for linux,while it denies access to dangerous Tauri relevant files andfolders by default on windows.'''permissions = ["scope-applocaldata-reasonable", "allow-read-file"]这些示例仅突出显示作用域功能本身。每个插件或应用程序开发者需要根据其用例考虑合理的作用域组合。
© 2025 Tauri 贡献者。CC-BY / MIT