跳转到内容
Tauri

权限

权限是对命令明确权限的描述。

[[permission]]
identifier = "my-identifier"
description = "This describes the impact and more."
commands.allow = [
"read_file"
]
[[scope.allow]]
my-scope = "$HOME/*"
[[scope.deny]]
my-scope = "$HOME/secret"

它可以使命令在前端被执行。它可以映射作用域到命令,并定义哪些命令是被启用的。权限可以启用或拒绝某些命令,定义作用域或者两者兼而有之。

权限可以作为一个集合被新的标识符分组。这被称为权限集。这允许将相关作用域的权限与命令相关的权限结合起来。它还可以将特定于操作系统的权限组合成更易用的集合。

作为插件开发者,你可以为所有公开的命令提供多个、预定义的、良好命名的权限。

作为应用开发者,你可以扩展现有的插件权限或为你的命令定义它们。它们可以被组合成集合,以便重用或简化后续的主配置文件。

权限标识符

权限标识符用于确保权限可以重用且有唯一的名称。

  • <name>:default 表示权限是一个插件或应用的默认权限
  • <name>:<command-name> 表示权限是针对单个命令的

插件前缀 tauri-plugin- 将在编译时自动添加到插件的标识符前,并且不需要手动指定。

标识符仅限于 ASCII 小写字母字符 [a-z],当前标识符的最大长度限制为 116,原因如下

const IDENTIFIER_SEPARATOR: u8 = b':';
const PLUGIN_PREFIX: &str = "tauri-plugin-";
// https://doc.rust-lang.net.cn/cargo/reference/manifest.html#the-name-field
const MAX_LEN_PREFIX: usize = 64 - PLUGIN_PREFIX.len();
const MAX_LEN_BASE: usize = 64;
const MAX_LEN_IDENTIFIER: usize = MAX_LEN_PREFIX + 1 + MAX_LEN_BASE;

配置文件

以下是一个简化的示例,展示塔里 插件 目录结构

终端窗口
tauri-plugin
├── README.md
├── src
└── lib.rs
├── build.rs
├── Cargo.toml
├── permissions
└── <identifier>.json/toml
└── default.json/toml

默认权限以特殊方式处理,因为它会被自动添加到应用配置文件中,只要使用 Tauri CLI 向塔里应用添加插件。

对于 应用 开发者,结构是类似的

终端窗口
tauri-app
├── index.html
├── package.json
├── src
├── src-tauri
├── Cargo.toml
├── permissions
└── <identifier>.toml
| ├── capabilities
└── <identifier>.json/.toml
├── src
├── tauri.conf.json

示例

来自 File System 插件的权限示例。

plugins/fs/permissions/autogenerated/base-directories/home.toml
[[permission]]
identifier = "scope-home"
description = """This scope permits access to all files and
list content of top level directories in the `$HOME`folder."""
[[scope.allow]]
path = "$HOME/*"
plugins/fs/permissions/read-files.toml
[[permission]]
identifier = "read-files"
description = """This enables all file read related
commands without any pre-configured accessible paths."""
commands.allow = [
"read_file",
"read",
"open",
"read_text_file",
"read_text_file_lines",
"read_text_file_lines_next"
]
plugins/fs/permissions/autogenerated/commands/mkdir.toml
[[permission]]
identifier = "allow-mkdir"
description = "This enables the mkdir command."
commands.allow = [
"mkdir"
]

示例:在应用中扩展上述插件权限

my-app/src-tauri/permissions/home-read-extends.toml
[[set]]
identifier = "allow-home-read-extended"
description = """ This allows non-recursive read access to files and to create directories
in the `$HOME` folder.
"""
permissions = [
"fs:read-files",
"fs:scope-home",
"fs:allow-mkdir"
]

© 2025 Tauri贡献者。CC-BY / MIT