diff --git a/Cargo.lock b/Cargo.lock index 59e5fec9bb..ee8426623f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7057,6 +7057,7 @@ dependencies = [ "craft-image-codecs", "craft-vectorcraft-format", "log", + "rustix 1.1.6", "serde", "serde_json", "sha2", diff --git a/crates/engine/src/cmd/plugin.rs b/crates/engine/src/cmd/plugin.rs index 5570dc6a81..2a1b8c8b40 100644 --- a/crates/engine/src/cmd/plugin.rs +++ b/crates/engine/src/cmd/plugin.rs @@ -1044,7 +1044,7 @@ fn load_folder(s: &Session, folder: &str) -> Value { if r.disabled_changed { s.store_disabled_plugins_in(&turn); } - json!({"folder": folder, "loaded": r.loaded, "failed": r.failed.iter().map(|(f, e)| json!({"file": f, "error": e})).collect::>()}) + json!({"folder": folder, "loaded": r.loaded, "failed": r.failed.iter().map(|(f, e)| json!({"file": f, "error": e})).collect::>(), "leftOut": r.left_out}) } Err(e) => json!({"folder": folder, "error": e.to_string()}), } @@ -1270,7 +1270,7 @@ pub fn specs() -> Vec { "List Plug-ins", [], None, - "{} → {plugins: [{id, name, version, kind: \"filter\"|\"effect\"|\"exporter\"|\"importer\", author, description, params (doc), paramsSchema, effect? (the effect id of a live effect: plugin.), format? and extensions? (an exporter's or importer's format id, plugin., for document.export or document.open's importer, and its file extensions), budget?: \"heavy\" (a filter that may compute for longer, see plugin.install's allowHeavy), inputs?: [\"type\"|\"image\"|\"symbolInstance\"] (the objects besides paths a filter reads, see plugin.run), size, source, permissions: [name] (what it may do: the ones its manifest lists, else the ones its kind implies; progress: it may report its progress, see Cancel and write log lines; settings: it may keep settings of its own; cache: it may keep a cache of its own; commands: a filter may run the commands its commandAllowlist names, see docs/plugins.md), commandAllowlist?: [command id] (the commands a filter with the commands permission may run while it runs, with vc_call), hash (the module's SHA-256), api (the plug-in API level it was built for, 1 or 2), paintOutput?: true (a live effect whose pieces have paints of their own, see docs/plugins.md), cacheBytes (the bytes its cache holds; see plugin.clearCache), commands?: [{id, command (its command id, plugin.., which runs it), label, params (doc), paramsSchema}] (the menu commands a filter or live effect adds)}], folder: the Additional Plug-ins Folder load report {folder, loaded, failed: [{file, error}]} | null (under automation read roots, a folder outside them is null, and a failed file and a plug-in's source outside them are named by their file name), safeMode: bool (no plug-in loads or runs, see plugin.safeMode), disabled: [{id, file, name, label}] (turned off for the next launch, see plugin.setEnabled)}", + "{} → {plugins: [{id, name, version, kind: \"filter\"|\"effect\"|\"exporter\"|\"importer\", author, description, params (doc), paramsSchema, effect? (the effect id of a live effect: plugin.), format? and extensions? (an exporter's or importer's format id, plugin., for document.export or document.open's importer, and its file extensions), budget?: \"heavy\" (a filter that may compute for longer, see plugin.install's allowHeavy), inputs?: [\"type\"|\"image\"|\"symbolInstance\"] (the objects besides paths a filter reads, see plugin.run), size, source, permissions: [name] (what it may do: the ones its manifest lists, else the ones its kind implies; progress: it may report its progress, see Cancel and write log lines; settings: it may keep settings of its own; cache: it may keep a cache of its own; commands: a filter may run the commands its commandAllowlist names, see docs/plugins.md), commandAllowlist?: [command id] (the commands a filter with the commands permission may run while it runs, with vc_call), hash (the module's SHA-256), api (the plug-in API level it was built for, 1 or 2), paintOutput?: true (a live effect whose pieces have paints of their own, see docs/plugins.md), cacheBytes (the bytes its cache holds; see plugin.clearCache), commands?: [{id, command (its command id, plugin.., which runs it), label, params (doc), paramsSchema}] (the menu commands a filter or live effect adds)}], folder: the Additional Plug-ins Folder load report {folder, loaded, failed: [{file, error}], leftOut} | null (under automation read roots, a folder outside them is null, and a failed file and a plug-in's source outside them are named by their file name), safeMode: bool (no plug-in loads or runs, see plugin.safeMode), disabled: [{id, file, name, label}] (turned off for the next launch, see plugin.setEnabled)}", always, list ), @@ -1333,7 +1333,7 @@ pub fn specs() -> Vec { "Reload Plug-ins", ["Object", "Plug-ins"], None, - "{path?: a folder (default: the Additional Plug-ins Folder preference)} install every *.wasm in it (desktop; a bad module, and one disabled with plugin.setEnabled, is reported and skipped, and so are a heavy filter and a plug-in that lists permissions the user hasn't granted this very module, its bytes included; an error in Safe Mode) → {folder, loaded: [id], failed: [{file, error}]}", + "{path?: a folder (default: the Additional Plug-ins Folder preference)} install every *.wasm in it, the first 1,000 by file name (desktop; a bad module, one that isn't a regular file, and one disabled with plugin.setEnabled, is reported and skipped, and so are a heavy filter and a plug-in that lists permissions the user hasn't granted this very module, its bytes included; an error in Safe Mode) → {folder, loaded: [id], failed: [{file, error}], leftOut: the count of *.wasm files past the first 1,000, not tried}", always, reload ), diff --git a/crates/plugins/Cargo.toml b/crates/plugins/Cargo.toml index 28b972d679..1f388b85b0 100644 --- a/crates/plugins/Cargo.toml +++ b/crates/plugins/Cargo.toml @@ -27,5 +27,10 @@ wasmi = { workspace = true } # the build through eframe. web-time = "1" +# Opens a `.wasm` in the plug-ins folder without waiting when it is a named pipe (`registry.rs`). +# Already in Cargo.lock, so this adds no new crate. +[target.'cfg(unix)'.dependencies] +rustix = { workspace = true, features = ["std", "fs"] } + [dev-dependencies] wat = { workspace = true } diff --git a/crates/plugins/src/registry.rs b/crates/plugins/src/registry.rs index 1178f7c9ac..6d20452ce6 100644 --- a/crates/plugins/src/registry.rs +++ b/crates/plugins/src/registry.rs @@ -182,20 +182,75 @@ pub fn intern(s: &str) -> &'static str { /// and loads it. #[cfg(not(target_arch = "wasm32"))] pub fn load_file(path: &std::path::Path, limits: Limits) -> Result { - use std::io::Read; - let meta = std::fs::metadata(path).map_err(|e| Error::Io(format!("{}: {e}", path.display())))?; - if !meta.is_file() { - return Err(Error::Io(format!("{} is not a file", path.display()))); - } - if meta.len() > limits.max_module_bytes as u64 { - return Err(Error::Module(format!("{} is {} bytes (limit {})", path.display(), meta.len(), limits.max_module_bytes))); - } - let f = std::fs::File::open(path).map_err(|e| Error::Io(format!("{}: {e}", path.display())))?; - let mut bytes = Vec::new(); - f.take(limits.max_module_bytes as u64 + 1).read_to_end(&mut bytes).map_err(|e| Error::Io(format!("{}: {e}", path.display())))?; + let bytes = read_module(path, &limits, || {})?; Ok(Plugin::load(&bytes, limits)?.with_source(path.display().to_string())) } +/// The bytes of the module file at `path`. A path that doesn't name a regular file is refused +/// before it is opened, and so is one over the module limit. On Unix the file is opened without +/// waiting, so a named pipe put in its place after that check doesn't hold the load until something +/// writes to it; what was opened is checked again and refused the same way. `opening` runs between +/// the first check and the open. +#[cfg(not(target_arch = "wasm32"))] +fn read_module(path: &std::path::Path, limits: &Limits, opening: impl FnOnce()) -> Result> { + use std::io::Read; + let io = |e: std::io::Error| Error::Io(format!("{}: {e}", path.display())); + let regular = |meta: std::fs::Metadata| { + if !meta.is_file() { + return Err(Error::Io(format!("{} is not a file", path.display()))); + } + if meta.len() > limits.max_module_bytes as u64 { + return Err(Error::Module(format!("{} is {} bytes (limit {})", path.display(), meta.len(), limits.max_module_bytes))); + } + Ok(()) + }; + regular(std::fs::metadata(path).map_err(io)?)?; + opening(); + let f = open_module(path).map_err(io)?; + regular(f.metadata().map_err(io)?)?; + let mut bytes = Vec::new(); + f.take(limits.max_module_bytes as u64 + 1).read_to_end(&mut bytes).map_err(io)?; + Ok(bytes) +} + +/// Opens the module file at `path` for reading. On Unix the open doesn't wait for a writer when +/// the path names a named pipe, and doesn't make a terminal device the controlling terminal. +#[cfg(not(target_arch = "wasm32"))] +fn open_module(path: &std::path::Path) -> std::io::Result { + let mut open = std::fs::OpenOptions::new(); + open.read(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::custom_flags(&mut open, (rustix::fs::OFlags::NONBLOCK | rustix::fs::OFlags::NOCTTY).bits() as i32); + open.open(path) +} + +/// The most `.wasm` files one [`load_folder`] reads. +#[cfg(not(target_arch = "wasm32"))] +const MAX_FOLDER_FILES: usize = 1000; + +/// The first [`MAX_FOLDER_FILES`] `*.wasm` paths directly in `dir` by file name, sorted, and how +/// many more there are. Only that many paths are held while the folder is listed. An error while +/// the folder is listed is returned, as one opening it is. +#[cfg(not(target_arch = "wasm32"))] +fn folder_files(dir: &std::path::Path) -> Result<(Vec, usize)> { + let io = |e: std::io::Error| Error::Io(format!("{}: {e}", dir.display())); + let rd = std::fs::read_dir(dir).map_err(io)?; + let mut kept = std::collections::BinaryHeap::new(); + let mut more = 0; + for entry in rd { + let p = entry.map_err(io)?.path(); + if !p.extension().is_some_and(|x| x.eq_ignore_ascii_case("wasm")) { + continue; + } + kept.push(p); + if kept.len() > MAX_FOLDER_FILES { + kept.pop(); + more += 1; + } + } + Ok((kept.into_sorted_vec(), more)) +} + /// What [`load_folder`] reports for a plug-in the user disabled. pub const DISABLED: &str = "disabled: turn it on again in Help › Safe Mode, for the next launch"; @@ -204,18 +259,22 @@ pub const DISABLED: &str = "disabled: turn it on again in Help › Safe Mode, fo pub struct FolderReport { /// Ids of the plug-ins installed. pub loaded: Vec, - /// `(file, error)` for each `.wasm` that failed to load. + /// `(file, error)` for each `.wasm` that failed to load, including one that isn't a regular file. pub failed: Vec<(String, String)>, + /// How many `.wasm` files were not tried because the folder holds more than a load reads. + pub left_out: usize, /// The disabled items saved for the next launch changed: a plug-in disabled by its id alone was /// found in a file, which is now disabled too ([`crate::safe_mode::disable_file_of`]). The app /// saves the list again. pub disabled_changed: bool, } -/// Installs every `*.wasm` directly in `dir` (not recursive), in file-name order. A bad module is -/// reported and skipped, the others still load; so is a heavy one ([`crate::Budget::Heavy`]), -/// which installs only once the user agrees, and one that lists permissions the user has not -/// granted this very module ([`approved`]). +/// Installs every `*.wasm` directly in `dir` (not recursive), in file-name order, up to the first +/// 1,000 by file name; the report counts the files left out ([`FolderReport::left_out`]). Anything +/// that isn't a regular file, such as a named pipe, is reported and skipped without waiting on it +/// ([`load_file`]). A bad module is reported and skipped, the others still load; so is a heavy one +/// ([`crate::Budget::Heavy`]), which installs only once the user agrees, and one that lists +/// permissions the user has not granted this very module ([`approved`]). #[cfg(not(target_arch = "wasm32"))] pub fn load_folder(dir: &std::path::Path) -> Result { load_folder_where(dir, |_| Ok(()), approved) @@ -237,12 +296,8 @@ pub fn load_folder_where( ) -> Result { use crate::safe_mode::{self, Item}; safe_mode::check()?; - let rd = std::fs::read_dir(dir).map_err(|e| Error::Io(format!("{}: {e}", dir.display())))?; - let mut files: Vec = - rd.filter_map(|e| e.ok().map(|e| e.path())).filter(|p| p.extension().is_some_and(|x| x.eq_ignore_ascii_case("wasm"))).collect(); - files.sort(); - files.truncate(1000); - let mut report = FolderReport::default(); + let (files, left_out) = folder_files(dir)?; + let mut report = FolderReport { left_out, ..FolderReport::default() }; for f in files { if let Err(why) = allow(&f) { report.failed.push((f.display().to_string(), why)); @@ -453,4 +508,114 @@ mod tests { assert!(granted(id).is_none(), "a module without permissions forgets the grant"); assert!(remove(id)); } + + /// An empty folder for one test, with anything an earlier run left there removed. + #[cfg(not(target_arch = "wasm32"))] + fn test_dir(name: &str) -> std::path::PathBuf { + let dir = std::env::temp_dir().join(format!("vc-registry-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + /// Makes a named pipe at `path`. + #[cfg(unix)] + fn make_pipe(path: &std::path::Path) { + assert!(std::process::Command::new("mkfifo").arg(path).status().unwrap().success(), "mkfifo {}", path.display()); + } + + /// Limits that let a test go past the module size with a few bytes. + #[cfg(not(target_arch = "wasm32"))] + fn small_limits() -> Limits { + Limits { max_module_bytes: 16, ..Limits::default() } + } + + /// A module file is opened so that a named pipe in its place doesn't make the open wait for a + /// writer. The test holds the pipe open for reading and writing itself, so the open returns + /// either way. + #[cfg(unix)] + #[test] + fn a_module_file_is_opened_without_waiting_for_a_writer() { + let dir = test_dir("open"); + let pipe = dir.join("x.wasm"); + make_pipe(&pipe); + let held = std::fs::OpenOptions::new().read(true).write(true).open(&pipe).unwrap(); + let opened = open_module(&pipe).unwrap(); + let flags = rustix::fs::fcntl_getfl(&opened).unwrap(); + drop((opened, held)); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(flags.contains(rustix::fs::OFlags::NONBLOCK), "the open would wait for a writer: {flags:?}"); + } + + /// A `.wasm` that a named pipe replaces after it was checked is refused as not a file, even + /// when the pipe holds more bytes than the module limit allows. + #[cfg(unix)] + #[test] + fn a_file_replaced_by_a_pipe_is_refused() { + use std::io::Write; + let dir = test_dir("pipe"); + let path = dir.join("x.wasm"); + let pipe = dir.join("pipe"); + std::fs::write(&path, b"\0asm").unwrap(); + make_pipe(&pipe); + let mut held = std::fs::OpenOptions::new().read(true).write(true).open(&pipe).unwrap(); + held.write_all(&[0; 32]).unwrap(); + let read = read_module(&path, &small_limits(), || std::fs::rename(&pipe, &path).unwrap()); + drop(held); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(matches!(&read, Err(Error::Io(e)) if e.ends_with("is not a file")), "{read:?}"); + } + + /// A `.wasm` that grows past the module limit after it was checked is refused by its size. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_file_that_grows_after_the_check_is_refused() { + let dir = test_dir("grow"); + let path = dir.join("x.wasm"); + std::fs::write(&path, b"\0asm").unwrap(); + let read = read_module(&path, &small_limits(), || std::fs::write(&path, [0; 32]).unwrap()); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(matches!(&read, Err(Error::Module(e)) if e.contains("limit 16")), "{read:?}"); + } + + /// A folder load reports a subfolder, a named pipe and a link to a named pipe with `.wasm` + /// names as not files, and goes on. + #[cfg(unix)] + #[test] + fn a_folder_reports_entries_that_are_not_files() { + let _lock = crate::safe_mode::TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + crate::safe_mode::set_active(None); + let dir = test_dir("special"); + std::fs::create_dir(dir.join("a.wasm")).unwrap(); + make_pipe(&dir.join("b.wasm")); + std::os::unix::fs::symlink(dir.join("b.wasm"), dir.join("c.wasm")).unwrap(); + let r = load_folder_where(&dir, |_| Ok(()), |_| false); + std::fs::remove_dir_all(&dir).unwrap(); + let r = r.unwrap(); + let failed: Vec<(String, bool)> = r.failed.iter().map(|(f, e)| (f.clone(), e.ends_with("is not a file"))).collect(); + let expected: Vec<(String, bool)> = ["a", "b", "c"].iter().map(|n| (dir.join(format!("{n}.wasm")).display().to_string(), true)).collect(); + assert_eq!(failed, expected, "{r:?}"); + assert!(r.loaded.is_empty() && r.left_out == 0, "{r:?}"); + } + + /// A folder with more `.wasm` files than a load reads tries the first ones by file name and + /// counts the files it left out. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_folder_over_the_file_limit_reports_the_files_left_out() { + let _lock = crate::safe_mode::TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + crate::safe_mode::set_active(None); + let dir = test_dir("limit"); + // Written last first, so the listing's order is not the file-name order. + for i in (0..MAX_FOLDER_FILES + 2).rev() { + std::fs::write(dir.join(format!("{i:05}.wasm")), b"").unwrap(); + } + let r = load_folder_where(&dir, |_| Ok(()), |_| false).unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(r.loaded.is_empty(), "{r:?}"); + assert_eq!(r.left_out, 2, "{r:?}"); + let tried: Vec = r.failed.iter().map(|(f, _)| f.clone()).collect(); + let first: Vec = (0..MAX_FOLDER_FILES).map(|i| dir.join(format!("{i:05}.wasm")).display().to_string()).collect(); + assert_eq!(tried, first, "the first files by name, in that order"); + } } diff --git a/docs/plugins.md b/docs/plugins.md index 09bb7c8dbd..5265255845 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -34,7 +34,7 @@ in `crates/engine/src/cmd/plugin.rs`). | `plugin.inspect` | as `plugin.install`: `{"path"}` or `{"dataBase64", "name"?}` | Validates a module without installing it; returns the `plugin.list` entry it would have, plus `needsApproval` (true when installing it needs a person's approval), `needsReplace` (true when `plugin.install` needs `replace: true`), and the `commandAllowlist` ids no command has (`unknownCommands`) and those no plug-in may call (`refusedCommands`). | | `plugin.remove` | `{"id"}` | Uninstalls, and deletes it from the `Plug-ins` folder with its [settings and cache](#settings-and-cache). | | `plugin.clearCache` | `{"id"?}` | Deletes every entry in the [cache](#settings-and-cache) of plug-in `id`, or of every installed plug-in; settings stay. Returns `{"cleared": [id], "bytesFreed"}`. | -| `plugin.reload` | `{"path"?: "folder"}` (default: the preference folder) | Installs every `*.wasm` in a folder; a bad module is reported and skipped, and so are a heavy filter and a plug-in whose permissions the user hasn't granted (nobody was asked). | +| `plugin.reload` | `{"path"?: "folder"}` (default: the preference folder) | Installs every `*.wasm` in a folder, the first 1,000 by file name; a bad module is reported and skipped, and so are an entry that is not a regular file, a heavy filter and a plug-in whose permissions the user hasn't granted (nobody was asked). Returns `{"folder", "loaded", "failed": [{file, error}], "leftOut"}`; `leftOut` counts the `*.wasm` files past the first 1,000, which were not tried. | Live effects are applied like any effect: `effect.apply {"effect": "plugin.", "params": {…}}`, edited with `effect.setParams`, listed by `effect.list` (menu `Effect › Plug-ins`) and baked by Object › Expand Appearance. @@ -108,7 +108,10 @@ replaced by a module without it. **Preference folder.** Edit › Preferences › Plug-ins & Scratch Disks › *Additional Plug-ins Folder*: when set, every `*.wasm` in that folder (not recursive) is installed at startup and whenever the preference changes (desktop builds; the web build installs from bytes). On the desktop, Choose… beside the field picks the folder; the dialog refuses a -picked or typed folder whose contents can't be listed, and `prefs.set` refuses such a folder too. +picked or typed folder whose contents can't be listed, and `prefs.set` refuses such a folder too. A folder load reads at +most 1,000 `.wasm` files, the first ones by file name (VectorCraft's own limit); its report counts the files left out +(`leftOut`). An entry that is not a regular file, such as a subfolder or a named pipe with a `.wasm` name, is listed in +the report and skipped, and the load does not wait on a named pipe. Plug-ins are installed **per process**, not per document. Documents store a live effect as an ordinary effect record (`{"id": "plugin.", "params": {…}}`): while the plug-in is installed it is drawn, exported and expanded; without it