Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 39 ยท Security and Permissions

CSP, Isolation and Hardening Tauri Apps

Set a Content Security Policy for a Tauri app, use the isolation pattern to inspect IPC messages, freeze prototypes, audit dependencies and follow a release hardening checklist, with runnable models.

Capabilities and scopes limit what a compromised webview may ask for. The layers in this lesson make it harder to compromise the webview in the first place, and to abuse the bridge if it does happen. They are the Content Security Policy (CSP), the isolation pattern, and a handful of hardening habits that cost little and help a lot.

You should have read The Tauri Security Model and Capabilities and Permissions first.

Real versus stand-in. The JSON, HTML and Rust shown are real Tauri 2 configuration. The runnable blocks are JavaScript models of a CSP check and an isolation hook, for learning.

Content Security Policy

A CSP is a header-like string that tells the webview which sources of scripts, styles, images and network connections it may use. Anything else is blocked and reported in the console. It turns many XSS bugs from "attacker runs code" into "browser refuses and logs an error".

In Tauri you set it in src-tauri/tauri.conf.json:

JSON
{
  "app": {
    "security": {
      "csp": {
        "default-src": "'self'",
        "script-src": "'self'",
        "style-src": "'self' 'unsafe-inline'",
        "img-src": "'self' asset: data:",
        "font-src": "'self'",
        "connect-src": "ipc: http://ipc.localhost 'self' https://api.example.com"
      }
    }
  }
}

What the directives mean:

  • default-src 'self' is the fallback: only load from the app's own origin.
  • script-src 'self' allows scripts from your bundle but not inline scripts or eval. Avoid 'unsafe-inline' and 'unsafe-eval' for scripts.
  • style-src ... 'unsafe-inline' is commonly needed because many UI frameworks inject style tags; it is much less dangerous than for scripts.
  • img-src adds asset: (files exposed through Tauri's asset protocol) and data: images.
  • connect-src lists servers your frontend may call with fetch or WebSocket. Tauri's own IPC channel needs ipc: and http://ipc.localhost.

Tauri injects nonces and hashes into your bundled scripts and styles at build time, so your own code keeps working under a strict policy. In development the dev server must also be reachable: the dev config may be looser than production, so always test the release build.

Node.js Stand-in: check requests against a CSP

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice how the font request falls back to default-src because no font-src exists.

Handling external content

If you load remote scripts from a CDN, you must list that CDN, which also lets everything else it hosts run. Better: install packages with npm and bundle them. If you need a remote API, list the exact host in connect-src, not https: or *.

The isolation pattern

Even with a good CSP, a malicious npm package that runs inside your frontend can call invoke with arguments you never intended. The isolation pattern puts a small, sandboxed gatekeeper in between: every message from the frontend first goes through an <iframe> running a script you control, which can inspect, rewrite or reject it, then the message is encrypted with a key generated per session before it is delivered to the Rust core. Messages that were not routed through the isolation iframe cannot be decrypted.

Enable it in tauri.conf.json:

JSON
{
  "app": {
    "security": {
      "pattern": {
        "use": "isolation",
        "options": { "dir": "../dist-isolation" }
      }
    }
  }
}

The folder holds an index.html and a script:

HTML
<!doctype html>
<html>
  <head><meta charset="utf-8" /><title>Isolation</title></head>
  <body>
    <script src="index.js"></script>
  </body>
</html>
JavaScript
// dist-isolation/index.js
window.__TAURI_ISOLATION_HOOK__ = (payload) => {
  // payload.cmd is the command name; return the payload to let it through
  if (payload.cmd === 'plugin:fs|read_text_file' && String(payload.path ?? '').includes('..')) {
    throw new Error('blocked suspicious path');
  }
  return payload;
};

Isolation adds a small cost per message, so it is most useful in apps with a large dependency tree, those that display third-party content, or that guard sensitive actions. Keep the hook tiny and dependency-free, because it is itself security-critical code.

Node.js Stand-in: an isolation hook that vets messages

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

More hardening switches

  • freezePrototype: "app": { "security": { "freezePrototype": true } } freezes Object.prototype and related built-ins, which blocks "prototype pollution" tricks. Test it, as some libraries misbehave.
  • Disable devtools in production. Devtools are off in release builds unless you enable the devtools Cargo feature. Keep it that way.
  • Keep remote capability empty. See capabilities.
  • Avoid loading remote pages in the main window. If you must show remote content, use a separate window with no capability.
  • Audit dependencies. Run cargo audit and npm audit in CI, commit lockfiles, and update regularly.
  • Do not log secrets. Log files persist on disk.
  • Signed releases and updates. See Updater and Releases.
  • Review Cargo.toml features and remove plugins you no longer use.

A pre-release checklist

  1. Strict CSP with no unsafe-eval, no unsafe-inline for scripts, specific connect-src hosts.
  2. Capabilities per window, scoped paths, no "*" windows, no unneeded remote.
  3. App commands listed in the manifest and validated in Rust.
  4. No innerHTML of untrusted text anywhere; frameworks used with their default escaping.
  5. Isolation on if you have many dependencies or show third-party content.
  6. Release build tested with the production CSP.
  7. Dependencies audited; updater key stored safely.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does script-src 'self' do?

    With 'self', scripts must come from your bundle; injected inline scripts and third-party hosts are blocked.
  2. 2. Why do you list ipc: in connect-src?

    Without ipc: and http://ipc.localhost, a strict connect-src would block Tauri's commands.
  3. 3. What is the isolation pattern?

    Isolation routes messages through a script you control, so suspicious calls can be rejected, and encrypts the rest.
  4. 4. Why test the release build with the production CSP?

    Development often uses looser settings or a dev-server origin. Only the production build proves the policy works.

Summary

  • A CSP restricts where scripts, styles, images and connections may come from; Tauri adds nonces and hashes for your own bundle.
  • Keep script-src strict, list exact hosts in connect-src, and include ipc: for Tauri's channel.
  • The isolation pattern lets a small trusted hook vet every IPC message and encrypts the rest.
  • Freeze prototypes, keep devtools off, avoid remote content, audit dependencies and use the checklist before each release.

Next: Mobile Development