diff --git a/crates/penpot/src/lib.rs b/crates/penpot/src/lib.rs index aafc555c3..eda3bdf85 100644 --- a/crates/penpot/src/lib.rs +++ b/crates/penpot/src/lib.rs @@ -382,16 +382,43 @@ pub fn manifest(generated_by: &str, files: &[(&str, &str, Value)]) -> Value { serde_json::json!({"type": MANIFEST_TYPE, "version": 1, "generatedBy": generated_by, "refer": "penpot", "files": files, "relations": []}) } +/// Incremental in-memory Penpot container writer. Members are compressed as they are added; +/// the writer retains their names and archive output, not a second collection of payloads. +/// Paths must be safe and distinct. Entries keep insertion order and use deflate level 3. +/// If [`Writer::add`] fails, discard the writer: additions are not transactional, and a ZIP +/// failure may leave a reserved member name or partially written output. Do not finish or reuse +/// a writer after an error. +#[derive(Default)] +pub struct Writer { + zip: craft_zip::Writer, + seen: BTreeSet, +} + +impl Writer { + pub fn new() -> Self { + Self::default() + } + + /// Write one member immediately; the caller may release its serialized payload afterwards. + /// On any error, discard this writer rather than retrying or calling [`Writer::finish`]. + pub fn add(&mut self, path: &str, bytes: &[u8]) -> Result<(), Error> { + if path.is_empty() || !safe_name(path) || !self.seen.insert(path.to_owned()) { + return Err(Error::Io(format!("unsafe or repeated member path: {}", path.chars().take(200).collect::()))); + } + self.zip.add(path, bytes, craft_zip::Compression::Level(3)).map_err(|e| Error::Io(e.to_string())) + } + + pub fn finish(self) -> Result, Error> { + self.zip.finish().map_err(|e| Error::Io(e.to_string())) + } +} + /// A Penpot archive of `members` (path, bytes), deflated, in the order given. Paths must be safe /// and distinct, as [`Archive::open`] requires of what it reads. pub fn write_archive<'a>(members: impl IntoIterator) -> Result, Error> { - let mut z = craft_zip::Writer::new(); - let mut seen = BTreeSet::new(); + let mut writer = Writer::new(); for (path, bytes) in members { - if path.is_empty() || !safe_name(path) || !seen.insert(path) { - return Err(Error::Io(format!("unsafe or repeated member path: {}", path.chars().take(200).collect::()))); - } - z.add(path, bytes, craft_zip::Compression::Level(3)).map_err(|e| Error::Io(e.to_string()))?; + writer.add(path, bytes)?; } - z.finish().map_err(|e| Error::Io(e.to_string())) + writer.finish() } diff --git a/crates/penpot/src/tests.rs b/crates/penpot/src/tests.rs index c5f14ef72..2644157ff 100644 --- a/crates/penpot/src/tests.rs +++ b/crates/penpot/src/tests.rs @@ -438,3 +438,23 @@ fn an_archive_declaring_too_many_members_is_refused_before_it_is_indexed() { let bomb = craft_zip::zip64_entry_count_sample(MAX_ENTRIES as u64 + 1); assert_eq!(Archive::open(&bomb).err(), Some(Error::Corrupt("too many ZIP members".into()))); } + +#[test] +fn incremental_writer_keeps_members_and_rejects_ambiguous_paths() { + let all = members(); + let mut writer = Writer::new(); + for (name, bytes) in &all { + writer.add(name, bytes).unwrap(); + } + let output = writer.finish().unwrap(); + assert_eq!(output, write_archive(all.iter().map(|(n, b)| (n.as_str(), b.as_slice()))).unwrap()); + let mut archive = Archive::open(&output).unwrap(); + assert_eq!(archive.names().first().map(String::as_str), Some("manifest.json")); + assert_eq!(archive.page_shapes(FILE, PAGE).unwrap().len(), 2); + for bad in ["../x", "/abs", "a/./b", "a\\b", "", "a\u{1}b"] { + assert!(Writer::new().add(bad, b"x").is_err()); + } + let mut duplicate = Writer::new(); + duplicate.add("a", b"first").unwrap(); + assert!(duplicate.add("a", b"second").is_err()); +} diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 3611cafbb..ef1464dd4 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1010,7 +1010,7 @@ const NAMES_XML: &str = r#""#; let root = r#""#; - let bytes = zip(&[("[Content_Types].xml", types.as_bytes()), ("_rels/.rels", root.as_bytes()), ("xl/workbook.xml", xml.as_bytes())]); + let bytes = zip(&[("[Content_Types].xml", types.as_bytes()), ("_rels/.rels", root.as_bytes()), ("xl/workbook.xml", xml.as_bytes())]).unwrap(); Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap().defined_names } @@ -1146,7 +1146,8 @@ fn xml_sheet(xml: &str) -> (Vec, SheetPart) { ("xl/workbook.xml", book.as_bytes()), ("xl/_rels/workbook.xml.rels", rels.as_bytes()), ("xl/sheet.xml", xml.as_bytes()), - ]); + ]) + .unwrap(); let (rows, part, _) = sizes_of(&bytes).unwrap(); (rows, part) } @@ -1256,6 +1257,7 @@ fn styled_file(styles: &[u8], types: &str) -> Vec { ("data/two.bin", &worksheet(&[], &[])), ("look/styles.bin", styles), ]) + .unwrap() } /// The styles part of a package, and the warnings reading it gave. @@ -1440,7 +1442,8 @@ fn xml_styles(xml: &str) -> Styles { ("xl/workbook.xml", br#""#), ("xl/_rels/workbook.xml.rels", rels.as_bytes()), ("xl/styles.xml", xml.as_bytes()), - ]); + ]) + .unwrap(); styles_of(&bytes).0.unwrap() } @@ -1793,7 +1796,7 @@ fn book_with_tables(data: &[u8], tables: &[Vec]) -> Vec { ("text/strings.bin", &strings), ]; entries.extend(names.iter().map(String::as_str).zip(tables.iter().map(Vec::as_slice))); - zip(&entries) + zip(&entries).unwrap() } /// Sheet one's tables through `Package`, and its warnings. @@ -1834,7 +1837,8 @@ fn a_name_using_a_structured_reference_is_decoded_after_tables_are_indexed() { ("data/two.bin", two.as_slice()), ("text/strings.bin", strings.as_slice()), ("tables/t1.bin", table.as_slice()), - ]); + ]) + .unwrap(); let names = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap().defined_names; assert_eq!(names.iter().find(|n| n.name == "Vals").map(|n| n.formula.as_str()), Some("Sales[Val]")); let wb = read(&bytes).unwrap(); diff --git a/crates/xtask/src/report.rs b/crates/xtask/src/report.rs index f876d06f9..3c021cb97 100644 --- a/crates/xtask/src/report.rs +++ b/crates/xtask/src/report.rs @@ -90,6 +90,10 @@ mod tests { fn set_permissions(&self, file: &File, perms: Permissions) -> io::Result<()> { RealFs.set_permissions(file, perms) } + #[cfg(unix)] + fn set_group(&self, file: &File, gid: u32) -> io::Result<()> { + RealFs.set_group(file, gid) + } } fn keys(v: &Value) -> Vec<&str> {