anarlog detect 插件权限参考:Tauri ACL 命令权限的自动参考文档与实现解析
【免费下载链接】anarlogOpen source Granola AI Alternative项目地址: https://gitcode.com/GitHub_Trending/hy/anarlog
本篇技术指南以 anarlog 仓库中tauri-plugin-detect插件的自动生成的权限参考文档 reference.md 为核心,完整解读该插件的 13 个 Tauri 命令对应的 26 条 ACL 权限(allow/deny)、默认权限集的构成方式,以及权限文件背后的 JSON Schema 结构;读完后你将掌握如何在 Tauri v2 项目中阅读、校验并按需扩展插件级命令权限,并能对照 命令实现 与 漂移检测测试 确认权限文档与源码始终一致。
1. reference.md 是什么:一份由插件构建系统自动生成的权限清单
plugins/detect/permissions/autogenerated/reference.md 位于plugins/detect/permissions/autogenerated/目录下,与同目录下的commands/子目录(存放每个命令的权限定义文件)一起,由 Tauri 插件构建系统在编译期自动生成。该目录的产物结构为:
reference.md:权限参考文档,包含两个部分——“Default Permission”(默认权限集说明)和“Permission Table”(完整权限表);commands/<命令名>.toml:每个 Tauri 命令一个权限文件,文件头带有# Automatically generated - DO NOT EDIT!标记;- plugins/detect/permissions/default.toml:定义插件默认放行的权限集合;
- plugins/detect/permissions/schemas/schema.json:所有权限文件的 JSON Schema(
PermissionFile)。
以 capture_meeting_chat_messages.toml 为例,每个自动生成的权限文件都成对定义allow-*与deny-*两个权限:
# Automatically generated - DO NOT EDIT! "$schema" = "../../schemas/schema.json" [[permission]] identifier = "allow-capture-meeting-chat-messages" description = "Enables the capture_meeting_chat_messages command without any pre-configured scope." commands.allow = ["capture_meeting_chat_messages"] [[permission]] identifier = "deny-capture-meeting-chat-messages" description = "Denies the capture_meeting_chat_messages command without any pre-configured scope." commands.deny = ["capture_meeting_chat_messages"]这解释了 reference.md 中每条权限描述里反复出现的 “without any pre-configured scope”(无预配置 scope):这些生成权限只控制命令级别的允许/拒绝,不包含细粒度 scope;如需更细的控制,可以在自定义权限文件中通过scope字段实现(见第 4 节)。
2. 默认权限集:13 条 allow 权限全量放行
reference.md 的 “Default Permission” 部分说明了插件的默认权限集(Default permissions for the plugin),其数据来源是 default.toml:
[default] description = "Default permissions for the plugin" permissions = [ "allow-list-installed-applications", "allow-get-installed-application-icons", "allow-list-mic-using-applications", "allow-inspect-meeting-accessibility", "allow-send-meeting-chat-message", "allow-capture-meeting-chat-messages", "allow-set-respect-do-not-disturb", "allow-set-ignored-bundle-ids", "allow-set-included-bundle-ids", "allow-list-default-ignored-bundle-ids", "allow-get-preferred-languages", "allow-get-current-locale-identifier", "allow-set-mic-active-threshold", ]该默认权限集包含以下 13 条allow-*权限(即 13 个命令全部默认放行,这也是 reference.md 中 “This default permission set includes the following” 清单的完整内容):
| 默认权限标识符 | 对应命令 | 功能用途 |
|---|---|---|
allow-list-installed-applications | list_installed_applications | 列出系统已安装的应用(返回 bundle id 与名称) |
allow-get-installed-application-icons | get_installed_application_icons | 获取指定应用图标的 PNG Data URL |
allow-list-mic-using-applications | list_mic_using_applications | 列出当前正在使用麦克风的应用 |
allow-inspect-meeting-accessibility | inspect_meeting_accessibility | 检查会议应用的辅助功能(Accessibility)可用性 |
allow-send-meeting-chat-message | send_meeting_chat_message | 向会议应用发送聊天消息 |
allow-capture-meeting-chat-messages | capture_meeting_chat_messages | 捕获会议应用的聊天消息 |
allow-set-respect-do-not-disturb | set_respect_do_not_disturb | 设置是否遵循系统“勿扰模式”(DND) |
allow-set-ignored-bundle-ids | set_ignored_bundle_ids | 设置被忽略(排除)的应用 bundle id 列表 |
allow-set-included-bundle-ids | set_included_bundle_ids | 设置被纳入(白名单)的应用 bundle id 列表 |
allow-list-default-ignored-bundle-ids | list_default_ignored_bundle_ids | 列出默认忽略的 bundle id |
allow-get-preferred-languages | get_preferred_languages | 获取系统首选语言列表(BCP-47 编码) |
allow-get-current-locale-identifier | get_current_locale_identifier | 获取当前本地化标识符(locale identifier) |
allow-set-mic-active-threshold | set_mic_active_threshold | 设置“麦克风活跃”判定的时间阈值(秒) |
3. 完整权限表:26 条 allow/deny 权限逐一对照
reference.md 的 “Permission Table” 以 HTML 表格形式列出了全部 26 条权限。以下将其转写为 Markdown 表格,并在原 Identifier 与 Description 之外补充“对应命令”与“源码实现位置”两列,便于检索与验证。所有权限标识符遵循 Tauri ACL 的命名约定{plugin}:{allow|deny}-{命令名-kebab-case},插件前缀固定为detect(见 src/lib.rs 中const PLUGIN_NAME: &str = "detect";)。
| Identifier | Description | 对应命令 | 源码实现 |
|---|---|---|---|
detect:allow-capture-meeting-chat-messages | Enables thecapture_meeting_chat_messagescommand without any pre-configured scope. | capture_meeting_chat_messages | commands.rs#L156-L173 |
detect:deny-capture-meeting-chat-messages | Denies thecapture_meeting_chat_messagescommand without any pre-configured scope. | capture_meeting_chat_messages | 同上 |
detect:allow-get-current-locale-identifier | Enables theget_current_locale_identifiercommand without any pre-configured scope. | get_current_locale_identifier | commands.rs#L226-L232 |
detect:deny-get-current-locale-identifier | Denies theget_current_locale_identifiercommand without any pre-configured scope. | get_current_locale_identifier | 同上 |
detect:allow-get-installed-application-icons | Enables theget_installed_application_iconscommand without any pre-configured scope. | get_installed_application_icons | commands.rs#L88-L110 |
detect:deny-get-installed-application-icons | Denies theget_installed_application_iconscommand without any pre-configured scope. | get_installed_application_icons | 同上 |
detect:allow-get-preferred-languages | Enables theget_preferred_languagescommand without any pre-configured scope. | get_preferred_languages | commands.rs#L215-L224 |
detect:deny-get-preferred-languages | Denies theget_preferred_languagescommand without any pre-configured scope. | get_preferred_languages | 同上 |
detect:allow-inspect-meeting-accessibility | Enables theinspect_meeting_accessibilitycommand without any pre-configured scope. | inspect_meeting_accessibility | commands.rs#L128-L134 |
detect:deny-inspect-meeting-accessibility | Denies theinspect_meeting_accessibilitycommand without any pre-configured scope. | inspect_meeting_accessibility | 同上 |
detect:allow-list-default-ignored-bundle-ids | Enables thelist_default_ignored_bundle_idscommand without any pre-configured scope. | list_default_ignored_bundle_ids | commands.rs#L120-L126 |
detect:deny-list-default-ignored-bundle-ids | Denies thelist_default_ignored_bundle_idscommand without any pre-configured scope. | list_default_ignored_bundle_ids | 同上 |
detect:allow-list-installed-applications | Enables thelist_installed_applicationscommand without any pre-configured scope. | list_installed_applications | commands.rs#L80-L86 |
detect:deny-list-installed-applications | Denies thelist_installed_applicationscommand without any pre-configured scope. | list_installed_applications | 同上 |
detect:allow-list-mic-using-applications | Enables thelist_mic_using_applicationscommand without any pre-configured scope. | list_mic_using_applications | commands.rs#L112-L118 |
detect:deny-list-mic-using-applications | Denies thelist_mic_using_applicationscommand without any pre-configured scope. | list_mic_using_applications | 同上 |
detect:allow-send-meeting-chat-message | Enables thesend_meeting_chat_messagecommand without any pre-configured scope. | send_meeting_chat_message | commands.rs#L136-L154 |
detect:deny-send-meeting-chat-message | Denies thesend_meeting_chat_messagecommand without any pre-configured scope. | send_meeting_chat_message | 同上 |
detect:allow-set-ignored-bundle-ids | Enables theset_ignored_bundle_idscommand without any pre-configured scope. | set_ignored_bundle_ids | commands.rs#L175-L183 |
detect:deny-set-ignored-bundle-ids | Denies theset_ignored_bundle_idscommand without any pre-configured scope. | set_ignored_bundle_ids | 同上 |
detect:allow-set-included-bundle-ids | Enables theset_included_bundle_idscommand without any pre-configured scope. | set_included_bundle_ids | commands.rs#L185-L193 |
detect:deny-set-included-bundle-ids | Denies theset_included_bundle_idscommand without any pre-configured scope. | set_included_bundle_ids | 同上 |
detect:allow-set-mic-active-threshold | Enables theset_mic_active_thresholdcommand without any pre-configured scope. | set_mic_active_threshold | commands.rs#L205-L213 |
detect:deny-set-mic-active-threshold | Denies theset_mic_active_thresholdcommand without any pre-configured scope. | set_mic_active_threshold | 同上 |
detect:allow-set-respect-do-not-disturb | Enables theset_respect_do_not_disturbcommand without any pre-configured scope. | set_respect_do_not_disturb | commands.rs#L195-L203 |
detect:deny-set-respect-do-not-disturb | Denies theset_respect_do_not_disturbcommand without any pre-configured scope. | set_respect_do_not_disturb | 同上 |
这 26 条权限正好覆盖 13 个命令:每个命令一对allow-*/deny-*,与permissions/autogenerated/commands/目录下的 13 个 TOML 文件一一对应(每个文件内定义该命令的一对权限)。
4. 权限文件的 Schema 结构:default、set 与 permission 三类字段
reference.md 中每个权限条目的描述模板(“Enables/Denies the X command without any pre-configured scope.”)都对应到 schema.json 中PermissionKind的枚举常量。该 Schema 定义了所有权限文件(包括上面生成的 TOML 和default.toml)的合法结构,顶层为PermissionFile对象,支持三类字段:
default(DefaultPermission):插件的默认权限集,行为等价于一个标识符为default的权限。必填字段permissions(字符串数组,引用其他权限标识符),可选version(uint64,最小值 1)与description(约定使用<h4>标题以便生成文档——这正是 reference.md 中 “#### This default permission set includes the following:” 这种<h4>风格的来源)。default.toml 就是该结构的实例;set(PermissionSet 数组):把若干直接权限(PermissionKind)按新的名字分组的权限集,必填identifier、description、permissions,可用于定义如 “meeting-chat 全套权限” 之类的组合;permission(Permission 数组):内联权限定义,唯一必填字段是identifier,其余可选字段包括:commands:{ allow: [...], deny: [...] }两个命令列表;Schema 明确说明“如果同一个命令同时出现在allow与deny中,则默认拒绝(denied by default)”,即 deny 优先;scope:{ allow: [...], deny: [...] }的任意 serde 可序列化值,用于对命令行为做细粒度控制;scope 会传递给命令实现并在其中强制执行。Schema 给出的示例形如{"allow": [{"path": "$HOME/**"}], "deny": [{"path": "$HOME/secret.txt"}]};platforms:该权限适用的目标平台数组,可选值枚举为macOS、windows、linux、android、iOS;不配置时对所有平台生效。
对照 detect 插件的实际产物可以看出:自动生成的 13 个命令权限文件只使用了commands.allow/commands.deny,未配置scope与platforms(全平台生效);而default.toml只使用default.permissions引用了 13 个allow-*标识符。这种“生成文件保持最小、自定义文件按 Schema 扩展”的分工,使得权限参考文档可以稳定地从单一来源再生成。
5. 源码级印证:命令注册、权限清单与漂移检测
reference.md 中列出的 13 个命令并非孤立存在,它们在源码中有完整的注册与实现链路,可以从三个层面验证权限文档与实现的一致性。
(1)命令实现与语义。全部命令实现集中在 plugins/detect/src/commands.rs,其中与权限表中“会议类”权限最相关的两条链路值得展开:
send_meeting_chat_message(message, mic_active_bundle_ids)(L138-L154):先调用list_mic_using_applications获取当前正在使用麦克风的应用,再通过intersect_mic_active_bundle_ids将前端传入的 bundle id 与实时结果求交集,只把“请求中且当前确实活跃”的 bundle id 传给anlg_detect::send_meeting_chat_message。这是一种纵深防御:即使拥有detect:allow-send-meeting-chat-message权限的前端代码传入了过期或伪造的 bundle id,也会被过滤掉。配套单元测试(L245-L283)验证了三个行为:请求与当前活跃应用求交且去重、拒绝陈旧/伪造的 bundle id、丢弃空 bundle id;capture_meeting_chat_messages(L158-L173):同样先取当前麦克风活跃应用,再用其 bundle id 列表调用anlg_detect::capture_meeting_chat_messages,即捕获范围天然被限定在“正在使用麦克风的应用”内。
(2)插件入口与命令清单。src/lib.rs 中make_specta_builder(L61-L81)通过tauri_specta::collect_commands!宏注册了与权限表完全一致的 13 个命令,init()(L83-L102)负责构建tauri-plugin-detect插件、挂载事件并初始化DetectorState/ProcessorState两个全局状态。插件的 Rust 包名为tauri-plugin-detect(见 plugins/detect/Cargo.toml),依赖anlg-detect(开启mic、list、language、sleep特性)提供底层检测能力,其中 macOS 专属实现(如图标抓取)通过objc2-app-kit等依赖完成。
(3)防漂移测试。command_manifest_is_single_source 测试(src/lib.rsL104-L159 的mod test中)把三方产物做交叉比对:src/manifest.rs中的COMMANDS清单、specta 导出的 TypeScript 绑定中plugin:detect|前缀的命令、以及./permissions/autogenerated/commands/目录下的全部 TOML 文件名(即权限文件)。任一方向出现漂移都会使测试失败(断言信息为 “generated permissions drifted from src/manifest.rs”)。因此 reference.md 中权限表覆盖的命令集合与插件真实注册的命令集合是测试保证一致的,而非人工维护的副本。
6. 如何使用与扩展这份权限参考
基于以上结构,在实际使用 detect 插件时有以下可操作的要点:
- 以默认权限集起步:应用侧只要声明 detect 插件的
default权限(即引用 default.toml 对应的默认集),13 个命令即全部可用,无需逐条声明; - 按需收紧:若只想暴露部分能力(例如仅允许语言/区域类只读命令),可在应用自身的权限声明中引用
allow-get-preferred-languages、allow-get-current-locale-identifier等具体allow-*标识符,而不使用default;由于 Schema 规定 deny 优先,追加对应的deny-*权限即可显式收回某个命令; - 细粒度控制走 scope:自动生成的权限不带 scope;如需限制,应依据 schema.json 中
Permission.scope的定义在自定义权限文件中编写(allow/deny两个任意 JSON 值数组),并在命令实现侧强制执行——这是 Tauri ACL 的约定,detect 插件的命令实现即为参考; - 平台限定:对仅在特定平台有意义的命令(如
get_installed_application_icons在非 macOS 平台返回空列表,见 commands.rs#L105-L109),可利用platforms字段(macOS/windows/linux/android/iOS)把权限限定到对应平台; - 文档再生成而非手改:
permissions/autogenerated/下的reference.md与各命令 TOML 均标注自动生成,新增/删除命令时应修改 Rust 命令实现与src/manifest.rs清单,重新构建后由构建系统再生成权限文件,并由command_manifest_is_single_source测试兜底校验一致性。
7. 小结
reference.md 以“默认权限集 + 全量权限表”的形式,把tauri-plugin-detect插件 13 个命令的 26 条 ACL 权限完整固化成了可检索的参考文档;结合 schema.json 可以理解其背后default/set/permission三类字段与 deny 优先、scope 强制执行的平台通用规则;再结合 commands.rs 的实现与 lib.rs 的防漂移测试,可以确认该文档与插件真实能力严格同步。对维护者而言,这份参考文档的价值在于:它既是前端调用前检查“是否已授权”的清单,也是权限收紧/扩展时的命名与结构依据。
【免费下载链接】anarlogOpen source Granola AI Alternative项目地址: https://gitcode.com/GitHub_Trending/hy/anarlog
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考