Skip to content

Lesson 2 of 9 · 26m

Rust Core, WebView Frontend: Designing the Boundary

Model commands, events, state, and serialization as a trust boundary rather than treating the frontend and backend as one JavaScript program.

Teams coming from Electron often bring a bad habit with them: they treat the UI process as the center of the universe and let it reach directly into powerful APIs. In Tauri, the correct mental model is narrower. The WebView is an untrusted-ish client. Rust is the privileged service. Commands are the API between them. If you design that API well, Tauri gives you a meaningful security boundary; if you expose generic shell and filesystem primitives, you have merely added ceremony.

The process-shaped design

The frontend never imports Rust directly. It invokes named commands whose arguments cross a serialization boundary, while events flow back from the core. Validation belongs in Rust even when TypeScript already validates input for usability.

Commands should be verbs in your domain

A command named read_file(path) is a filesystem API accidentally exposed to the UI. A command named load_project(path) can enforce workspace policy, extensions, size limits, symlink behavior, and audit logging. Prefer narrow domain operations:

#[tauri::command]
async fn open_project(path: String, root: tauri::State<'_, WorkspaceRoot>) -> Result<String, String> {
    let candidate = std::fs::canonicalize(root.0.join(path)).map_err(|e| e.to_string())?;
    if !candidate.starts_with(&root.0) { return Err("path escapes workspace".into()); }
    std::fs::read_to_string(candidate).map_err(|e| e.to_string())
}

The shape matters more than the syntax. open_project can become safer over time. read_file cannot; its entire purpose is broad authority.

Events are broadcasts, not private replies

Tauri events are convenient for progress, tray updates, file-watch notifications, and background jobs. They are also easy to over-share. A global event can be received by any window listening for that name. Do not place tokens, document contents, or sensitive object identifiers in events unless every listener is authorized to receive them.

window.emit("import-progress", serde_json::json!({
    "job_id": job_id,
    "percent": percent,
})).map_err(|e| e.to_string())?;

Use events for state transitions, not as a secret backchannel. If only one window should receive a result, target that window deliberately or return the value from the command.

State is shared mutable application memory

tauri::State is useful for database pools, configuration, cancellation tokens, and background task handles. It is not a substitute for concurrency design:

struct AppState {
    db: sqlx::SqlitePool,
    jobs: tokio::sync::Mutex<std::collections::HashMap<String, tokio::task::JoinHandle<()>>>,
}

Treat every command handler as concurrent. Two windows can invoke the same command at the same time. A long-running operation can outlive the window that started it. Cancellation, idempotency, and ownership belong in the Rust design from day one.

Field notes

  • Never expose a generic command runner to the frontend. run_shell(command) converts any renderer compromise into host compromise.
  • Keep command handlers thin and put policy in testable Rust modules.
  • Validate paths after canonicalization, not before, and remember canonicalization itself can fail for missing files.
  • Avoid storing secrets in frontend-accessible state. The WebView is not a vault.

Was this lesson helpful?

Sign in to rate