From 064afccce82b01cbf85da5e265bcf897553bea2a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 23:01:56 -0500 Subject: [PATCH] craft-penpot: incremental container writer for bounded serialization (#1985) --- crates/penpot/src/lib.rs | 41 +++++++++++++++++++++++++++++++------- crates/penpot/src/tests.rs | 20 +++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) 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()); +}