From 22a6edc9167e607646a9d254baf1508791f7d5d4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 8 Nov 2026 10:17:58 -0800 Subject: [PATCH] Plug-ins: a module that lists no permissions ends the grant of its id Installing a module that lists no permissions under a granted id now forgets the grant, in the process and in grants.json, so the formerly approved bytes need a new approval. A refused install leaves the grant. Attaching a plug-in store forgets the grant of an id whose stored module lists no permissions. A plug-in folder load forgets the process's grant for such an id and leaves grants.json alone, because the store keeps the module its grant covers. --- crates/engine/src/cmd/plugin.rs | 8 +- crates/engine/src/store.rs | 15 ++- crates/engine/src/tests/plugins.rs | 141 +++++++++++++++++++++++++++++ crates/plugins/src/registry.rs | 7 ++ docs/mcp.md | 3 +- docs/plugins.md | 11 ++- docs/target-app-parity.md | 1 + 7 files changed, 178 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/plugin.rs b/crates/engine/src/cmd/plugin.rs index 6457e2cc9d..c8c6391487 100644 --- a/crates/engine/src/cmd/plugin.rs +++ b/crates/engine/src/cmd/plugin.rs @@ -336,7 +336,7 @@ fn grant_param(c: &str, p: &Value) -> Result>> { /// `allowHeavy` says the user agreed, and one that lists permissions the user hasn't granted this /// very module unless `grant` lists them all, and one whose SHA-256 isn't the `hash` given; records /// the grant with the module's hash, keeps it for the next start and installs it → its -/// `plugin.list` entry. +/// `plugin.list` entry. A module that lists no permissions ends any grant held for its id. fn install(s: &mut Session, p: &Value) -> Result { const C: &str = "plugin.install"; let (replace, allow_heavy) = (switch_or(p, "replace", true, C)?, switch_or(p, "allowHeavy", false, C)?); @@ -375,6 +375,10 @@ fn install(s: &mut Session, p: &Value) -> Result { let g = Grant { id: m.id.clone(), hash: plugin.hash().to_string(), permissions: wanted }; registry::grant(g.clone()); s.store_grant(&m.id, Some(g)); + } else { + // This module takes the id over, so nothing the user approved for the id applies any more. + registry::revoke(&m.id); + s.store_grant(&m.id, None); } // Installed plug-ins stay installed after a restart (#2541). s.store_plugin(plugin.id(), &bytes); @@ -527,7 +531,7 @@ pub fn specs() -> Vec { "Install Plug-in", [], None, - "{path: a .wasm file (desktop) | dataBase64: the module (+ name?: shown as its source), replace?: bool (true: a plug-in with the same id is replaced), name?: file name (with dataBase64), allowHeavy?: bool (true once the user agreed to a heavy budget: a filter whose manifest asks for one, budget \"heavy\" in plugin.inspect, installs only with it), grant?: [permission] (the permissions the user granted: a plug-in whose manifest lists permissions, needsApproval in plugin.inspect, installs only when grant holds every one of them; the grant is recorded with the module's hash and covers only that module, so a module with other bytes needs a new grant even when it lists the same permissions; with a plug-in store attached, only the grants kept there count), hash?: string (the module's SHA-256 as plugin.inspect gave it: a module with other bytes is refused)} validate and install a WebAssembly plug-in for this session → its plugin.list entry", + "{path: a .wasm file (desktop) | dataBase64: the module (+ name?: shown as its source), replace?: bool (true: a plug-in with the same id is replaced), name?: file name (with dataBase64), allowHeavy?: bool (true once the user agreed to a heavy budget: a filter whose manifest asks for one, budget \"heavy\" in plugin.inspect, installs only with it), grant?: [permission] (the permissions the user granted: a plug-in whose manifest lists permissions, needsApproval in plugin.inspect, installs only when grant holds every one of them; the grant is recorded with the module's hash and covers only that module, so a module with other bytes needs a new grant even when it lists the same permissions; installing a module that lists no permissions forgets the grant held for its id; with a plug-in store attached, only the grants kept there count), hash?: string (the module's SHA-256 as plugin.inspect gave it: a module with other bytes is refused)} validate and install a WebAssembly plug-in for this session → its plugin.list entry", always, install ), diff --git a/crates/engine/src/store.rs b/crates/engine/src/store.rs index 3d8259361b..14f9b21d94 100644 --- a/crates/engine/src/store.rs +++ b/crates/engine/src/store.rs @@ -21,7 +21,7 @@ //! The files are untrusted input like any other: sizes and counts are capped, a file that does not //! parse is skipped with a log record, and every write is atomic. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::io::Read; use std::path::{Path, PathBuf}; @@ -225,11 +225,13 @@ impl Session { /// Keep the plug-ins this session installs in `dir`, and install the ones already there (a /// module that doesn't load, and one that lists permissions the grants there don't grant that - /// very module, is skipped with a log record, and stays in `dir` with its grant). + /// very module, is skipped with a log record, and stays in `dir` with its grant). A grant for + /// an id whose module there lists no permissions is forgotten. pub fn attach_plugin_store(&mut self, dir: PathBuf) { let grants = stored_grants(&dir); let limits = vectorcraft_plugins::Limits::default(); let mut left = MAX_PLUGIN_STORE; + let mut asks_nothing = BTreeSet::new(); for path in list_files(&dir, PLUGIN_EXT) { if left == 0 { log::warn!("skipping the other stored plug-ins: they hold more than {MAX_PLUGIN_STORE} bytes together"); @@ -246,6 +248,7 @@ impl Session { let m = plugin.manifest(); match grants.get(&m.id).filter(|g| g.covers(&m.id, plugin.hash(), &m.permissions())) { Some(g) => { + asks_nothing.remove(&m.id); vectorcraft_plugins::registry::grant(g.clone()); vectorcraft_plugins::registry::install(plugin.with_source(path.display().to_string())); } @@ -253,12 +256,20 @@ impl Session { } } Ok(plugin) => { + asks_nothing.insert(plugin.id().to_string()); vectorcraft_plugins::registry::install(plugin.with_source(path.display().to_string())); } Err(e) => log::warn!("skipping the stored plug-in {}: {e}", path.display()), } } + let kept = read_grants(&dir).unwrap_or_default(); self.plugin_dir = Some(dir); + for id in &asks_nothing { + vectorcraft_plugins::registry::revoke(id); + if kept.grants.contains_key(id) || kept.other.iter().any(|e| e.get("id").and_then(serde_json::Value::as_str) == Some(id.as_str())) { + self.store_grant(id, None); + } + } } /// Whether `plugin` installs without asking the user: it lists no permissions, or this very diff --git a/crates/engine/src/tests/plugins.rs b/crates/engine/src/tests/plugins.rs index 7d0983ef2d..5425b9d311 100644 --- a/crates/engine/src/tests/plugins.rs +++ b/crates/engine/src/tests/plugins.rs @@ -634,6 +634,147 @@ fn a_grant_covers_only_the_module_that_was_approved() { run(&mut s, "plugin.remove", json!({"id": exempt})); } +/// Installing a module that lists no permissions under a granted id forgets the grant, so the +/// formerly approved bytes need a new approval. A refused install leaves the grant, and a module +/// that lists permissions keeps replacing it as before. +#[test] +fn a_module_without_permissions_ends_the_grant_of_its_id() { + use vectorcraft_plugins::permissions::sha256_hex; + let mut s = session(); + let id = "test.engine.permissions.replaced"; + let empty = json!({"objects": []}); + let approved = asking(id, r#"["doc.read"]"#, &empty); + let approved_data = vectorcraft_format::base64_encode(&approved); + let granted = || vectorcraft_plugins::registry::granted(id).map(|g| g.hash); + for permissions in ["null", "[]"] { + let plain = vectorcraft_format::base64_encode(&asking(id, permissions, &empty)); + run(&mut s, "plugin.install", json!({"dataBase64": approved_data, "grant": ["doc.read"]})); + // Refused installs change nothing. + let e = s.execute("plugin.install", &json!({"dataBase64": plain, "hash": "0".repeat(64)})).unwrap_err().to_string(); + assert!(e.contains("SHA-256 `hash`"), "{e}"); + let e = s.execute("plugin.install", &json!({"dataBase64": plain, "replace": false})).unwrap_err().to_string(); + assert!(e.contains("already installed"), "{e}"); + assert_eq!(granted(), Some(sha256_hex(&approved)), "{permissions}: a refused install leaves the grant"); + + run(&mut s, "plugin.install", json!({"dataBase64": plain})); + assert_eq!(granted(), None, "{permissions}"); + assert_eq!(run(&mut s, "plugin.inspect", json!({"dataBase64": approved_data}))["needsApproval"], true, "{permissions}"); + let e = s.execute("plugin.install", &json!({"dataBase64": approved_data})).unwrap_err().to_string(); + assert!(e.contains("`grant`"), "{permissions}: {e}"); + assert!(!vectorcraft_plugins::registry::get(id).unwrap().manifest().asks_permissions(), "{permissions}: the module installed stays"); + } + // With no module installed under the id, the grant ends the same way. + run(&mut s, "plugin.install", json!({"dataBase64": approved_data, "grant": ["doc.read"]})); + vectorcraft_plugins::registry::remove(id); + run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&asking(id, "null", &empty))})); + assert_eq!(granted(), None); + // A module that lists permissions records its own grant. + run(&mut s, "plugin.install", json!({"dataBase64": approved_data, "grant": ["doc.read"]})); + assert_eq!(granted(), Some(sha256_hex(&approved))); + run(&mut s, "plugin.remove", json!({"id": id})); +} + +/// In a plug-in store, a module that lists no permissions removes only its own id's entry from +/// `grants.json`, and the formerly approved bytes need a new approval after a restart. A store +/// that holds such a module and a grant for its id has that grant forgotten when it is attached. +#[test] +fn a_module_without_permissions_ends_the_grant_kept_in_the_store() { + use vectorcraft_plugins::permissions::{Grant, Permission, sha256_hex}; + let folder = crate::tests_links::Folder::new("plugin-store-stale-grant"); + let id = "test.engine.permissions.storedstale"; + let other = "test.engine.permissions.storedother"; + let empty = json!({"objects": []}); + let approved = asking(id, r#"["doc.read"]"#, &empty); + let approved_data = vectorcraft_format::base64_encode(&approved); + let plain = asking(id, "null", &empty); + let other_wasm = asking(other, r#"["doc.read"]"#, &empty); + let grants = folder.0.join("grants.json"); + let ids = || -> Vec { + serde_json::from_slice::>(&std::fs::read(&grants).unwrap()) + .unwrap() + .iter() + .map(|e| e["id"].as_str().unwrap().to_string()) + .collect() + }; + let mut s = session(); + s.attach_plugin_store(folder.0.clone()); + run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&other_wasm), "grant": ["doc.read"]})); + run(&mut s, "plugin.install", json!({"dataBase64": approved_data, "grant": ["doc.read"]})); + assert_eq!(ids().len(), 2); + run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&plain)})); + assert_eq!(ids(), vec![other.to_string()], "only this id's entry goes"); + assert!(vectorcraft_plugins::registry::granted(id).is_none()); + assert!(vectorcraft_plugins::registry::granted(other).is_some(), "the other plug-in's grant stays"); + + vectorcraft_plugins::registry::remove(id); + let mut next = session(); + next.attach_plugin_store(folder.0.clone()); + assert_eq!(run(&mut next, "plugin.inspect", json!({"dataBase64": approved_data}))["needsApproval"], true); + + // A grant the store already holds for an id whose stored module lists no permissions. + let entry = json!({"id": id, "hash": sha256_hex(&approved), "permissions": ["doc.read"]}); + let other_entry = json!({"id": other, "hash": sha256_hex(&other_wasm), "permissions": ["doc.read"]}); + std::fs::write(&grants, serde_json::to_vec(&json!([entry, other_entry])).unwrap()).unwrap(); + vectorcraft_plugins::registry::remove(id); + let mut third = session(); + third.attach_plugin_store(folder.0.clone()); + assert!(vectorcraft_plugins::registry::get(id).is_some(), "the module that lists nothing still loads"); + assert_eq!(ids(), vec![other.to_string()]); + assert!(vectorcraft_plugins::registry::get(other).is_some()); + + // So does an entry this version can't read, and a grant held only by the process. + let future = json!({"id": id, "hash": "00", "permissions": ["doc.read", "ui.panel"]}); + std::fs::write(&grants, serde_json::to_vec(&json!([future, other_entry])).unwrap()).unwrap(); + vectorcraft_plugins::registry::remove(id); + vectorcraft_plugins::registry::grant(Grant { id: id.into(), hash: sha256_hex(&approved), permissions: vec![Permission::DocRead] }); + let mut fourth = session(); + fourth.attach_plugin_store(folder.0.clone()); + assert_eq!(ids(), vec![other.to_string()]); + assert!(vectorcraft_plugins::registry::granted(id).is_none()); + run(&mut fourth, "plugin.remove", json!({"id": id})); + run(&mut fourth, "plugin.remove", json!({"id": other})); +} + +/// A plug-in folder that leaves a module listing no permissions under an id forgets the process's +/// grant for it, unless a later file in the folder leaves a granted module there. A grant kept in +/// the plug-in store stays, with the stored module it covers. +#[test] +fn a_plugin_folder_ends_the_process_grant_of_an_id_it_replaces() { + let mut s = session(); + let id = "test.engine.permissions.folderstale"; + let empty = json!({"objects": []}); + let approved = asking(id, r#"["doc.read"]"#, &empty); + let approved_data = vectorcraft_format::base64_encode(&approved); + let plain = asking(id, "null", &empty); + let dir = crate::tests_links::Folder::new("plugin-folder-stale-grant"); + std::fs::write(dir.0.join("a-plain.wasm"), &plain).unwrap(); + std::fs::write(dir.0.join("b-approved.wasm"), &approved).unwrap(); + let path = json!({"path": dir.0.display().to_string()}); + run(&mut s, "plugin.install", json!({"dataBase64": approved_data, "grant": ["doc.read"]})); + + let r = run(&mut s, "plugin.reload", path.clone()); + assert_eq!(r["failed"], json!([]), "{r}"); + assert!(vectorcraft_plugins::registry::granted(id).is_some(), "the granted module is the one left"); + + std::fs::remove_file(dir.0.join("b-approved.wasm")).unwrap(); + let r = run(&mut s, "plugin.reload", path.clone()); + assert_eq!(r["loaded"], json!([id]), "{r}"); + assert!(vectorcraft_plugins::registry::granted(id).is_none()); + assert_eq!(run(&mut s, "plugin.inspect", json!({"dataBase64": approved_data}))["needsApproval"], true); + run(&mut s, "plugin.remove", json!({"id": id})); + + let store = crate::tests_links::Folder::new("plugin-folder-stale-grant-store"); + let mut stored = session(); + stored.attach_plugin_store(store.0.clone()); + run(&mut stored, "plugin.install", json!({"dataBase64": approved_data, "grant": ["doc.read"]})); + let grants = store.0.join("grants.json"); + let recorded = std::fs::read(&grants).unwrap(); + run(&mut stored, "plugin.reload", path); + assert_eq!(std::fs::read(&grants).unwrap(), recorded, "the store still holds the module its grant covers"); + assert_eq!(run(&mut stored, "plugin.inspect", json!({"dataBase64": approved_data}))["needsApproval"], false); + run(&mut stored, "plugin.remove", json!({"id": id})); +} + /// A stored module whose bytes changed is skipped at the next start and stays in the store with its /// grant, and the original bytes load again. A stored grant whose hash is malformed covers nothing. /// With a store attached, a matching grant held only by the process doesn't count. diff --git a/crates/plugins/src/registry.rs b/crates/plugins/src/registry.rs index 8e4635c3fa..986a30e172 100644 --- a/crates/plugins/src/registry.rs +++ b/crates/plugins/src/registry.rs @@ -133,6 +133,8 @@ pub fn load_folder(dir: &std::path::Path) -> Result { /// [`load_folder`], loading only the files `allow` accepts, and of the plug-ins that list /// permissions only those `approve` accepts; the others are reported as failed with the reason. +/// When the module a load leaves under an id lists no permissions, the process's grant for that id +/// is forgotten. #[cfg(not(target_arch = "wasm32"))] pub fn load_folder_where( dir: &std::path::Path, @@ -166,5 +168,10 @@ pub fn load_folder_where( Err(e) => report.failed.push((f.display().to_string(), e.to_string())), } } + for id in &report.loaded { + if get(id).is_some_and(|p| !p.manifest().asks_permissions()) { + revoke(id); + } + } Ok(report) } diff --git a/docs/mcp.md b/docs/mcp.md index 71e7978bd6..d83db18ca0 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -6176,7 +6176,8 @@ the entry's `hash` as `hash` so that a module that changed since is refused. Wit an error naming the permissions needed. The grant is kept with the module's hash and covers only that module: the same bytes install again without `grant`, and a module with other bytes has `needsApproval: true` and needs a new `grant` even when it lists the same permissions. `plugin.install` does not check that a person approved `grant`: it -trusts the client that sends it. `plugin.remove` forgets the grant. A +trusts the client that sends it. `plugin.remove` forgets the grant, and so does installing a module that lists no +permissions under that id: the earlier bytes then need a new `grant`. A filter without `doc.write` or `state` that returns output needing them gets an error and the document is unchanged. A plug-in of API level 2 (`api: 2` in its entry) that lists `progress` may report its progress, see Cancel and write log lines: `notifications/cancelled` makes its `vc_cancelled` return 1, and its log lines go to the server's log diff --git a/docs/plugins.md b/docs/plugins.md index 80544ad2bc..2865f1dbfe 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -137,9 +137,14 @@ capability model proposed for PhotoCraft plug-ins `Plug-ins` folder, and covers only that module. Installing the same bytes again needs no new approval. A module with other bytes asks again, even when it lists the same permissions. Until it is approved, the installed module and its grant stay as they are; once it is, the grant records the new hash. A grant whose hash is not 64 lower-case - hexadecimal digits covers nothing. `plugin.remove` forgets the grant. An entry of `grants.json` that this version can't use (a - permission it doesn't know, an invalid id) is skipped and stays in the file until its plug-in is granted again or - removed. A `grants.json` that is not a JSON list is never overwritten, and the file is never written larger than + hexadecimal digits covers nothing. `plugin.remove` forgets the grant, and so does a successful `plugin.install` of a + module that lists no permissions under that id, so the earlier bytes need a new approval. A refused install leaves + the grant as it is. A stored module that lists no permissions has any grant for its id forgotten when the `Plug-ins` + folder is attached. The Additional Plug-ins Folder and `plugin.reload` do not change the `Plug-ins` folder: when they + leave a module that lists no permissions under an id, the process forgets its own grant for that id, and a grant + kept in `grants.json` stays with the stored module it covers. An entry of `grants.json` that this version can't use (a + permission it doesn't know, an invalid id) is skipped and stays in the file until its plug-in is granted again, + replaced by a module that lists no permissions, or removed. A `grants.json` that is not a JSON list is never overwritten, and the file is never written larger than 1 MiB, the most that is read. When a `Plug-ins` folder is in use, only the grants kept in it count for `plugin.install`, `plugin.inspect`, `plugin.reload` and the Additional Plug-ins Folder. At startup, a stored plug-in is skipped when no grant covers its bytes and the permissions it lists. The Additional Plug-ins diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 816df8d0f3..bb6e532f26 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1505,6 +1505,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - **Plug-in instruction budget at a bulk memory instruction:** a run that pauses where one slice of its instruction budget ends keeps the instructions the slice still held. A `memory.fill` or `memory.copy` that the slice can't pay for in full runs on the next slice, so a run whose budget covers its work finishes; a run ends with `Limit("instruction budget")` only when the instructions it has left can't pay for the next instruction. Regressions: `crates/plugins/tests/abi.rs`. Scores and hours unchanged. - **Plug-in permissions (G17):** a manifest may list `permissions` (`doc.read`, `doc.write`, `state`, `effect.render`, `export.write`; the first three follow storytold/photocraft#662) and an `api` level; without the list a plug-in gets what its kind implies and installs as before. Install Plug-in… and File › Open show the name, id, version, file SHA-256 and each permission in plain words, with control and bidirectional characters escaped, and install nothing on Cancel; `plugin.install` needs `grant`, and folders install only granted plug-ins. The grant is kept with the module's hash, a module asking for more asks again, and a filter run refuses output its permissions don't allow. Regressions: `crates/plugins/src/permissions.rs`, `crates/plugins/src/manifest.rs`, `crates/engine/src/tests/plugins.rs`, `crates/engine/tests/plugin_fuzz.rs`, `crates/ui-egui/src/tests/plugins.rs`. Scores and hours unchanged. - **Plug-in grants cover one module (G17):** a grant counts only for the module with the id and SHA-256 it records. A module with other bytes under the same id needs a new approval, even with the same permissions: `plugin.inspect` reports `needsApproval`, `plugin.install` refuses it without `grant`, Install Plug-in… and File › Open ask again, and the plug-in store and plug-in folders skip it. A grant with a malformed hash covers nothing. Regressions: `crates/plugins/src/permissions.rs`, `crates/engine/src/tests/plugins.rs`, `crates/ui-egui/src/tests/plugins.rs`. Scores and hours unchanged. +- **A plug-in with no permissions ends its id's grant (G17):** installing a module that lists no permissions under a granted id forgets the grant, in the process and in `grants.json`, so the formerly approved bytes need a new approval. Attaching a `Plug-ins` folder whose module for that id lists no permissions forgets it too, and plug-in folders forget the process's grant. A refused install leaves the grant. Regressions: `crates/engine/src/tests/plugins.rs`. Scores and hours unchanged. - **Plug-in progress, Cancel and log lines (G17):** at plug-in API level 2 a plug-in with the `progress` permission imports `env.vc_progress`, `env.vc_cancelled` and `env.vc_log`. A heavy filter's and an export's progress windows show its progress as a percentage bar, their Cancel reaches it through `vc_cancelled`, and its log lines go to the app's log with its id, capped per line and per run. Installing checks every import and names the one it refuses. Regressions: `crates/plugins/src/host.rs`, `crates/plugins/tests/abi.rs`, `crates/engine/src/tests/plugins.rs`, `crates/engine/tests/plugin_fuzz.rs`, `crates/ui-egui/src/tests/plugin_runs.rs`. Scores and hours unchanged. - **Plug-in effects after a plug-in changes:** when a plug-in is installed for the first time, installed again or removed (`plugin.install`, `plugin.reload`, `plugin.remove`, File › Open of a `.wasm` file, the Additional Plug-ins Folder preference), the next frame the canvas draws uses the plug-ins then installed for objects with a plug-in effect and their raster effects, for the blends, envelopes and repeats that show such objects, and for pattern tiles and symbol instances whose art holds one.