diff --git a/crates/engine/src/file_access.rs b/crates/engine/src/file_access.rs index afc8c52e73..7f817726cc 100644 --- a/crates/engine/src/file_access.rs +++ b/crates/engine/src/file_access.rs @@ -1606,4 +1606,31 @@ mod tests { assert_eq!(roots.check(&s(base.join("out/x.svg")), Access::Write), Ok(())); let _ = std::fs::remove_dir_all(base); } + + /// A checked write to a real path passes a script gate only inside the gate's write folder, and + /// only when every gate chained to it passes the path too. + #[cfg(unix)] + #[test] + fn a_checked_write_to_a_real_path_passes_every_chained_gate() { + let (base, _) = folders("check-real"); + let base = std::fs::canonicalize(&base).unwrap(); + let gate = |dir: &str| ScriptGate { + read: ScriptScope::default(), + write: ScriptScope::of_folder(&base.join(dir)), + documents: vec![], + held: vec![], + linked: LinkedFiles::default(), + also: None, + }; + let (inside, outside) = (base.join("out/new.svg"), base.join("outside/new.svg")); + let denied = |p: &Path| Err(format!("Script file access denied: {}", p.display())); + assert_eq!(gate("out").check_real(&inside, Access::Write), Ok(())); + assert_eq!(gate("out").check_real(&outside, Access::Write), denied(&outside)); + assert_eq!(with_script_gate(gate("out"), || check_write_real(&inside)), Ok(()), "the running command's gate passes it"); + assert_eq!(with_script_gate(gate("out"), || check_write_real(&outside)), denied(&outside), "the running command's gate refuses it"); + let chained = gate("out").followed_by(Arc::new(gate("outside"))); + assert_eq!(chained.check_real(&inside, Access::Write), denied(&inside), "the chained gate refuses it"); + assert_eq!(chained.check_real(&outside, Access::Write), denied(&outside), "the first gate refuses it"); + let _ = std::fs::remove_dir_all(base); + } } diff --git a/crates/engine/src/tests/arrowheads.rs b/crates/engine/src/tests/arrowheads.rs index 0cdc3312ff..656f034be7 100644 --- a/crates/engine/src/tests/arrowheads.rs +++ b/crates/engine/src/tests/arrowheads.rs @@ -444,3 +444,20 @@ fn a_group_used_as_the_clipping_path_adds_none_of_its_own_fills() { let b = read.heads[0].path.bounds().unwrap(); assert!((b.width() - 4.0).abs() < 1e-3 && (b.height() - 4.0).abs() < 1e-3, "the square alone: {b:?}"); } + +/// An arrowheads file over the size limit is refused from its size: no heads are listed, and the +/// error says why. +#[test] +fn an_arrowheads_file_over_the_size_limit_lists_no_heads() { + let dir = folder("too-big"); + let file = dir.join(arrowheads::FILE_NAME); + // Sparse: no data blocks are written. + std::fs::File::create(&file).unwrap().set_len(arrowheads::MAX_FILE_BYTES + 1).unwrap(); + let (mut s, _) = session_with_line(); + s.attach_arrowheads_file(file.to_string_lossy().to_string()); + let list = s.execute("stroke.arrowheads.list", &json!({})).unwrap(); + let _ = std::fs::remove_dir_all(&dir); + assert_eq!(list["arrowheads"], json!([])); + let error = list["error"].as_str().unwrap_or_default(); + assert!(error.contains(&format!("larger than {} MB", arrowheads::MAX_FILE_BYTES >> 20)), "{list}"); +} diff --git a/crates/engine/src/tests/library.rs b/crates/engine/src/tests/library.rs index cf5fc3c269..1ee23ef47d 100644 --- a/crates/engine/src/tests/library.rs +++ b/crates/engine/src/tests/library.rs @@ -1299,3 +1299,26 @@ fn a_library_is_exported_to_a_file_and_imported_from_it() { v.libraries.set_dir(Some(folder)); assert_eq!(groups(&mut v), [("Logos".into(), refs(&[("graphic", &g)]))]); } + +/// A library file over 512 MB is refused from its size before it is opened: the file here cannot +/// be opened at all, so only the size check gives this error. +#[cfg(unix)] +#[test] +fn a_library_file_over_the_size_limit_is_refused_before_it_is_read() { + use std::os::unix::fs::PermissionsExt; + let dir = std::env::temp_dir().join(format!("vc-library-too-big-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("big.vclibrary"); + // Sparse: no data blocks are written. + std::fs::File::create(&path).unwrap().set_len((512 << 20) + 1).unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o000)).unwrap(); + // A process that may read any file (root) opens it: nothing to check. + let opened = std::fs::File::open(&path).is_ok(); + let r = (!opened).then(|| session().execute("library.import", &json!({"path": path.to_string_lossy()}))); + let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)); + let _ = std::fs::remove_dir_all(&dir); + let Some(r) = r else { return }; // root opened the file: the size check is not what refuses it + let e = r.err().unwrap_or_else(|| panic!("the file was imported")).to_string(); + assert!(e.contains("the file is larger than 512 MB"), "{e}"); +} diff --git a/crates/engine/src/tests/plugins.rs b/crates/engine/src/tests/plugins.rs index 54c599d5fd..42728c7fa6 100644 --- a/crates/engine/src/tests/plugins.rs +++ b/crates/engine/src/tests/plugins.rs @@ -2739,6 +2739,78 @@ fn stored_files_whose_reads_fail_leave_the_store_budget_to_the_others() { assert!(vectorcraft_plugins::registry::remove(id)); } +/// Sets the bytes the stored plug-ins of this thread's stores may read together, and puts the +/// usual budget back when dropped, a failed assertion included. +struct StoreBudget; + +impl StoreBudget { + fn set(bytes: u64) -> Self { + crate::store::PLUGIN_STORE_BUDGET.with(|b| b.set(bytes)); + StoreBudget + } +} + +impl Drop for StoreBudget { + fn drop(&mut self) { + crate::store::PLUGIN_STORE_BUDGET.with(|b| b.set(crate::store::MAX_PLUGIN_STORE)); + } +} + +/// The failure listed for each stored file of `folder`, as (file name, reason), sorted. +fn stored_failures_in(folder: &std::path::Path) -> Vec<(String, String)> { + let mut rows: Vec<(String, String)> = crate::store::failed_stored_plugins(folder) + .into_iter() + .map(|(p, why)| (p.file_name().unwrap().to_string_lossy().into_owned(), why)) + .collect(); + rows.sort(); + rows +} + +/// Once the stored plug-ins have used up the bytes a store may read, the stored files after them +/// are listed as failed and are not installed. +#[test] +fn stored_plugins_past_the_store_budget_are_listed_as_failed() { + let (folder, first) = store_with("plugin-budget-used-up", "test.engine.budgetused.a"); + for id in ["test.engine.budgetused.b", "test.engine.budgetused.c"] { + let module = wat::parse_str(tpl::returning(&tpl::manifest(id, "filter", "{}"), r#"{"objects":[]}"#)).unwrap(); + std::fs::write(folder.0.join(format!("{id}.wasm")), module).unwrap(); + } + let len = std::fs::metadata(&first).unwrap().len(); + let mut s = session(); + { + let _budget = StoreBudget::set(len); + s.attach_plugin_store(folder.0.clone()); + } + let installed: Vec = + ["a", "b", "c"].iter().map(|n| vectorcraft_plugins::registry::get(&format!("test.engine.budgetused.{n}")).is_some()).collect(); + let failed = stored_failures_in(&folder.0); + for n in ["a", "b", "c"] { + vectorcraft_plugins::registry::remove(&format!("test.engine.budgetused.{n}")); + } + assert_eq!(installed, [true, false, false], "only the first file fits the budget"); + let why = format!("the stored plug-ins hold more than {} bytes together", crate::store::MAX_PLUGIN_STORE); + let expected: Vec<(String, String)> = ["b", "c"].iter().map(|n| (format!("test.engine.budgetused.{n}.wasm"), why.clone())).collect(); + assert_eq!(failed, expected); +} + +/// A stored file longer than the bytes the store may still read is skipped and listed as failed. +#[test] +fn a_stored_plugin_larger_than_the_store_budget_is_skipped() { + let id = "test.engine.budgetsmall"; + let (folder, module) = store_with("plugin-budget-small", id); + let len = std::fs::metadata(&module).unwrap().len(); + let mut s = session(); + { + let _budget = StoreBudget::set(len - 1); + s.attach_plugin_store(folder.0.clone()); + } + let installed = vectorcraft_plugins::registry::remove(id); + assert!(!installed, "a file over the budget is not read"); + let failed = stored_failures_in(&folder.0); + assert_eq!(failed.len(), 1, "{failed:?}"); + assert!(failed[0].1.contains(&format!("larger than {} bytes", len - 1)), "{failed:?}"); +} + /// A stored file replaced after its module loaded, by one of the same length with the same modified /// time, is skipped: its content no longer holds the module. That holds for a file modified moments /// ago and for one modified an hour ago.