From 6db2836dae8a4e76dcbbd2304580f1a770d12c40 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 10 Nov 2026 22:54:16 +0000 Subject: [PATCH] Bound project archive and file reads, harden exported file names --- crates/automation/src/headless.rs | 4 +- crates/engine/src/file_cmds.rs | 4 +- crates/engine/src/linked_files.rs | 2 +- crates/engine/src/linked_resource.rs | 2 +- crates/engine/src/material_cmds.rs | 4 +- crates/engine/src/package_file_commands.rs | 2 +- crates/engine/src/view_cmds.rs | 2 +- crates/formats/src/file.rs | 45 ++++++++++++++++++++-- crates/formats/src/lib.rs | 6 ++- crates/formats/src/project.rs | 17 +++++++- crates/formats/src/tests.rs | 44 +++++++++++++++++++++ 11 files changed, 116 insertions(+), 16 deletions(-) diff --git a/crates/automation/src/headless.rs b/crates/automation/src/headless.rs index 2ebafb0..4558841 100644 --- a/crates/automation/src/headless.rs +++ b/crates/automation/src/headless.rs @@ -26,7 +26,9 @@ pub struct Headless { fn read_file(fs: &Filesystem, path: &str) -> Result, AutomationError> { match fs { Filesystem::Denied => Err(bad("file access is not granted: start the server with --read-root ")), - Filesystem::TrustedLocal => std::fs::read(path).map_err(|e| AutomationError::Io(format!("cannot read {path}: {e}"))), + Filesystem::TrustedLocal => { + materialcraft_formats::read_capped(path, materialcraft_formats::MAX_FILE).map_err(|e| AutomationError::Io(format!("cannot read {path}: {e}"))) + } Filesystem::Workspace(w) => w.read(path), } } diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs index 19b6e1e..d5d30c8 100644 --- a/crates/engine/src/file_cmds.rs +++ b/crates/engine/src/file_cmds.rs @@ -249,7 +249,7 @@ pub fn open_file(s: &mut Session, path: &str) -> Result<()> { return open_bytes_linked(s, &bytes, Some(path.into()), &linked_files(path)); } - let bytes = std::fs::read(path).map_err(|e| bad("file.open", format!("{path}: {e}")))?; + let bytes = materialcraft_formats::read_capped(path, materialcraft_formats::MAX_FILE).map_err(|e| bad("file.open", format!("{path}: {e}")))?; open_bytes_linked(s, &bytes, Some(path.to_string()), &linked_files(path)) } @@ -447,7 +447,7 @@ pub(crate) fn push_resource(pkg: &mut Package, file: &str, bytes: Vec, name: pub(crate) fn import_resource_file(s: &mut Session, p: &J, vector: bool) -> Result { let path = strp(p, "path").ok_or_else(|| bad("file.importBitmap", "`path` is required"))?; validate_resource_extension(path, vector)?; - let bytes = std::fs::read(path).map_err(|e| bad("file.importBitmap", format!("{path}: {e}")))?; + let bytes = materialcraft_formats::read_capped(path, materialcraft_formats::MAX_FILE).map_err(|e| bad("file.importBitmap", format!("{path}: {e}")))?; let file = std::path::Path::new(path).file_name().and_then(|f| f.to_str()).unwrap_or("bitmap.png").to_string(); let name = add_resource_into(s, &file, bytes, strp(p, "name"), strp(p, "folder").unwrap_or("Resources"))?; let def = if file.to_ascii_lowercase().ends_with(".svg") { "svg" } else { "bitmap" }; diff --git a/crates/engine/src/linked_files.rs b/crates/engine/src/linked_files.rs index 2ea6e57..cad471a 100644 --- a/crates/engine/src/linked_files.rs +++ b/crates/engine/src/linked_files.rs @@ -72,7 +72,7 @@ pub(crate) fn load(package: &mut Package) -> Vec { return Err("external file is unavailable in the browser".into()); #[cfg(not(target_arch = "wasm32"))] { - let bytes = std::fs::read(&path).map_err(|error| error.to_string())?; + let bytes = materialcraft_formats::read_capped(&path, materialcraft_formats::MAX_FILE).map_err(|error| error.to_string())?; let read = materialcraft_formats::sbs::read_sbs(&bytes, &path, &crate::file_cmds::linked_files(&path)).map_err(|error| error.to_string())?; warnings.extend(read.warnings); Ok(read.pkg) diff --git a/crates/engine/src/linked_resource.rs b/crates/engine/src/linked_resource.rs index 7a0939c..7d5f742 100644 --- a/crates/engine/src/linked_resource.rs +++ b/crates/engine/src/linked_resource.rs @@ -32,7 +32,7 @@ pub fn reader(path: &str) -> impl Fn(&str) -> Option> + use<> { return None; } - std::fs::read(file).ok() + materialcraft_formats::read_capped(file, materialcraft_formats::MAX_FILE).ok() } #[cfg(target_arch = "wasm32")] None diff --git a/crates/engine/src/material_cmds.rs b/crates/engine/src/material_cmds.rs index d262c67..f7cf7a3 100644 --- a/crates/engine/src/material_cmds.rs +++ b/crates/engine/src/material_cmds.rs @@ -162,7 +162,7 @@ fn export_material(s: &mut Session, p: &J) -> Result { let (gi, mut o) = material_request(s, CMD, p)?; for (id, file) in p.get("inputs").and_then(J::as_object).into_iter().flatten() { let file = file.as_str().ok_or_else(|| bad(CMD, format!("inputs.{id} must be an image path")))?; - let bytes = std::fs::read(file).map_err(|e| bad(CMD, format!("{file}: {e}")))?; + let bytes = materialcraft_formats::read_capped(file, materialcraft_formats::MAX_FILE).map_err(|e| bad(CMD, format!("{file}: {e}")))?; o.inputs.insert(id.clone(), materialcraft_formats::decode(&bytes, !srgb_usage(id))?); } let bytes = material_bytes(s, gi, &o)?; @@ -238,7 +238,7 @@ fn bind(s: &mut Session, p: &J) -> Result { #[cfg(not(target_arch = "wasm32"))] fn read_image_file(cmd: &str, path: &str) -> Result<(String, Vec)> { - let bytes = std::fs::read(path).map_err(|e| bad(cmd, format!("{path}: {e}")))?; + let bytes = materialcraft_formats::read_capped(path, materialcraft_formats::MAX_FILE).map_err(|e| bad(cmd, format!("{path}: {e}")))?; materialcraft_formats::decode(&bytes, false)?; let name = std::path::Path::new(path).file_name().and_then(|f| f.to_str()).unwrap_or("input.png").to_string(); Ok((name, bytes)) diff --git a/crates/engine/src/package_file_commands.rs b/crates/engine/src/package_file_commands.rs index 39e92dd..c75a49d 100644 --- a/crates/engine/src/package_file_commands.rs +++ b/crates/engine/src/package_file_commands.rs @@ -55,7 +55,7 @@ fn read_package(path: &str) -> Result> { } #[cfg(not(target_arch = "wasm32"))] { - std::fs::read(path).map_err(|error| bad("file.reload", error.to_string())) + materialcraft_formats::read_capped(path, materialcraft_formats::MAX_FILE).map_err(|error| bad("file.reload", error.to_string())) } #[cfg(target_arch = "wasm32")] Err(bad("file.reload", "reopen local browser files with the file picker")) diff --git a/crates/engine/src/view_cmds.rs b/crates/engine/src/view_cmds.rs index 274a88f..ed33934 100644 --- a/crates/engine/src/view_cmds.rs +++ b/crates/engine/src/view_cmds.rs @@ -168,7 +168,7 @@ fn view3d(s: &mut Session, p: &J) -> Result { #[cfg(not(target_arch = "wasm32"))] fn load_mesh(s: &mut Session, p: &J) -> Result { let path = strp(p, "path").ok_or_else(|| bad("view.3d.loadMesh", "`path` is required"))?; - let bytes = std::fs::read(path).map_err(|e| bad("view.3d.loadMesh", format!("{path}: {e}")))?; + let bytes = materialcraft_formats::read_capped(path, materialcraft_formats::MAX_FILE).map_err(|e| bad("view.3d.loadMesh", format!("{path}: {e}")))?; let name = std::path::Path::new(path).file_name().and_then(|n| n.to_str()).unwrap_or(path).to_string(); s.load_mesh_bytes(&name, &bytes)?; let tris = s.custom_mesh.as_ref().map_or(0, |(_, m)| m.triangle_count()); diff --git a/crates/formats/src/file.rs b/crates/formats/src/file.rs index 27acebd..443d9a8 100644 --- a/crates/formats/src/file.rs +++ b/crates/formats/src/file.rs @@ -1,11 +1,50 @@ #[cfg(not(target_arch = "wasm32"))] use crate::{FormatError, Result}; +/// Longest file name `safe_file_name` returns, in characters +const MAX_NAME: usize = 100; + pub fn safe_file_name(name: &str) -> String { let base = name.rsplit(['/', '\\']).next().unwrap_or(name); - let cleaned: String = base.chars().map(|c| if c.is_alphanumeric() || "._- ".contains(c) { c } else { '_' }).collect(); - let cleaned = cleaned.trim_start_matches('.').to_string(); - if cleaned.is_empty() { "resource".into() } else { cleaned } + let cleaned: String = base.chars().map(|c| if c.is_alphanumeric() || "._- ".contains(c) { c } else { '_' }).take(MAX_NAME).collect(); + // Windows drops trailing dots and spaces, so `CON.` would still open the device + let cleaned = cleaned.trim_start_matches('.').trim_end_matches(['.', ' ']).to_string(); + if cleaned.is_empty() { + return "resource".into(); + } + + let stem = cleaned.split('.').next().unwrap_or("").trim_end().to_ascii_uppercase(); + let mut chars = stem.chars(); + let device = matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CONIN$" | "CONOUT$"); + // COM1..COM9 and LPT1..LPT9, also with the superscript digits Windows treats as digits + let numbered = stem.chars().count() == 4 + && (stem.starts_with("COM") || stem.starts_with("LPT")) + && chars.next_back().is_some_and(|c| matches!(c, '1'..='9' | '\u{b9}' | '\u{b2}' | '\u{b3}')); + let reserved = device || numbered; + if reserved { format!("_{cleaned}") } else { cleaned } +} + +/// Read a whole regular file of at most `max` bytes: a FIFO, a device or an oversized file is refused before anything is allocated +#[cfg(not(target_arch = "wasm32"))] +pub fn read_capped(path: impl AsRef, max: u64) -> Result> { + use std::io::Read; + let path = path.as_ref(); + let io = |e: std::io::Error| FormatError::Io(format!("{}: {e}", path.display())); + // Check before open: opening a FIFO for reading blocks until a writer appears + let regular = |m: std::fs::Metadata| if m.is_file() { Ok(m.len()) } else { Err(FormatError::Io(format!("{}: not a regular file", path.display()))) }; + regular(std::fs::metadata(path).map_err(io)?)?; + let file = std::fs::File::open(path).map_err(io)?; + let len = regular(file.metadata().map_err(io)?)?; + if len > max { + return Err(FormatError::Io(format!("{}: file is larger than {max} bytes", path.display()))); + } + + let mut bytes = Vec::with_capacity(len as usize); + file.take(max + 1).read_to_end(&mut bytes).map_err(io)?; + if bytes.len() as u64 > max { + return Err(FormatError::Io(format!("{}: file is larger than {max} bytes", path.display()))); + } + Ok(bytes) } /// The file stem of an exported output: `pattern` with `$(graph)`, `$(identifier)` and `$(usage)` replaced, sanitized for file systems diff --git a/crates/formats/src/lib.rs b/crates/formats/src/lib.rs index 46e1459..746db24 100644 --- a/crates/formats/src/lib.rs +++ b/crates/formats/src/lib.rs @@ -15,9 +15,9 @@ mod guard; mod image; pub use depth::Depth; -#[cfg(not(target_arch = "wasm32"))] -pub use file::write_atomic; pub use file::{output_name, safe_file_name}; +#[cfg(not(target_arch = "wasm32"))] +pub use file::{read_capped, write_atomic}; pub use format_error::FormatError; pub(crate) use guard::guard; pub use image::{decode, encode}; @@ -25,6 +25,8 @@ pub use image::{decode, encode}; pub type Result = std::result::Result; pub(crate) const MAX_JSON: u64 = 64 << 20; pub(crate) const MAX_RESOURCE: u64 = 256 << 20; +/// Largest file the apps read whole from disk: a bound on memory, far above any real document, image or mesh +pub const MAX_FILE: u64 = 1 << 30; pub const EXPORT_FORMATS: &[(&str, &str)] = &[("png", "PNG"), ("jpg", "JPEG"), ("tga", "TGA"), ("tif", "TIFF"), ("bmp", "BMP"), ("webp", "WEBP"), ("exr", "OpenEXR"), ("hdr", "Radiance HDR")]; pub const VECTOR_EXTENSIONS: &[&str] = &["svg"]; diff --git a/crates/formats/src/project.rs b/crates/formats/src/project.rs index 0cc4b59..b88dc31 100644 --- a/crates/formats/src/project.rs +++ b/crates/formats/src/project.rs @@ -4,7 +4,7 @@ mod project_files; pub use project_files::ProjectFiles; -use crate::{FormatError, Result, sbs}; +use crate::{FormatError, MAX_FILE, MAX_RESOURCE, Result, sbs}; use materialcraft_core::{Package, Value}; use std::collections::{BTreeMap, BTreeSet}; #[cfg(not(target_arch = "wasm32"))] @@ -74,8 +74,16 @@ fn resolve(owner: &str, reference: &str) -> Result { Ok(components.join("/")) } +/// Most entries a project archive may hold +const MAX_ENTRIES: usize = 100_000; + pub fn read(bytes: &[u8], identity: &str) -> Result { let mut zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|error| bad(error.to_string()))?; + if zip.len() > MAX_ENTRIES { + return Err(bad("Archive has too many entries")); + } + + let mut total = 0u64; let mut files = BTreeMap::new(); let mut seen = BTreeSet::new(); for index in 0..zip.len() { @@ -95,8 +103,13 @@ pub fn read(bytes: &[u8], identity: &str) -> Result { continue; } + // The declared size is untrusted, so the bound is on the bytes actually inflated let mut content = Vec::new(); - entry.read_to_end(&mut content).map_err(|error| bad(error.to_string()))?; + (&mut entry).take(MAX_RESOURCE.min(MAX_FILE - total) + 1).read_to_end(&mut content).map_err(|error| bad(error.to_string()))?; + total += content.len() as u64; + if content.len() as u64 > MAX_RESOURCE || total > MAX_FILE { + return Err(bad(format!("Archive entry `{name}` is too large"))); + } files.insert(name, content); } let roots: Vec<_> = files.keys().filter(|name| !name.contains('/') && name.to_ascii_lowercase().ends_with(".sbs")).cloned().collect(); diff --git a/crates/formats/src/tests.rs b/crates/formats/src/tests.rs index e8fe792..4cd5339 100644 --- a/crates/formats/src/tests.rs +++ b/crates/formats/src/tests.rs @@ -50,3 +50,47 @@ fn decode_matches_previous_values() { let want = srgb_to_linear(128.0 / 255.0); assert!((img.get(1, 1, false)[0] - want).abs() < 1e-4); } + +#[test] +fn safe_file_name_blocks_windows_device_names() { + for (name, want) in [ + ("CON", "_CON"), + ("nul.png", "_nul.png"), + ("com1", "_com1"), + ("LPT9.txt", "_LPT9.txt"), + ("aux. ", "_aux"), + ("con.", "_con"), + ("COM0", "COM0"), + ("console", "console"), + ] { + assert_eq!(safe_file_name(name), want, "{name}"); + } + assert_eq!(safe_file_name("name. . "), "name"); + assert_eq!(safe_file_name("...").as_str(), "resource"); + assert_eq!(safe_file_name(&"a".repeat(5000)).len(), 100); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn read_capped_bounds_size_and_kind() { + let dir = std::env::temp_dir().join(format!("mc-read-capped-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("f.bin"); + std::fs::write(&file, b"12345").unwrap(); + assert_eq!(read_capped(&file, 5).unwrap(), b"12345"); + assert!(read_capped(&file, 4).is_err()); + assert!(read_capped(&dir, 100).is_err(), "directories are not regular files"); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn project_archive_entry_count_is_bounded() { + let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new())); + let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + for i in 0..100_001 { + zip.start_file(format!("f{i}"), stored).unwrap(); + } + let bytes = zip.finish().unwrap().into_inner(); + let error = project::read(&bytes, "x").unwrap_err().to_string(); + assert!(error.contains("too many entries"), "{error}"); +}