mcpField
M
managing-tauri-plugin-permissions

managing-tauri-plugin-permissions

github.com/dchukセキュリティ36156 回インストール40 週間前に更新
リポジトリ

managing-tauri-plugin-permissions について

Tauri アプリ開発者向けのセキュリティ設定スキルです。ウィンドウやプラットフォームごとのケイパビリティ、プラグイン権限、独自の権限設定を扱います。

主な機能

  1. 01ウィンドウとプラットフォーム別の設定
  2. 02コマンド権限とパススコープの設定
  3. 03独自プラグイン権限セットの作成
  4. 04許可・拒否権限の自動生成

活用シーン

  1. 01ウィンドウごとのシステムアクセス制限
  2. 02ファイル操作のパス範囲指定
  3. 03独自プラグインの権限レベル設計

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/dchuk/claude-code-tauri-skills --skill managing-tauri-plugin-permissions

インストール後、Claude Code で/managing-tauri-plugin-permissionsを入力して読み込まれたか確認します。

詳細

Tauri プラグインの権限管理

Tauri のケイパビリティと権限システムにより、デスクトップアプリケーションとモバイルアプリケーションのセキュリティをきめ細かく制御できます。このスキルでは、ウィンドウやプラットフォーム向けのケイパビリティの設定、プラグイン権限の効果的な使用方法、カスタムプラグイン権限の作成方法について説明します。

基本概念

ケイパビリティ

ケイパビリティは、特定のウィンドウとプラットフォームに権限を割り当てる JSON 設定ファイルです。最小権限の原則に従います。

場所: src-tauri/capabilities/

構造:

{
  "identifier": "capability-name",
  "description": "Human-readable purpose",
  "local": true,
  "windows": ["window-label"],
  "permissions": ["plugin:allow-action"],
  "platforms": ["linux", "windows", "macos", "android", "ios"]
}
権限レベル

権限は次の 2 つのレベルで動作します。

  • コマンド: 個々の操作(例: allow-write-text-file)
  • スコープ: アクセス可能なファイルやディレクトリを定義する、パスベースの制限

プラットフォーム固有のケイパビリティ

プラットフォームの指定

platforms フィールドを使用して、ケイパビリティを特定のオペレーティングシステムに制限します。

{
  "identifier": "desktop-fs-access",
  "description": "Filesystem access for desktop platforms",
  "windows": ["main"],
  "permissions": ["fs:allow-home-read"],
  "platforms": ["linux", "windows", "macos"]
}

利用可能なプラットフォーム: linux、windows、macos、android、ios

モバイル専用のケイパビリティ
{
  "identifier": "mobile-camera",
  "description": "Camera access for mobile devices",
  "windows": ["main"],
  "permissions": ["camera:allow-capture"],
  "platforms": ["android", "ios"]
}

ウィンドウ固有のケイパビリティ

複数のウィンドウの設定

tauri.conf.json でウィンドウを定義します。

{
  "windows": [
    {
      "label": "main",
      "title": "Main Window",
      "width": 800,
      "height": 600
    },
    {
      "label": "settings",
      "title": "Settings",
      "width": 400,
      "height": 300
    }
  ]
}
ウィンドウへのケイパビリティの割り当て

各ウィンドウのニーズに応じて、個別のケイパビリティファイルを作成します。

src-tauri/capabilities/main-window.json:

{
  "identifier": "main-window-capabilities",
  "description": "Full access for main window",
  "local": true,
  "windows": ["main"],
  "permissions": [
    "fs:allow-home-read",
    "fs:allow-home-write",
    "dialog:allow-open",
    "dialog:allow-save"
  ]
}

src-tauri/capabilities/settings-window.json:

