Security: Fortifying Your App
Security: Fortifying Your App
Section titled “Security: Fortifying Your App”Unlike a standard website, a Tauri application has access to the user’s operating system. With great power comes great responsibility. Security in Tauri is built on the philosophy that the frontend should never be fully trusted.
Isolation Pattern
Section titled “Isolation Pattern”The most secure way to design your app is to assume the WebView could be compromised (e.g., via a malicious third-party script). The Isolation Pattern intercepts messages sent from the frontend to the backend and injects a secure bridge. This prevents arbitrary JavaScript from accessing Tauri APIs directly unless explicitly allowed.
While complex to implement manually, Tauri v2 provides robust defaults using the Access Control List (ACL) system to isolate capabilities.
Content Security Policy (CSP)
Section titled “Content Security Policy (CSP)”CSP is your first line of defense against Cross-Site Scripting (XSS) attacks. It restricts the sources from which your application can load scripts, styles, and images. In Tauri, you should configure this in your tauri.conf.json under the security section.
{ "app": { "security": { "csp": "default-src 'self'; script-src 'self'; style-src 'self'" } }}This configuration tells the WebView: “Only load content that is packaged with the app (‘self’). Do not execute external scripts or load remote styles.” Avoid using 'unsafe-inline' or wildcards (*) whenever possible.
Validating Inputs in Rust
Section titled “Validating Inputs in Rust”Never assume data coming from the frontend is safe. If you have a command that writes a file, and the frontend sends the filename, a malicious actor might send ../../../../etc/passwd.
Always sanitize and validate inputs on the Rust side:
#[tauri::command]fn save_document(filename: String, content: String) -> Result<(), String> { // BAD: File::create(filename)
// GOOD: Validate the filename contains no path traversals if filename.contains("/") || filename.contains("\\") { return Err("Invalid filename".into()); } // Proceed with saving... Ok(())}Remote Code Execution (RCE) Risks
Section titled “Remote Code Execution (RCE) Risks”One of the biggest security risks is allowing your app to navigate to remote URLs (like https://google.com) while having Tauri API hooks injected. If that remote site is compromised, the attacker gains full control over the user’s OS via your app.
Golden Rule: Only expose Tauri APIs to your local content. If you must display remote content, use an <iframe> or a separate window with no Tauri capabilities enabled.
Compliance and Guidelines
Section titled “Compliance and Guidelines”Tauri undergoes independent security audits. To maintain this security posture, you should review the official Tauri Security Guidelines. These guidelines cover configuring permissions scopes (limiting which filesystem paths are accessible) and freezing objects in the WebView to prevent prototype pollution.