From 421189893fdbc67cb096459f0f8ed15f58319186 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 8 Nov 2026 15:18:31 -0800 Subject: [PATCH] Plug-ins: composite fonts stay out of importer filters; base64 size is checked first MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit File › Open imports .vccomposite files as composite fonts, so an importer listing that extension no longer shows it under its own filter or in document.formats. File › Open gets a Composite fonts filter. document.open refuses dataBase64 that decodes past the importer input limit before decoding it. A file whose start shows a built-in format still opens as that format, as it does by path. --- crates/engine/src/cmd/fileio/formats.rs | 8 +++-- crates/engine/src/cmd/fileio/importer.rs | 31 ++++++++++++++++--- crates/engine/src/cmd/fileio/load.rs | 5 ++- crates/engine/src/cmd/fileio/tests.rs | 4 +-- crates/engine/tests/plugin_import.rs | 19 ++++++++++++ crates/format/src/lib.rs | 2 +- .../src/dialogs/tests_plugin_import.rs | 3 +- 7 files changed, 60 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/cmd/fileio/formats.rs b/crates/engine/src/cmd/fileio/formats.rs index 3c52d5de89..dfe9c911e1 100644 --- a/crates/engine/src/cmd/fileio/formats.rs +++ b/crates/engine/src/cmd/fileio/formats.rs @@ -686,8 +686,8 @@ pub fn opens_with_importer(name: &str) -> bool { /// Whether File › Open opens a file of extension `ext` (lower case) as something other than a /// document: a plug-in (installed), a shortcut set, a script (checked), flattener, PDF, print, -/// perspective grid or export presets (imported), a Libraries panel library, or a swatch or graphic -/// style library. +/// perspective grid or export presets or composite fonts (imported), a Libraries panel library, or a +/// swatch or graphic style library. pub fn opens_as_other(ext: &str) -> bool { [ crate::cmd::plugin::EXTS, @@ -698,6 +698,7 @@ pub fn opens_as_other(ext: &str) -> bool { crate::cmd::printpresets::PRESET_EXTS, crate::cmd::perspgrid::PRESET_EXTS, screens::PRESET_FILE_EXTS, + crate::cmd::compositefonts::EXTS, crate::cmd::library::LIBRARY_EXTS, crate::cmd::swatchlib::LIBRARY_EXTS, std::slice::from_ref(&vectorcraft_doc::style_libs::STYLES_EXT), @@ -771,7 +772,7 @@ pub fn builtin_open_filters() -> impl Iterator impl Iterator { std::iter::once(("Swatch libraries", crate::cmd::swatchlib::LIBRARY_EXTS)) .chain(std::iter::once(("Libraries", crate::cmd::library::LIBRARY_EXTS))) @@ -779,6 +780,7 @@ fn other_open_filters() -> impl Iterator Result Result<()> { + let Some(b64) = str_param(p, "dataBase64") else { + return Ok(()); + }; + let name = str_param(p, "name").unwrap_or("Untitled"); + let ext = extension(name); + let explicit = named(p)?.is_some(); + let built_in = !explicit && (super::readable_format(&ext).is_some() || import::for_extension(&ext).is_empty()); + if built_in || vectorcraft_format::base64_decoded_len(b64) <= import::MAX_INPUT_BYTES as u64 { + return Ok(()); + } + if !explicit { + let start: Vec = b64.bytes().filter(|b| !b.is_ascii_whitespace()).take(SNIFF_BYTES / 3 * 4).collect(); + let head = std::str::from_utf8(&start).ok().and_then(vectorcraft_format::base64_decode); + if head.is_some_and(|head| detect(name, &head).is_some()) { + return Ok(()); + } + } + Err(err(format!("can't open `{}`: {}", file_name(name), import::too_large()))) +} + /// The file at `path` for `document.open`'s params `p`. A file that only an importer reads (one /// `importer` names, or an extension no built-in format reads that an importer lists) is read up /// to [`import::MAX_INPUT_BYTES`]: past that, it is an error, unless the start of the file shows a @@ -62,9 +88,6 @@ pub(super) fn pick(name: &str, bytes: &[u8], p: &Value) -> Result Result> { - /// How much of the file [`detect`] is given: more than its content checks read. - const SNIFF_BYTES: usize = 64 << 10; - let ext = extension(path); let explicit = named(p)?.is_some(); if !explicit && (super::readable_format(&ext).is_some() || import::for_extension(&ext).is_empty()) { diff --git a/crates/engine/src/cmd/fileio/load.rs b/crates/engine/src/cmd/fileio/load.rs index 2bc1de1857..5d22630e54 100644 --- a/crates/engine/src/cmd/fileio/load.rs +++ b/crates/engine/src/cmd/fileio/load.rs @@ -689,7 +689,10 @@ pub(super) fn open(s: &mut Session, p: &Value) -> Result { // A file only an importer plug-in reads is read up to the importer's input limit. let src = match str_param(p, "path") { Some(path) => Source { name: path, bytes: super::importer::read_path(path, p)?, path: Some(path) }, - None => source(p, "document.open")?, + None => { + super::importer::check_data(p)?; + source(p, "document.open")? + } }; // A relative path is the file in the working directory: its folder is where relative links // are found (a bare file name has no folder of its own). diff --git a/crates/engine/src/cmd/fileio/tests.rs b/crates/engine/src/cmd/fileio/tests.rs index 2e8677e582..c523284ba8 100644 --- a/crates/engine/src/cmd/fileio/tests.rs +++ b/crates/engine/src/cmd/fileio/tests.rs @@ -377,8 +377,8 @@ fn open_exts_cover_every_readable_format() { assert_eq!(filters[0], ("All readable files", OPEN_EXTS)); assert_eq!( filters.len(), - 10 + FORMATS.iter().filter(|f| f.read).count(), - "and swatch libraries, libraries, flattener, PDF, print and export presets, shortcut sets, scripts, plug-ins" + 11 + FORMATS.iter().filter(|f| f.read).count(), + "and swatch libraries, libraries, flattener, PDF, print and export presets, composite fonts, shortcut sets, scripts, plug-ins" ); assert!(filters.contains(&("Export presets", SCREEN_EXPORT_PRESET_EXTS)), "File › Open imports export presets"); assert!(filters.contains(&("Scripts", crate::cmd::scripts::EXTS)), "File › Open checks scripts"); diff --git a/crates/engine/tests/plugin_import.rs b/crates/engine/tests/plugin_import.rs index a75b4666a7..ddb4145645 100644 --- a/crates/engine/tests/plugin_import.rs +++ b/crates/engine/tests/plugin_import.rs @@ -199,6 +199,7 @@ fn file_open_lists_only_the_extensions_it_opens_with_an_importer() { let filters: Vec<_> = fileio::open_filters().collect(); assert!(filters.contains(&("Test test.claimed", &["tcl"][..])), "{filters:?}"); assert!(!filters.iter().any(|(label, _)| *label == "Test test.claimedonly"), "{filters:?}"); + assert!(filters.contains(&("Composite fonts", vectorcraft_engine::cmd::compositefonts::EXTS)), "{filters:?}"); s.execute("plugin.remove", &json!({"id": "test.claimed"})).unwrap(); s.execute("plugin.remove", &json!({"id": "test.claimedonly"})).unwrap(); } @@ -366,6 +367,20 @@ fn a_file_past_the_input_limit_is_refused() { let e = open(&mut s, "big.tsb", &vec![0; max + 1], json!({})).unwrap_err(); assert!(e.to_string().contains("larger than"), "{e}"); assert_eq!(state(&mut s), before, "as data"); + // The size is checked before the text is decoded: this text is not base64 at all. + let not_base64 = "!".repeat((max + 1) / 3 * 4 + 8); + for extra in [json!({}), json!({"importer": "plugin.test.large"})] { + let mut p = json!({"name": "big.tsb", "dataBase64": not_base64}); + p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone()); + let e = s.execute("document.open", &p).unwrap_err(); + assert!(e.to_string().contains("larger than"), "{e}"); + assert_eq!(state(&mut s), before, "before decoding"); + } + // White space is not part of the file's size. + let spaced = format!("QUJD{}", " ".repeat(max / 3 * 4 + 8)); + if let Err(e) = s.execute("document.open", &json!({"name": "small.tsb", "dataBase64": spaced})) { + assert!(!e.to_string().contains("larger than"), "{e}"); + } let _ = std::fs::remove_dir_all(&dir); } @@ -386,5 +401,9 @@ fn a_built_in_file_past_the_input_limit_still_opens() { assert!(s.documents().is_empty()); let r = s.execute("document.open", &json!({"path": path_of(&path)})).unwrap(); assert_eq!(r["format"], "png"); + let mut data = png.into_inner(); + data.resize((vectorcraft_plugins::import::MAX_INPUT_BYTES + 3) / 3 * 3, 0); + let encoded = json!({"name": "big.tsp", "dataBase64": vectorcraft_format::base64_encode(&data)}); + assert_eq!(s.execute("document.open", &encoded).unwrap()["format"], "png"); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs index b3aa7eab79..5d7aabd077 100644 --- a/crates/format/src/lib.rs +++ b/crates/format/src/lib.rs @@ -56,7 +56,7 @@ use vectorcraft_doc::{Document, ImageBlob}; #[cfg(not(target_arch = "wasm32"))] pub use atomic::{sync_file, write_atomic, write_atomic_with, write_new_with}; -pub use craft_vectorcraft_format::base64::{decode as base64_decode, encode as base64_encode}; +pub use craft_vectorcraft_format::base64::{decode as base64_decode, decoded_len as base64_decoded_len, encode as base64_encode}; pub use craft_vectorcraft_format::{ BLOB_MAGIC, COMPRESSED_SINCE, EXTENSION, ICC_MIME, INLINE_MAX, LEGACY_EXTENSION, MIN_VERSION, PREVIEW_MAX, VERSION, is_compressed, json_part, sniff, diff --git a/crates/ui-egui/src/dialogs/tests_plugin_import.rs b/crates/ui-egui/src/dialogs/tests_plugin_import.rs index 63e97d86aa..7dd035c1e1 100644 --- a/crates/ui-egui/src/dialogs/tests_plugin_import.rs +++ b/crates/ui-egui/src/dialogs/tests_plugin_import.rs @@ -230,7 +230,7 @@ fn a_dropped_file_only_an_importer_reads_opens() { #[test] fn importer_filters_leave_out_what_file_open_opens_as_something_else() { use vectorcraft_engine::cmd::{self, fileio}; - let lists: [&[&str]; 10] = [ + let lists: [&[&str]; 11] = [ cmd::plugin::EXTS, cmd::shortcutset::EXTS, cmd::scripts::EXTS, @@ -239,6 +239,7 @@ fn importer_filters_leave_out_what_file_open_opens_as_something_else() { cmd::printpresets::PRESET_EXTS, cmd::perspgrid::PRESET_EXTS, fileio::SCREEN_EXPORT_PRESET_EXTS, + cmd::compositefonts::EXTS, cmd::library::LIBRARY_EXTS, cmd::swatchlib::LIBRARY_EXTS, ];