diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml index 2a0101b04..bf00e6aeb 100644 --- a/crates/format/Cargo.toml +++ b/crates/format/Cargo.toml @@ -14,7 +14,6 @@ figcraft-pathops = { workspace = true } figcraft-layout = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } -zip = { workspace = true } flate2 = { workspace = true } ruzstd = { workspace = true } sha1 = { workspace = true } @@ -28,3 +27,6 @@ craft-figcraft-format = { workspace = true } craft-figma-rest = { workspace = true } craft-jpeg = { workspace = true } craft-image-codecs = { workspace = true } + +[dev-dependencies] +zip = { workspace = true } diff --git a/crates/format/src/penpot.rs b/crates/format/src/penpot.rs index c47b55b18..16a873d6a 100644 --- a/crates/format/src/penpot.rs +++ b/crates/format/src/penpot.rs @@ -8,6 +8,8 @@ use std::collections::{BTreeMap, BTreeSet}; use std::io::{self, Cursor, Read}; +use crate::{FormatError, add_image}; +use craft_zip::{SeekableArchive, SeekableLimits}; use figcraft_doc::color::{BlendMode, Color, ColorStop, Gradient, GradientKind, ImageScaleMode, Paint, PaintKind}; use figcraft_doc::geom::{Affine, BezPath, Shape}; use figcraft_doc::{ @@ -17,9 +19,8 @@ use figcraft_doc::{ Variable, VariableType, VariableValue, VectorNetwork, }; use serde_json::Value; -use zip::ZipArchive; -use crate::{FormatError, add_image}; +type PenpotArchive<'a> = SeekableArchive>; mod embedded_fonts; mod export; @@ -135,11 +136,12 @@ impl Read for BudgetReader<'_, R> { } /// Read a JSON archive member within the per-member decompression limit. -fn json(z: &mut ZipArchive>, path: &str, budget: &mut InflateBudget) -> Result { - let entry = z.by_name(path).map_err(|e| corrupt(format!("{path}: {e}")))?; - if entry.size() > MAX_JSON { +fn json(z: &mut PenpotArchive<'_>, path: &str, budget: &mut InflateBudget) -> Result { + let declared = z.declared_size(path).ok_or_else(|| corrupt(format!("{path}: missing ZIP member")))?; + if declared > MAX_JSON { return Err(corrupt(format!("JSON member too large: {path}"))); } + let entry = z.open_entry(path, MAX_JSON).map_err(|e| corrupt(e.to_string()))?.ok_or_else(|| corrupt(format!("{path}: missing ZIP member")))?; let mut data = Vec::new(); BudgetReader { inner: entry, budget }.take(MAX_JSON + 1).read_to_end(&mut data).map_err(|e| corrupt(e.to_string()))?; if data.len() as u64 > MAX_JSON { @@ -149,14 +151,15 @@ fn json(z: &mut ZipArchive>, path: &str, budget: &mut InflateBudge } /// Read a shape JSON member within member and per-page decompression limits. -fn shape_json(z: &mut ZipArchive>, path: &str, budget: &mut InflateBudget, page_budget: &mut u64) -> Result { - let entry = z.by_name(path).map_err(|e| corrupt(format!("{path}: {e}")))?; - if entry.size() > MAX_JSON { +fn shape_json(z: &mut PenpotArchive<'_>, path: &str, budget: &mut InflateBudget, page_budget: &mut u64) -> Result { + let declared = z.declared_size(path).ok_or_else(|| corrupt(format!("{path}: missing ZIP member")))?; + if declared > MAX_JSON { return Err(corrupt(format!("JSON member too large: {path}"))); } - if entry.size() > *page_budget { + if declared > *page_budget { return Err(corrupt("page shapes exceed byte budget")); } + let entry = z.open_entry(path, MAX_JSON).map_err(|e| corrupt(e.to_string()))?.ok_or_else(|| corrupt(format!("{path}: missing ZIP member")))?; let mut data = Vec::new(); let max_read = (MAX_JSON + 1).min(*page_budget + 1); BudgetReader { inner: entry, budget }.take(max_read).read_to_end(&mut data).map_err(|e| corrupt(e.to_string()))?; @@ -360,29 +363,35 @@ fn finish_masked_group(doc: &mut Document, group: NodeId, source: &Value, warnin } } +fn open_archive(bytes: &[u8]) -> Result, FormatError> { + let limits = SeekableLimits { + max_entries: MAX_ENTRIES as u64, + max_metadata_bytes: MAX_ARCHIVE as u64, + // The Penpot budget also charges partial reads (embedded font signatures) and + // remembers an overflow that an optional-member caller might otherwise swallow. + max_total_bytes: u64::MAX, + }; + let z = SeekableArchive::open(Cursor::new(bytes), limits).map_err(|e| match e { + craft_zip::Error::TooManyEntries { .. } => corrupt("too many ZIP members"), + _ => corrupt(format!("invalid ZIP: {e}")), + })?; + if let Some(name) = z.duplicate_names().next() { + return Err(corrupt(format!("duplicate ZIP member: {}", name.chars().take(200).collect::()))); + } + Ok(z) +} + /// Read one exported `.penpot` archive without touching the filesystem or running a converter. /// Returns native, editable FigCraft nodes plus notes about unsupported features. pub fn import(bytes: &[u8]) -> Result<(Document, Vec), FormatError> { if bytes.len() > MAX_ARCHIVE { return Err(corrupt("archive exceeds 512 MiB")); } - let mut z = ZipArchive::new(Cursor::new(bytes)).map_err(|e| corrupt(format!("invalid ZIP: {e}")))?; - if z.len() > MAX_ENTRIES { - return Err(corrupt("too many ZIP members")); - } - // Reject oversized logical archives before decoding any JSON. This guards against ZIP bombs - // whose compressed size is innocuous but whose advertised uncompressed size is enormous. + let mut z = open_archive(bytes)?; + // Check declared totals and member paths before decoding any JSON. let mut expanded = 0_u64; - let mut names = BTreeSet::new(); for index in 0..z.len() { - let member = z.by_index(index).map_err(|e| corrupt(e.to_string()))?; - // An importer never extracts members to disk, but ambiguous ZIP names can - // cause one member to be inspected and another member to be read by name. - // Reject duplicates and traversal instead of guessing which version is real. - let name = member.name(); - if !names.insert(name.to_owned()) { - return Err(corrupt(format!("duplicate ZIP member: {}", name.chars().take(200).collect::()))); - } + let name = z.entry_name(index).map_err(|e| corrupt(e.to_string()))?.ok_or_else(|| corrupt("missing ZIP member"))?; if name.starts_with('/') || name.contains('\\') || name.split('/').any(|part| part == "." || part == "..") @@ -390,10 +399,14 @@ pub fn import(bytes: &[u8]) -> Result<(Document, Vec), FormatError> { { return Err(corrupt("unsafe ZIP member path")); } - expanded = expanded.saturating_add(member.size()); + let (size, _) = z.entry_sizes(index).map_err(|e| corrupt(e.to_string()))?.ok_or_else(|| corrupt("missing ZIP member"))?; + expanded = expanded.saturating_add(size); if expanded > MAX_DECOMPRESSED { return Err(corrupt("ZIP contents exceed 1 GiB")); } + // The old indexing pass also rejected entries the decoder could not open, + // even when mapping the document would never request those members. + z.open_entry_index(index, MAX_DECOMPRESSED).map_err(|e| corrupt(e.to_string()))?.ok_or_else(|| corrupt("missing ZIP member"))?; } let mut inflate_budget = InflateBudget::new(); let manifest = json(&mut z, "manifest.json", &mut inflate_budget)?; @@ -447,12 +460,12 @@ pub fn import(bytes: &[u8]) -> Result<(Document, Vec), FormatError> { // The page index files are peers of the directories containing their layers. let prefix = format!("{base}/pages/"); let page_names: Vec = z - .file_names() + .names() .filter(|name| { name.strip_prefix(&prefix).is_some_and(|part| part.len() == 41 && part.ends_with(".json") && part.get(..36).is_some_and(uuid)) }) .take(501) - .map(str::to_owned) + .map(std::borrow::Cow::into_owned) .collect(); if page_names.len() > 500 { return Err(corrupt("too many pages")); @@ -508,13 +521,13 @@ pub fn import(bytes: &[u8]) -> Result<(Document, Vec), FormatError> { } let page_prefix = format!("{prefix}{page_id}/"); let paths: Vec = z - .file_names() + .names() .filter(|name| { name.strip_prefix(&page_prefix) .is_some_and(|part| part.len() == 41 && part.ends_with(".json") && part.get(..36).is_some_and(uuid)) }) .take(MAX_LAYERS + 1) - .map(str::to_owned) + .map(std::borrow::Cow::into_owned) .collect(); if paths.len() > MAX_LAYERS { return Err(corrupt("too many layers on a page")); @@ -1439,13 +1452,13 @@ fn bind_applied_tokens(doc: &mut Document, active_modes: &BTreeMap>, base: &str, doc: &mut Document, warnings: &mut Vec, budget: &mut InflateBudget) { +fn import_document_colors(z: &mut PenpotArchive<'_>, base: &str, doc: &mut Document, warnings: &mut Vec, budget: &mut InflateBudget) { let prefix = format!("{base}/colors/"); let paths = z - .file_names() + .names() .filter(|name| name.strip_prefix(&prefix).is_some_and(|part| part.len() == 41 && part.ends_with(".json") && part.get(..36).is_some_and(uuid))) .take(2049) - .map(str::to_owned) + .map(std::borrow::Cow::into_owned) .collect::>(); if paths.len() > 2048 { warn(warnings, "Penpot color library exceeds 2048 swatches; truncated".into()); @@ -1483,7 +1496,7 @@ fn import_document_colors(z: &mut ZipArchive>, base: &str, doc: &m /// Keep original values on the text nodes: attaching a shared style automatically /// would override local formatting unless the source explicitly links it. fn import_typography_styles( - z: &mut ZipArchive>, + z: &mut PenpotArchive<'_>, base: &str, doc: &mut Document, warnings: &mut Vec, @@ -1492,10 +1505,10 @@ fn import_typography_styles( let mut custom_refs = Vec::new(); let prefix = format!("{base}/typographies/"); let paths: Vec = z - .file_names() + .names() .filter(|name| name.strip_prefix(&prefix).is_some_and(|part| part.len() == 41 && part.ends_with(".json") && part.get(..36).is_some_and(uuid))) .take(2049) - .map(str::to_owned) + .map(std::borrow::Cow::into_owned) .collect(); if paths.len() > 2048 { warn(warnings, "Penpot typography library exceeds 2048 entries; truncated".into()); @@ -2359,7 +2372,7 @@ fn import_themed_tokens( /// Import supported scalar tokens and retain their original source. fn import_token_variables( - z: &mut ZipArchive>, + z: &mut PenpotArchive<'_>, base: &str, doc: &mut Document, active_modes: &mut BTreeMap, @@ -2367,7 +2380,9 @@ fn import_token_variables( budget: &mut InflateBudget, ) -> Option { let path = format!("{base}/tokens.json"); - z.index_for_name(&path)?; + if !z.contains(&path) { + return None; + } let value = match json(z, &path, budget) { Ok(v) => v, Err(_) => { @@ -2501,16 +2516,16 @@ fn report_design_system_limits(doc: &Document, warnings: &mut Vec) { /// that original into a native FigCraft component. Copies are still independent layers until the /// Penpot override/source graph can be translated safely (see docs). fn component_masters( - z: &mut ZipArchive>, + z: &mut PenpotArchive<'_>, base: &str, budget: &mut InflateBudget, ) -> (BTreeMap, BTreeMap, BTreeMap) { let prefix = format!("{base}/components/"); let paths = z - .file_names() + .names() .filter(|name| name.strip_prefix(&prefix).is_some_and(|part| part.len() == 41 && part.ends_with(".json") && part.get(..36).is_some_and(uuid))) .take(10001) - .map(str::to_owned) + .map(std::borrow::Cow::into_owned) .collect::>(); let mut result = BTreeMap::new(); let mut variant_names = BTreeMap::new(); @@ -2971,7 +2986,7 @@ fn svg_paint_ref(text: &str) -> Option<&str> { /// What importing a page's layers shares from layer to layer: the archive and the page's shapes, /// and the document, budgets and warnings they add to. struct LayerImport<'a, 'b> { - z: &'a mut ZipArchive>, + z: &'a mut PenpotArchive<'b>, base: &'a str, shapes: &'a BTreeMap, children_by_parent: &'a BTreeMap>, @@ -4135,7 +4150,7 @@ fn effects(v: &Value, warnings: &mut Vec) -> Vec { /// Resolve a Penpot image fill through `media/.json` -> `objects/.png|jpg`. fn paints( - z: &mut ZipArchive>, + z: &mut PenpotArchive<'_>, base: &str, shape: &Value, key: &str, @@ -4170,10 +4185,7 @@ fn paints( }; if let Some(obj) = object_id { let p = format!("objects/{obj}.{extension}"); - let data = z.by_name(&p).ok().and_then(|f| { - if f.size() > MAX_IMAGE { - return None; - } + let data = z.open_entry(&p, MAX_IMAGE).ok().flatten().and_then(|f| { let mut bytes = Vec::new(); BudgetReader { inner: f, budget: &mut *budget }.take(MAX_IMAGE + 1).read_to_end(&mut bytes).ok()?; if bytes.len() as u64 > MAX_IMAGE || bytes.len() > MAX_TOTAL_IMAGES.saturating_sub(*image_bytes) { @@ -4385,7 +4397,7 @@ mod tests { writer.write_all(b"[0,0]").unwrap(); } let bytes = writer.finish().unwrap().into_inner(); - let mut archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap(); + let mut archive = open_archive(&bytes).unwrap(); let mut budget = InflateBudget { json_values: 6, ..InflateBudget::new() }; let mut page_bytes = MAX_PAGE_SHAPES_BYTES; assert_eq!(json(&mut archive, "tokens.json", &mut budget).unwrap(), serde_json::json!([0, 0])); @@ -4493,14 +4505,14 @@ mod tests { z.start_file("shape.json", SimpleFileOptions::default()).unwrap(); z.write_all(br#"{"id":"00000000-0000-0000-0000-000000000001","type":"rect"}"#).unwrap(); let bytes = z.finish().unwrap().into_inner(); - let mut archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap(); + let mut archive = open_archive(&bytes).unwrap(); let mut inflate_budget = InflateBudget::new(); let mut page_budget = 10u64; // Too small for the shape JSON (~60 bytes) let res = shape_json(&mut archive, "shape.json", &mut inflate_budget, &mut page_budget); assert!(res.is_err()); - let mut archive2 = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap(); + let mut archive2 = open_archive(&bytes).unwrap(); let mut inflate_budget2 = InflateBudget::new(); let mut page_budget2 = 1000u64; let res2 = shape_json(&mut archive2, "shape.json", &mut inflate_budget2, &mut page_budget2); @@ -4508,7 +4520,7 @@ mod tests { assert!(page_budget2 < 1000); } use std::io::Write; - use zip::write::SimpleFileOptions; + use zip::{ZipArchive, write::SimpleFileOptions}; fn archive_with_shapes(shapes: &[(&str, Value)]) -> Vec { archive_with_components(shapes, &[]) diff --git a/crates/format/src/penpot/embedded_fonts.rs b/crates/format/src/penpot/embedded_fonts.rs index d09896804..bba87462b 100644 --- a/crates/format/src/penpot/embedded_fonts.rs +++ b/crates/format/src/penpot/embedded_fonts.rs @@ -1,20 +1,19 @@ //! Embedded font detection without decoding or retaining unsupported binaries. -use super::{BudgetReader, InflateBudget, MAX_ENTRIES, warn}; -use std::io::{Cursor, Read}; -use zip::ZipArchive; +use super::{BudgetReader, InflateBudget, MAX_ENTRIES, PenpotArchive, warn}; +use std::io::Read; /// Identify unsupported fonts from signatures without decoding their payloads. -pub(super) fn report(z: &mut ZipArchive>, warnings: &mut Vec, budget: &mut InflateBudget) { +pub(super) fn report(z: &mut PenpotArchive<'_>, warnings: &mut Vec, budget: &mut InflateBudget) { let names: Vec = z - .file_names() + .names() .filter(|n| n.starts_with("objects/") || n.starts_with("fonts/") || n.starts_with("assets/fonts/")) .take(MAX_ENTRIES) - .map(str::to_owned) + .map(std::borrow::Cow::into_owned) .collect(); let mut count = 0; for name in names { - let Ok(member) = z.by_name(&name) else { continue }; + let Ok(Some(member)) = z.open_entry(&name, u64::MAX) else { continue }; let mut header = [0; 4]; if (BudgetReader { inner: member, budget }).read_exact(&mut header).is_ok() && (matches!(&header, b"OTTO" | b"true" | b"ttcf" | b"wOFF" | b"wOF2") || header == [0, 1, 0, 0]) @@ -35,7 +34,7 @@ pub(super) fn report(z: &mut ZipArchive>, warnings: &mut Vec