{
  "identifier": "settings-window-capabilities",
  "description": "Limited access for settings window",
  "local": true,
  "windows": ["settings"],
  "permissions": [
    "fs:allow-app-read",
    "fs:allow-app-write"
  ]
}
ウィンドウ間で共有するケイパビリティ
{
  "identifier": "shared-dialog",
  "description": "Dialog access for multiple windows",
  "local": true,
  "windows": ["main", "settings"],
  "permissions": ["dialog:allow-ask", "dialog:allow-message"]
}

プラグイン権限の使用

デフォルトの権限セット

すべてのプラグインには、基本的なアクセス権を提供する default 権限セットがあります。次のように有効にします。

{
  "permissions": ["plugin-name:default"]
}
利用可能な権限の確認
  1. 公式プラグイン: Tauri のドキュメントにある権限の表を確認します
  2. プラグインのソース: permissions/autogenerated ディレクトリを確認します
  3. コミュニティプラグイン: リポジトリまたは crates.io のページを確認します
権限識別子の形式
plugin-name:permission-name

例:

  • fs:allow-read
  • fs:allow-write-text-file
  • dialog:allow-open
  • shell:allow-spawn
スコープを使った設定

権限を特定のパスに制限します。

{
  "identifier": "default",
  "description": "Main window capabilities",
  "windows": ["main"],
  "permissions": [
    "fs:allow-write-text-file",
    {
      "identifier": "fs:allow-read",
      "allow": [{ "path": "$HOME/Documents/**" }]
    },
    {
      "identifier": "fs:allow-write",
      "allow": [{ "path": "$APP/**" }]
    }
  ]
}
一般的なスコープ変数
変数説明
$APPアプリケーションのデータディレクトリ
$HOMEユーザーのホームディレクトリ
$RESOURCEアプリケーションのリソース
$TEMP一時ディレクトリ
$DESKTOPユーザーのデスクトップ
$DOCUMENTユーザーのドキュメント
$DOWNLOADユーザーのダウンロード
拒否する権限

特定の操作を明示的に拒否します。

{
  "permissions": [
    "fs:default",
    "fs:deny-write-text-file"
  ]
}

カスタムプラグイン権限の作成

プラグインの構造

Tauri CLI でプラグインを作成します。

cargo tauri plugin new my-plugin
cd tauri-plugin-my-plugin
コマンドの実装

src/commands.rs:

use tauri::{command, AppHandle, Runtime};

#[command]
pub(crate) async fn read_data<R: Runtime>(
    key: String,
    app: AppHandle<R>,
) -> Result<String, String> {
    // Implementation
    Ok(format!("Data for key: {}", key))
}

#[command]
pub(crate) async fn write_data<R: Runtime>(
    key: String,
    value: String,
    app: AppHandle<R>,
) -> Result<(), String> {
    // Implementation
    Ok(())
}

#[command]
pub(crate) async fn delete_data<R: Runtime>(
    key: String,
    app: AppHandle<R>,
) -> Result<(), String> {
    // Implementation
    Ok(())
}
権限の自動生成

src/build.rs:

const COMMANDS: &[&str] = &["read_data", "write_data", "delete_data"];

fn main() {
    tauri_plugin::Builder::new(COMMANDS)
        .global_api_script_path("./api-iife.js")
        .build();
}

次の権限が生成されます。

  • allow-read-data / deny-read-data
  • allow-write-data / deny-write-data
  • allow-delete-data / deny-delete-data
デフォルト権限の定義

permissions/default.toml:

"$schema" = "schemas/schema.json"

[default]
description = "Default permissions for my-plugin. Allows read operations only."
permissions = ["allow-read-data"]
権限セットの作成

permissions/read-write.toml:

"$schema" = "schemas/schema.json"

[[set]]
identifier = "read-write"
description = "Allows both read and write operations"
permissions = ["allow-read-data", "allow-write-data"]

permissions/full-access.toml:

"$schema" = "schemas/schema.json"

[[set]]
identifier = "full-access"
description = "Allows all operations including delete"
permissions = ["allow-read-data", "allow-write-data", "allow-delete-data"]
コマンドの登録

