乐闻世界logo
搜索文章和话题

How do Tauri's security mechanisms and permission system work

2月19日 13:26

Tauri's security mechanism is one of its core advantages, ensuring application security through multi-layered protection:

Permission System

Tauri adopts the principle of least privilege, and by default the frontend cannot access system resources. Permissions need to be explicitly declared in tauri.conf.json:

json
{ "tauri": { "allowlist": { "fs": { "all": false, "readFile": true, "writeFile": true, "scope": ["$HOME/*"] }, "shell": { "all": false, "open": true } } } }

Content Security Policy (CSP)

Restrict resource loading and script execution through CSP:

rust
tauri::Builder::default() .plugin(tauri_plugin_csp::Builder::new() .default_policy(ContentSecurityPolicy { default_src: vec!["'self'"], script_src: vec!["'self'"], style_src: vec!["'self'"], ..Default::default() }) .build())

Scope Restrictions

Set scopes for file system, Shell, and other operations to prevent access to sensitive paths:

json
{ "fs": { "scope": ["$HOME/documents/*", "$APPDATA/*"] } }

Protocol Whitelist

Restrict the protocols and domains that WebView can access:

json
{ "tauri": { "security": { "csp": "default-src 'self'", "dangerousDisableAssetCspModification": false } } }

Rust Backend Isolation

  • Rust code runs in a separate process
  • Frontend cannot directly execute Rust code
  • All system calls go through Tauri's API

Best Practices

  1. Only grant necessary minimum permissions
  2. Use CSP to restrict resource loading
  3. Validate and sanitize all user inputs
  4. Regularly update dependencies
  5. Audit the security of third-party plugins
标签:Tauri