From c9b623c438493067c4d67809067470bc39b18372 Mon Sep 17 00:00:00 2001 From: rob thijssen Date: Tue, 15 Sep 2026 18:07:48 +0300 Subject: [PATCH] feat(app): isolation pattern on, and a capability file that must say why The IPC isolation pattern is on from the first command: every message from the main window passes through the sandboxed isolation application and is encrypted before Rust sees it, so a script injected into the webview can read what the UI shows but cannot forge a command. The hook passes everything through today; the point of shipping it now is that every later command is born inside the boundary. Both `tauri` and `tauri-build` need the `isolation` feature, and the build script refuses to proceed if either lacks it. Capabilities are TOML rather than JSON so each permission can carry the reason it exists on the line above it. `tests/capabilities.rs` makes that comment mandatory, requires a real description on every capability, and refuses to grant anything to a window other than `main`. A capability is the one place a plugin's reach into the system is widened, and it should not be possible to widen it quietly. Closes #11 Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_014ftBXYuba8ARhQeF74oUgW --- Cargo.lock | 123 ++++++++++++++++++++ Cargo.toml | 6 +- crates/wallet-app/Cargo.toml | 3 + crates/wallet-app/capabilities/default.json | 7 -- crates/wallet-app/capabilities/default.toml | 14 +++ crates/wallet-app/isolation/index.html | 21 ++++ crates/wallet-app/tauri.conf.json | 43 ++++++- crates/wallet-app/tests/capabilities.rs | 97 +++++++++++++++ 8 files changed, 299 insertions(+), 15 deletions(-) delete mode 100644 crates/wallet-app/capabilities/default.json create mode 100644 crates/wallet-app/capabilities/default.toml create mode 100644 crates/wallet-app/isolation/index.html create mode 100644 crates/wallet-app/tests/capabilities.rs diff --git a/Cargo.lock b/Cargo.lock index a0e3cc9..3ae045c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,41 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + [[package]] name = "aho-corasick" version = "1.1.5" @@ -329,6 +364,16 @@ dependencies = [ "windows-link 0.2.1", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "combine" version = "4.6.8" @@ -429,6 +474,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", + "rand_core", "typenum", ] @@ -471,6 +517,15 @@ version = "0.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52560adf09603e58c9a7ee1fe1dcb95a16927b17c127f0ac02d6e768a0e25bc1" +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + [[package]] name = "darling" version = "0.21.3" @@ -1032,6 +1087,16 @@ dependencies = [ "r-efi 6.0.0", ] +[[package]] +name = "ghash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" +dependencies = [ + "opaque-debug", + "polyval", +] + [[package]] name = "gio" version = "0.18.4" @@ -1476,6 +1541,15 @@ dependencies = [ "cfb", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + [[package]] name = "ipnet" version = "2.12.2" @@ -2054,6 +2128,12 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "option-ext" version = "0.2.0" @@ -2218,6 +2298,18 @@ dependencies = [ "miniz_oxide 0.8.9", ] +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + [[package]] name = "potential_utf" version = "0.1.6" @@ -2331,6 +2423,15 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", +] + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -2857,6 +2958,12 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + [[package]] name = "swift-rs" version = "1.0.8" @@ -3035,6 +3142,7 @@ dependencies = [ "tokio", "tray-icon", "url", + "uuid", "webkit2gtk", "webview2-com", "window-vibrancy", @@ -3057,6 +3165,7 @@ dependencies = [ "semver", "serde", "serde_json", + "tauri-codegen", "tauri-utils", "tauri-winres", "walkdir", @@ -3160,12 +3269,14 @@ version = "2.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e176a18e67764923c4f1ce66f25ae4abe5f688384d5eb1a0fa6c77f3d90f887" dependencies = [ + "aes-gcm", "anyhow", "brotli", "cargo_metadata", "ctor", "dom_query", "dunce", + "getrandom 0.3.4", "glob", "http", "infer", @@ -3183,6 +3294,7 @@ dependencies = [ "serde-untagged", "serde_json", "serde_with", + "serialize-to-javascript", "swift-rs", "thiserror 2.0.20", "toml 1.1.6+spec-1.1.0", @@ -3680,6 +3792,16 @@ version = "1.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common", + "subtle", +] + [[package]] name = "url" version = "2.5.8" @@ -3779,6 +3901,7 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "toml 0.8.2", "tracing", "tracing-subscriber", "wallet-core", diff --git a/Cargo.toml b/Cargo.toml index abbcafd..697596c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,9 +26,11 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } # Types in `wallet-entities` are exported to the webview as TypeScript so the # UI and the Rust side never disagree about a field. See .cargo/config.toml. ts-rs = { version = "10", features = ["serde-compat", "no-serde-warnings"] } +toml = "0.8" zeroize = { version = "1", features = ["derive"] } -tauri = { version = "2", features = [] } -tauri-build = { version = "2", features = [] } +# `isolation`: the IPC isolation pattern (crates/wallet-app/isolation/index.html). +tauri = { version = "2", features = ["isolation"] } +tauri-build = { version = "2", features = ["isolation"] } [profile.release] lto = "thin" diff --git a/crates/wallet-app/Cargo.toml b/crates/wallet-app/Cargo.toml index 634962e..f643131 100644 --- a/crates/wallet-app/Cargo.toml +++ b/crates/wallet-app/Cargo.toml @@ -26,3 +26,6 @@ serde.workspace = true serde_json.workspace = true tracing.workspace = true tracing-subscriber.workspace = true + +[dev-dependencies] +toml.workspace = true diff --git a/crates/wallet-app/capabilities/default.json b/crates/wallet-app/capabilities/default.json deleted file mode 100644 index e53b9ed..0000000 --- a/crates/wallet-app/capabilities/default.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "$schema": "../gen/schemas/desktop-schema.json", - "identifier": "default", - "description": "The main window may call the app's own commands and nothing else. Every plugin permission is added here, deliberately, with a reason.", - "windows": ["main"], - "permissions": ["core:default"] -} diff --git a/crates/wallet-app/capabilities/default.toml b/crates/wallet-app/capabilities/default.toml new file mode 100644 index 0000000..a43e3d4 --- /dev/null +++ b/crates/wallet-app/capabilities/default.toml @@ -0,0 +1,14 @@ +# The main window's capability: the only capability this app has. +# +# TOML rather than JSON so every permission can carry the reason it exists, +# on the line above it. `tests/capabilities.rs` fails the build when a +# permission has no such comment, or when a capability lacks a description, +# or when any window other than `main` is granted anything. +identifier = "default" +description = "The main window may invoke this app's own commands and nothing else. Every permission below is deliberate and carries a reason." +windows = ["main"] + +permissions = [ + # The app's own commands and the core event/window plumbing the webview needs to render at all. + "core:default", +] diff --git a/crates/wallet-app/isolation/index.html b/crates/wallet-app/isolation/index.html new file mode 100644 index 0000000..b3602e8 --- /dev/null +++ b/crates/wallet-app/isolation/index.html @@ -0,0 +1,21 @@ + + + + + + + + + diff --git a/crates/wallet-app/tauri.conf.json b/crates/wallet-app/tauri.conf.json index ca9412c..5a68ac5 100644 --- a/crates/wallet-app/tauri.conf.json +++ b/crates/wallet-app/tauri.conf.json @@ -6,8 +6,14 @@ "build": { "frontendDist": "../../ui/dist", "devUrl": "http://localhost:5173", - "beforeDevCommand": { "script": "pnpm dev", "cwd": "../../ui" }, - "beforeBuildCommand": { "script": "pnpm build", "cwd": "../../ui" } + "beforeDevCommand": { + "script": "pnpm dev", + "cwd": "../../ui" + }, + "beforeBuildCommand": { + "script": "pnpm build", + "cwd": "../../ui" + } }, "app": { "windows": [ @@ -21,12 +27,27 @@ } ], "security": { - "csp": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src ipc: http://ipc.localhost" + "csp": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src ipc: http://ipc.localhost", + "pattern": { + "use": "isolation", + "options": { + "dir": "isolation" + } + }, + "capabilities": [ + "default" + ] } }, "bundle": { "active": true, - "targets": ["rpm", "deb", "appimage", "nsis", "dmg"], + "targets": [ + "rpm", + "deb", + "appimage", + "nsis", + "dmg" + ], "icon": [ "icons/32x32.png", "icons/128x128.png", @@ -37,8 +58,18 @@ "category": "Finance", "shortDescription": "A desktop wallet for post-quantum chains", "linux": { - "rpm": { "depends": ["webkit2gtk4.1", "gtk3"] }, - "deb": { "depends": ["libwebkit2gtk-4.1-0", "libgtk-3-0"] } + "rpm": { + "depends": [ + "webkit2gtk4.1", + "gtk3" + ] + }, + "deb": { + "depends": [ + "libwebkit2gtk-4.1-0", + "libgtk-3-0" + ] + } } } } diff --git a/crates/wallet-app/tests/capabilities.rs b/crates/wallet-app/tests/capabilities.rs new file mode 100644 index 0000000..647f2e6 --- /dev/null +++ b/crates/wallet-app/tests/capabilities.rs @@ -0,0 +1,97 @@ +//! Every Tauri permission this app grants must say why. Capabilities are +//! TOML so a comment can sit above each permission; this test is the gate +//! that makes the comment mandatory. It also refuses any capability that is +//! not for the `main` window, and any capability without a description. + +use std::fs; +use std::path::Path; + +fn capability_files() -> Vec { + let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("capabilities"); + let mut files: Vec<_> = fs::read_dir(&dir) + .expect("capabilities directory exists") + .map(|e| e.expect("readable entry").path()) + .collect(); + files.sort(); + files +} + +#[test] +fn capabilities_are_toml_only() { + for f in capability_files() { + assert_eq!( + f.extension().and_then(|e| e.to_str()), + Some("toml"), + "{}: capabilities must be TOML so permissions can carry a reason", + f.display() + ); + } +} + +#[test] +fn every_capability_has_a_description_and_only_the_main_window() { + for f in capability_files() { + let text = fs::read_to_string(&f).expect("readable capability"); + let doc: toml::Value = toml::from_str(&text).expect("valid TOML"); + let description = doc + .get("description") + .and_then(|d| d.as_str()) + .unwrap_or(""); + assert!( + description.trim().len() >= 20, + "{}: a capability needs a description that says what it is for", + f.display() + ); + let windows: Vec<&str> = doc + .get("windows") + .and_then(|w| w.as_array()) + .map(|a| a.iter().filter_map(|v| v.as_str()).collect()) + .unwrap_or_default(); + assert_eq!( + windows, + ["main"], + "{}: only the main window is granted anything", + f.display() + ); + } +} + +#[test] +fn every_permission_carries_a_reason_on_the_line_above() { + for f in capability_files() { + let text = fs::read_to_string(&f).expect("readable capability"); + let lines: Vec<&str> = text.lines().collect(); + let mut in_permissions = false; + let mut seen = 0usize; + for (i, line) in lines.iter().enumerate() { + let t = line.trim(); + if t.starts_with("permissions") && t.contains('[') { + in_permissions = true; + continue; + } + if !in_permissions { + continue; + } + if t == "]" { + break; + } + if t.is_empty() || t.starts_with('#') { + continue; + } + seen += 1; + let above = i.checked_sub(1).map(|j| lines[j].trim()).unwrap_or(""); + assert!( + above.starts_with('#') && above.trim_start_matches('#').trim().len() >= 12, + "{} line {}: permission `{}` has no reason on the line above it", + f.display(), + i + 1, + t.trim_end_matches(',') + ); + } + assert!( + seen > 0, + "{}: no permissions found; the parser is confused", + f.display() + ); + } +}