craft-penpot: incremental container writer for bounded serialization (#1985)

This commit is contained in:
Dylan M. Taylor
2026-11-16 23:01:56 -05:00
committed by GitHub
parent af9985523a
commit 064afccce8
2 changed files with 54 additions and 7 deletions
+34 -7
View File
@@ -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<String>,
}
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::<String>())));
}
self.zip.add(path, bytes, craft_zip::Compression::Level(3)).map_err(|e| Error::Io(e.to_string()))
}
pub fn finish(self) -> Result<Vec<u8>, 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<Item = (&'a str, &'a [u8])>) -> Result<Vec<u8>, 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::<String>())));
}
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()
}
+20
View File
@@ -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());
}