-
Notifications
You must be signed in to change notification settings - Fork 736
i18n(zh-cn): Add permission configuration related documents #3222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amourtoyou
wants to merge
5
commits into
tauri-apps:v2
Choose a base branch
from
amourtoyou:addi18n-security-permissions
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68de4ad
i18n(zh-cn): Add permission configuration related documents
amourtoyou 9c5bc71
Apply suggestions from code review
amourtoyou 09867a7
Apply suggestions from code review
amourtoyou 2358e39
Apply suggestions from code review
amourtoyou 32e3141
Apply suggestions from code review
amourtoyou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,156 @@ | ||||||
--- | ||||||
title: 权限 | ||||||
sidebar: | ||||||
order: 2 | ||||||
i18nReady: true | ||||||
--- | ||||||
|
||||||
权限(Permissions)是对命令所具备的明确特权(explicit privileges)的具体描述。 | ||||||
|
||||||
```toml | ||||||
[[permission]] | ||||||
identifier = "my-identifier" | ||||||
description = "此处填写带来的影响等等" | ||||||
commands.allow = [ | ||||||
"read_file" | ||||||
] | ||||||
|
||||||
[[scope.allow]] | ||||||
my-scope = "$HOME/*" | ||||||
|
||||||
[[scope.deny]] | ||||||
my-scope = "$HOME/secret" | ||||||
``` | ||||||
|
||||||
该功能允许在 Tauri 应用程序的前端访问特定命令,通过将作用域(Scopes)映射到命令并定义可启用的命令列表。 | ||||||
权限机制既可启用/禁用命令,也可定义作用域,或同时执行两种操作。 | ||||||
|
||||||
权限可以按新的标识符(Identifier)进行分组,形成`权限集(Permission set)`。这种机制允许将以下两类权限进行组合: | ||||||
1. 范围相关权限(如数据访问范围) | ||||||
2. 命令相关权限(如执行特定操作的权限) | ||||||
同时,还能将操作系统层面的特定权限归类为更易于使用的集合。 | ||||||
|
||||||
作为插件开发者,您可以为所有对外暴露的命令提供多个预定义且命名清晰的权限。 | ||||||
|
||||||
作为应用开发者,您可以扩展现有插件权限或为自定义命令定义新权限。 | ||||||
这些权限可被分组或组合成权限集(Permission Set),以便复用或简化后续的主配置文件配置。 | ||||||
|
||||||
## 权限标识符 | ||||||
|
||||||
权限标识符(Permissions identifier)用于确保权限可被复用,并具有唯一的名称。 | ||||||
|
||||||
:::tip | ||||||
|
||||||
此处 **name** 指插件的 crate 名称(不含 `tauri-plugin-` 前缀), | ||||||
用于命名空间隔离以降低命名冲突风险;当涉及应用程序自身权限引用时无需使用该前缀。 | ||||||
|
||||||
::: | ||||||
|
||||||
- `<name>:default` 表明该权限是插件或应用程序的默认设置 | ||||||
- `<name>:<command-name>` 表示该权限仅适用于单个命令(或独立命令)。 | ||||||
|
||||||
插件标识符在编译时将自动添加 `tauri-plugin-` 前缀,无需手动指定。 | ||||||
|
||||||
标识符仅限于ASCII小写字母字符 `[a-z]`,且当前最大长度限制为 `116` 字符(由以下常量决定): | ||||||
|
||||||
```rust | ||||||
const IDENTIFIER_SEPARATOR: u8 = b':'; | ||||||
const PLUGIN_PREFIX: &str = "tauri-plugin-"; | ||||||
|
||||||
// https://doc.rust-lang.org/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 **插件**目录结构的简化示例: | ||||||
|
||||||
```sh | ||||||
tauri-plugin | ||||||
├── README.md | ||||||
├── src | ||||||
│ └── lib.rs | ||||||
├── build.rs | ||||||
├── Cargo.toml | ||||||
├── permissions | ||||||
│ └── <identifier>.json/toml | ||||||
│ └── default.json/toml | ||||||
``` | ||||||
|
||||||
默认权限以特殊方式处理,只要使用 Tauri CLI 向 Tauri 应用添加插件,该权限便会自动加入应用配置中。 | ||||||
|
||||||
对于**应用程序**开发者而言,其结构类似: | ||||||
|
||||||
```sh | ||||||
tauri-app | ||||||
├── index.html | ||||||
├── package.json | ||||||
├── src | ||||||
├── src-tauri | ||||||
│ ├── Cargo.toml | ||||||
│ ├── permissions | ||||||
│ └── <identifier>.toml | ||||||
| ├── capabilities | ||||||
│ └── <identifier>.json/.toml | ||||||
│ ├── src | ||||||
│ ├── tauri.conf.json | ||||||
``` | ||||||
|
||||||
:::note | ||||||
|
||||||
作为应用开发者,功能配置文件(Capability files)可使用 `json`/`json5` 或 `toml` 格式编写,但权限配置仅支持 `toml` 格式。 | ||||||
|
||||||
::: | ||||||
|
||||||
## 示例 | ||||||
|
||||||
来自 File System 插件的权限示例 | ||||||
|
||||||
```toml title="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/*" | ||||||
``` | ||||||
|
||||||
```toml title="plugins/fs/permissions/read-files.toml" | ||||||
[[permission]] | ||||||
identifier = "read-files" | ||||||
description = """将允许所有读取文件的命令,无需预先指定可访问路径。""" | ||||||
commands.allow = [ | ||||||
"read_file", | ||||||
"read", | ||||||
"open", | ||||||
"read_text_file", | ||||||
"read_text_file_lines", | ||||||
"read_text_file_lines_next" | ||||||
] | ||||||
``` | ||||||
|
||||||
```toml title="plugins/fs/permissions/autogenerated/commands/mkdir.toml" | ||||||
[[permission]] | ||||||
identifier = "allow-mkdir" | ||||||
description = "This enables the mkdir command." | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
commands.allow = [ | ||||||
"mkdir" | ||||||
] | ||||||
``` | ||||||
|
||||||
以下示例演示如何在您的应用中扩展上述插件权限的实现: | ||||||
|
||||||
```toml title="my-app/src-tauri/permissions/home-read-extends.toml" | ||||||
[[set]] | ||||||
identifier = "allow-home-read-extended" | ||||||
description = """ 将允许在 `$HOME` 目录下使用非递归方式读取文件和创建文件夹 | ||||||
""" | ||||||
permissions = [ | ||||||
"fs:read-files", | ||||||
"fs:scope-home", | ||||||
"fs:allow-mkdir" | ||||||
] | ||||||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.