From f78e95159b3a3c8bccf45973952e2119122ccf24 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 18:11:39 -0800 Subject: [PATCH 1/2] Scripting: keep the placed image size message after app.open's limit The 256 MiB document limit sat in Disk::open_target, which placedItems' file assignment also calls, so an oversized image was refused as "Script document exceeds 256 MiB" before its own check ("is larger than 256 MiB") ran, and placed_file_refuses_bad_values_without_changing_the_document failed. app.open now checks the document size itself after open_target, and open_target still reports a failed metadata read as a read failure. --- crates/engine/src/script/dom/save_open.rs | 4 ++++ crates/engine/src/script/files.rs | 6 +----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/script/dom/save_open.rs b/crates/engine/src/script/dom/save_open.rs index 109d091180..a5886ca6d3 100644 --- a/crates/engine/src/script/dom/save_open.rs +++ b/crates/engine/src/script/dom/save_open.rs @@ -1091,6 +1091,10 @@ fn app_open(h: &Host, a: &[Data]) -> Answer { } // The command opens the given path; a real path that is not valid UTF-8 is still refused. let real = h.disk.borrow().open_target(path).map_err(|m| refused(m, path))?; + // The document's size is read before any of it is; a placed image keeps its own limit and message. + if std::fs::metadata(&real).is_ok_and(|m| m.len() > super::super::files::MAX_OPEN_BYTES) { + return Err(refused(super::super::files::OPEN_TOO_LARGE, path)); + } plain(real)?; p.insert("path".into(), json!(plain(std::path::PathBuf::from(path))?)); let r = crate::cmd::fileio::with_read_limit(super::super::files::MAX_OPEN_BYTES as usize, || { diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index b33e1c0c68..31ddbdacd2 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -726,13 +726,9 @@ impl Disk { Err(ScriptRead::Refused | ScriptRead::TooLarge) => return Err(ACCESS_DENIED), Err(ScriptRead::Failed(_)) => return Err(READ_FAILED), }; - let meta = std::fs::metadata(&real).map_err(|_| READ_FAILED)?; - if !meta.is_file() { + if !std::fs::metadata(&real).map_err(|_| READ_FAILED)?.is_file() { return Err(NOT_REGULAR); } - if meta.len() > MAX_OPEN_BYTES { - return Err(OPEN_TOO_LARGE); - } Ok(real) } From ae0b4664a00fa2718c8e5d5d41703b4ef64b7822 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 17:35:30 -0800 Subject: [PATCH 2/2] Scripts: multi-file exports catch case aliases and keep backslashes in Unix names Two destinations that differ only in letter case name one file on Windows and macOS, and a multi-file write now refuses them when either file is new. Files that already exist are still compared by their real paths, so two files in a case-sensitive folder stay distinct. On Linux and macOS a backslash is part of a file name, so the files written beside an export (per-artboard files, linked images, image maps) now go in the destination's folder instead of taking the name's text before the backslash as a folder. --- crates/engine/src/cmd/fileio/encode.rs | 23 ++++++++++++- crates/engine/src/cmd/fileio/export_names.rs | 36 ++++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/fileio/encode.rs b/crates/engine/src/cmd/fileio/encode.rs index 96e5d2210f..6ac9189b32 100644 --- a/crates/engine/src/cmd/fileio/encode.rs +++ b/crates/engine/src/cmd/fileio/encode.rs @@ -135,6 +135,27 @@ pub(super) fn add_note(notes: &mut Vec, note: Note) { } } +/// Whether `\` separates folders in a path: on Windows and on the web, where paths may come from +/// Windows. On other platforms `\` is a character of a file name. +const BACKSLASH_SEPARATES: bool = cfg!(any(windows, target_arch = "wasm32")); + +/// The length of `path`'s folder part, its last separator included. +fn dir_len(path: &str, backslash: bool) -> usize { + let at = if backslash { path.rfind(['/', '\\']) } else { path.rfind('/') }; + at.map_or(0, |i| i + 1) +} + +#[cfg(test)] +mod dir_tests { + #[test] + fn a_backslash_is_part_of_a_file_name_where_it_does_not_separate_folders() { + assert_eq!(super::dir_len("out/a\\b.svg", false), 4, "the file a\\b.svg is in out/"); + assert_eq!(super::dir_len("a\\b.svg", false), 0); + assert_eq!(super::dir_len("out\\a\\b.svg", true), 6); + assert_eq!(super::dir_len("b.svg", true), 0); + } +} + impl Encoded { pub(super) fn one(bytes: Vec) -> Self { Self { files: vec![(None, bytes)], ..Self::default() } @@ -180,7 +201,7 @@ impl Encoded { /// under their own names, then the image maps as `{image stem}.html` or `.map`. pub fn named<'a>(&'a self, doc: &Document, path: &str) -> Vec<(String, Cow<'a, [u8]>)> { // Siblings keep the path's own separators (this runs on every platform and on the web). - let dir = &path[..path.rfind(['/', '\\']).map_or(0, |i| i + 1)]; + let dir = &path[..dir_len(path, BACKSLASH_SEPARATES)]; let sibling = |name: &str| format!("{dir}{name}"); let mut out: Vec<(String, Cow<[u8]>)> = match self.files.as_slice() { [(_, bytes)] => vec![(path.to_string(), Cow::Borrowed(bytes.as_slice()))], diff --git a/crates/engine/src/cmd/fileio/export_names.rs b/crates/engine/src/cmd/fileio/export_names.rs index f75da1f6a1..73d93ef549 100644 --- a/crates/engine/src/cmd/fileio/export_names.rs +++ b/crates/engine/src/cmd/fileio/export_names.rs @@ -117,20 +117,40 @@ pub(super) fn write_encoded(path: Option<&str>, name: &str, doc: &vectorcraft_do /// is compared as written, and its write then reports its own error. #[cfg(not(target_arch = "wasm32"))] pub(super) fn distinct_write_targets<'a>(paths: impl IntoIterator) -> Result<()> { - let mut targets = std::collections::HashSet::new(); + // Files that exist are compared by their real paths. A file a write would create is also + // compared with every other destination by `target_key`. + let mut exact = std::collections::HashSet::new(); + let mut folded: std::collections::HashMap = std::collections::HashMap::new(); for path in paths { let real = crate::file_access::resolve(path) .ok() .or_else(|| dangling_target(path)) .or_else(|| std::path::absolute(path).ok()) .unwrap_or_else(|| std::path::PathBuf::from(path)); - if !targets.insert(real) { + let exists = std::fs::symlink_metadata(&real).is_ok(); + let same_new_file = match folded.insert(target_key(real.clone(), FOLD_CASE), exists) { + Some(other_exists) => !(exists && other_exists), + None => false, + }; + if !exact.insert(real) || same_new_file { return Err(EngineError::Other("output destinations resolve to the same file".into())); } } Ok(()) } +/// Whether file names ignore letter case: Windows and macOS file systems do by default, so `A.png` +/// and `a.png` name one file there. Files that already exist are compared by their real paths, so a +/// case-sensitive folder's `A.png` and `a.png` stay two files. +#[cfg(not(target_arch = "wasm32"))] +const FOLD_CASE: bool = cfg!(any(windows, target_os = "macos")); + +/// `real` as compared with the other destinations: in lowercase when `fold_case`. +#[cfg(not(target_arch = "wasm32"))] +fn target_key(real: std::path::PathBuf, fold_case: bool) -> std::path::PathBuf { + if fold_case { std::path::PathBuf::from(real.to_string_lossy().to_lowercase()) } else { real } +} + /// The file a write to `path` creates when `path` is a link (or a chain of at most 40 links) that /// leads to a file that does not exist yet. #[cfg(not(target_arch = "wasm32"))] @@ -163,6 +183,18 @@ fn dangling_target(path: &str) -> Option { .or_else(|| std::path::absolute(&at).ok()) } +#[cfg(all(test, not(target_arch = "wasm32")))] +mod case_tests { + use std::path::PathBuf; + + #[test] + fn names_that_differ_only_in_case_are_the_same_file_where_case_is_ignored() { + let key = |p: &str, fold| super::target_key(PathBuf::from(p), fold); + assert_eq!(key("/out/Out.png", true), key("/out/out.png", true), "one file where case is ignored"); + assert_ne!(key("/out/Out.png", false), key("/out/out.png", false), "two files where case counts"); + } +} + /// `a` with the fields of `b`. pub(crate) fn merge(mut a: Value, b: Value) -> Value { if let (Some(a), Value::Object(b)) = (a.as_object_mut(), b) {