mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-19 14:01:02 +01:00
Bound project archive and file reads, harden exported file names
This commit is contained in:
1 parent
e1e9b44cc0
commit
6db2836dae
11 files changed
+116
-16
No files matched your search
@@ -26,7 +26,9 @@ pub struct Headless {
|
||||
fn read_file(fs: &Filesystem, path: &str) -> Result<Vec<u8>, AutomationError> {
|
||||
match fs {
|
||||
Filesystem::Denied => Err(bad("file access is not granted: start the server with --read-root <dir>")),
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<u8>, name:
|
||||
pub(crate) fn import_resource_file(s: &mut Session, p: &J, vector: bool) -> Result<J> {
|
||||
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" };
|
||||
|
||||
@@ -72,7 +72,7 @@ pub(crate) fn load(package: &mut Package) -> Vec<String> {
|
||||
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)
|
||||
|
||||
@@ -32,7 +32,7 @@ pub fn reader(path: &str) -> impl Fn(&str) -> Option<Vec<u8>> + use<> {
|
||||
return None;
|
||||
}
|
||||
|
||||
std::fs::read(file).ok()
|
||||
materialcraft_formats::read_capped(file, materialcraft_formats::MAX_FILE).ok()
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
None
|
||||
|
||||
@@ -162,7 +162,7 @@ fn export_material(s: &mut Session, p: &J) -> Result<J> {
|
||||
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<J> {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn read_image_file(cmd: &str, path: &str) -> Result<(String, Vec<u8>)> {
|
||||
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))
|
||||
|
||||
@@ -55,7 +55,7 @@ fn read_package(path: &str) -> Result<Vec<u8>> {
|
||||
}
|
||||
#[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"))
|
||||
|
||||
@@ -168,7 +168,7 @@ fn view3d(s: &mut Session, p: &J) -> Result<J> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn load_mesh(s: &mut Session, p: &J) -> Result<J> {
|
||||
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());
|
||||
|
||||
@@ -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<std::path::Path>, max: u64) -> Result<Vec<u8>> {
|
||||
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
|
||||
|
||||
@@ -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<T> = std::result::Result<T, FormatError>;
|
||||
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"];
|
||||
|
||||
@@ -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<String> {
|
||||
Ok(components.join("/"))
|
||||
}
|
||||
|
||||
/// Most entries a project archive may hold
|
||||
const MAX_ENTRIES: usize = 100_000;
|
||||
|
||||
pub fn read(bytes: &[u8], identity: &str) -> Result<sbs::SbsRead> {
|
||||
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<sbs::SbsRead> {
|
||||
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();
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
Reference in new issue
Block a user