src/lib.rs:

use tauri::{
    plugin::{Builder, TauriPlugin},
    Manager, Runtime,
};

mod commands;

pub fn init<R: Runtime>() -> TauriPlugin<R> {
    Builder::new("my-plugin")
        .invoke_handler(tauri::generate_handler![
            commands::read_data,
            commands::write_data,
            commands::delete_data,
        ])
        .build()
}
フロントエンドの JavaScript バインディング

guest-js/index.ts:

import { invoke } from '@tauri-apps/api/core';

export async function readData(key: string): Promise<string> {
  return await invoke('plugin:my-plugin|read_data', { key });
}

export async function writeData(key: string, value: string): Promise<void> {
  return await invoke('plugin:my-plugin|write_data', { key, value });
}

export async function deleteData(key: string): Promise<void> {
  return await invoke('plugin:my-plugin|delete_data', { key });
}
カスタムプラグイン権限の使用

アプリケーションのケイパビリティファイルで次のように設定します。

{
  "identifier": "default",
  "windows": ["main"],
  "permissions": [
    "my-plugin:default",
    "my-plugin:read-write",
    "my-plugin:allow-delete-data"
  ]
}

完全な例: クロスプラットフォームアプリ

src-tauri/capabilities/desktop.json:

{
  "$schema": "../gen/schemas/desktop-schema.json",
  "identifier": "desktop",
  "windows": ["main"],
  "platforms": ["linux", "windows", "macos"],
  "permissions": [
    "core:default",
    "fs:default",
    { "identifier": "fs:allow-read", "allow": [{ "path": "$HOME/Documents/**" }] },
    { "identifier": "fs:allow-write", "allow": [{ "path": "$APP/**" }] },
    "dialog:allow-open",
    "dialog:allow-save",
    "shell:allow-open"
  ]
}

src-tauri/capabilities/mobile.json:

{
  "identifier": "mobile",
  "windows": ["main"],
  "platforms": ["android", "ios"],
  "permissions": [
    "fs:allow-app-read",
    "fs:allow-app-write",
    "notification:default"
  ]
}

ベストプラクティス

セキュリティ
  1. 最小権限の原則: 必要な権限のみを付与します
  2. スコープの使用: 広範な権限ではなく、特定のパスにファイルアクセスを制限します
  3. ウィンドウごとに分離: 各ウィンドウには必要な権限のみを付与します
  4. プラットフォームの指定: デスクトップにモバイルの権限を付与したり、その逆を行ったりしないようにします
構成
  1. 機能ごとに整理: 機能領域ごとにケイパビリティをグループ化します
  2. 説明的な識別子を使用: ケイパビリティの目的が明確になるようにします
  3. 権限を文書化: 各権限が必要な理由を説明する記述を含めます
プラグイン開発
  1. 最小限のデフォルト設定:デフォルトの権限セットは制限的にします
  2. 権限セットを作成する:段階的なアクセスレベル(読み取り専用、読み書き、フルアクセス)を用意します
  3. 自動生成を活用する:Tauri にコマンドの許可/拒否権限を生成させます
  4. 権限をテストする:サンプルアプリケーションで権限の動作を確認します

トラブルシューティング

権限拒否エラー

権限エラーが発生した場合は、次の項目を確認してください。

  1. capability ファイルの構文が正しいことを確認します(有効な JSON)
  2. ウィンドウラベルが設定と一致していることを確認します
  3. 権限識別子が正しいことを確認します
  4. パスベースの操作にスコープが必要かどうかを確認します
Capability が適用されない
  1. capability ファイルが src-tauri/capabilities/ にあることを確認します
  2. windows 配列に正しいウィンドウラベルが含まれていることを確認します
  3. platforms に対象の OS が含まれていることを確認します
  4. capability の変更後にアプリケーションを再ビルドします

参考資料

コメント