diff --git a/Cargo.lock b/Cargo.lock index 68bd771e1..b56684fec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2279,6 +2279,7 @@ dependencies = [ "craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a49329e71e32ad93794ab595c41f33209a7d2220)", "craft-penpot", "craft-sketch", + "craft-zip", "figcraft-doc", "figcraft-layout", "figcraft-pathops", diff --git a/Cargo.toml b/Cargo.toml index f8e1ab433..c891c0ef4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,7 @@ craft-penpot = { git = "https://github.com/storytold/craft-libs", rev = "a49329e # The design-file formats (craft-libs): the .fig container and its kiwi encoding, the mapping of # a .fig message onto the REST file JSON, and that JSON's shared helpers. craft-sketch = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" } +craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" } craft-fig = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" } craft-figcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" } craft-figma-rest = { git = "https://github.com/storytold/craft-libs", rev = "a49329e71e32ad93794ab595c41f33209a7d2220" } diff --git a/contributors/people.toml b/contributors/people.toml index 4e238341a..64fe14637 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -128,3 +128,6 @@ sync_github_name = true [people.B-uchi] real_name = "Ibuchukwu Umeano" + +[people.Terra854] +display_name = "Terra854" diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml index 3aede66bf..2a0101b04 100644 --- a/crates/format/Cargo.toml +++ b/crates/format/Cargo.toml @@ -22,6 +22,7 @@ sha2 = { workspace = true } image = { workspace = true } craft-penpot = { workspace = true } craft-sketch = { workspace = true } +craft-zip = { workspace = true } craft-fig = { workspace = true } craft-figcraft-format = { workspace = true } craft-figma-rest = { workspace = true } diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs index 8835572da..58353e67b 100644 --- a/crates/format/src/lib.rs +++ b/crates/format/src/lib.rs @@ -559,6 +559,7 @@ pub fn load_bytes(bytes: &[u8]) -> Result<(Document, Vec), FormatError> } /// Read a member from a foreign ZIP archive, refusing entries larger than `limit`. +#[cfg(test)] pub(crate) fn read_member(z: &mut zip::ZipArchive, name: &str, limit: u64) -> Result, FormatError> { let f = z.by_name(name).map_err(|e| FormatError::Corrupt(format!("{name}: {e}")))?; if f.size() > limit { diff --git a/crates/format/src/thumbnail.rs b/crates/format/src/thumbnail.rs index f8109ea09..06c361a67 100644 --- a/crates/format/src/thumbnail.rs +++ b/crates/format/src/thumbnail.rs @@ -8,6 +8,8 @@ use std::path::Path; /// Largest `thumbnail.png` read from an archive. pub const MAX_THUMBNAIL_BYTES: u64 = craft_figcraft_format::MAX_PREVIEW_THUMBNAIL_BYTES; +/// Maximum ZIP index bytes read while locating a Sketch preview. +const MAX_SKETCH_PREVIEW_METADATA_BYTES: u64 = 64 * 1024 * 1024; /// Largest image file read as its own picture. pub const MAX_IMAGE_FILE_BYTES: u64 = 32 * 1024 * 1024; /// Widest or tallest picture decoded (pixels); a larger one gives none. @@ -33,8 +35,13 @@ pub fn thumbnail_png(archive: R) -> Option> { /// The bytes of `previews/preview.png` in a `.sketch` zip; `None` as [`thumbnail_png`] gives it. pub fn sketch_preview_png(archive: R) -> Option> { - let mut z = zip::ZipArchive::new(archive).ok()?; - crate::read_member(&mut z, "previews/preview.png", MAX_THUMBNAIL_BYTES).ok() + let limits = craft_zip::SeekableLimits { + max_entries: craft_sketch::MAX_ENTRIES, + max_metadata_bytes: MAX_SKETCH_PREVIEW_METADATA_BYTES, + max_total_bytes: MAX_THUMBNAIL_BYTES, + }; + let mut archive = craft_zip::SeekableArchive::open(archive, limits).ok()?; + archive.read("previews/preview.png", MAX_THUMBNAIL_BYTES).ok().flatten() } /// Decode a picture (PNG, JPEG, WebP, GIF, BMP or TIFF) no larger than [`MAX_THUMBNAIL_SIDE`] on diff --git a/crates/format/src/thumbnail_tests.rs b/crates/format/src/thumbnail_tests.rs index 832bd00fd..1d4f01b6f 100644 --- a/crates/format/src/thumbnail_tests.rs +++ b/crates/format/src/thumbnail_tests.rs @@ -235,6 +235,19 @@ fn previews_carry_the_first_pages_colour() { assert!(p.page.is_none() && p.picture.is_some()); } +#[test] +fn sketch_preview_rejects_archives_above_the_import_entry_limit() { + let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new())); + let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + zip.start_file("previews/preview.png", stored).unwrap(); + zip.write_all(&png(1, 1)).unwrap(); + for number in 0..craft_sketch::MAX_ENTRIES { + zip.start_file(format!("pages/{number}.json"), stored).unwrap(); + } + let bytes = zip.finish().unwrap().into_inner(); + assert!(crate::thumbnail::sketch_preview_png(Cursor::new(bytes)).is_none()); +} + #[test] fn sketch_files_show_their_preview_png() { use crate::thumbnail::{Preview, file_preview};