Plug-ins: composite fonts stay out of importer filters; base64 size is checked first

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.
This commit is contained in:
dotCooCoo
2026-11-08 15:18:31 -08:00
parent ceca6fa796
commit 421189893f
7 changed files with 60 additions and 12 deletions
+5 -3
View File
@@ -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<Item = (&'static str, &'static [&
}
/// The open-dialog filters after the document formats: swatch libraries, Libraries panel
/// libraries, presets, shortcut sets, scripts and plug-ins.
/// libraries, presets, composite fonts, shortcut sets, scripts and plug-ins.
fn other_open_filters() -> impl Iterator<Item = (&'static str, &'static [&'static str])> {
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<Item = (&'static str, &'static [&'stati
.chain(std::iter::once(("PDF presets", crate::cmd::pdfcmds::PRESET_EXTS)))
.chain(std::iter::once(("Print presets", crate::cmd::printpresets::PRESET_EXTS)))
.chain(std::iter::once(("Export presets", screens::PRESET_FILE_EXTS)))
.chain(std::iter::once(("Composite fonts", crate::cmd::compositefonts::EXTS)))
.chain(std::iter::once(("Shortcut sets", crate::cmd::shortcutset::EXTS)))
.chain(std::iter::once(("Scripts", crate::cmd::scripts::EXTS)))
.chain(std::iter::once(("Plug-ins", crate::cmd::plugin::EXTS)))
+27 -4
View File
@@ -18,7 +18,7 @@ use vectorcraft_doc::{Document, MAX_COORD};
use vectorcraft_geom::Rect;
use vectorcraft_plugins::{Plugin, export, import, objects};
use super::super::{Result, bad};
use super::super::{Result, bad, str_param};
use super::load::{Loaded, detect, file_name};
use super::{err, extension, file_stem, format};
use crate::EngineError;
@@ -55,6 +55,32 @@ pub(super) fn pick(name: &str, bytes: &[u8], p: &Value) -> Result<Option<Arc<Plu
Ok(import::for_extension(&ext).into_iter().next())
}
/// How much of a file [`detect`] is given: more than its content checks read.
const SNIFF_BYTES: usize = 64 << 10;
/// Refuses `document.open`'s `dataBase64` in `p` before it is decoded when it holds a file past
/// [`import::MAX_INPUT_BYTES`] that only an importer reads, by the rule of `read_path`.
pub(super) fn check_data(p: &Value) -> 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<u8> = 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<Option<Arc<Plu
/// the limit that isn't read whole is reported from its size, without reading it.
#[cfg(not(target_arch = "wasm32"))]
pub(super) fn read_path(path: &str, p: &Value) -> Result<Vec<u8>> {
/// 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()) {
+4 -1
View File
@@ -689,7 +689,10 @@ pub(super) fn open(s: &mut Session, p: &Value) -> Result<Value> {
// 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).
+2 -2
View File
@@ -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");
+19
View File
@@ -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);
}
+1 -1
View File
@@ -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,
@@ -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,
];