From 98a26a5c932c7896d37be374199413c71adbd282 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:56:52 +0000 Subject: [PATCH 1/4] Allow ZIP archives to own their input --- crates/zip/src/archive.rs | 54 ++++++++++++++++++++++++++++++++++--- crates/zip/tests/archive.rs | 23 ++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/crates/zip/src/archive.rs b/crates/zip/src/archive.rs index 5739e830a..49da63585 100644 --- a/crates/zip/src/archive.rs +++ b/crates/zip/src/archive.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; use std::io::{Cursor, Read}; +use std::sync::Arc; use crate::preflight::{ReadError, read_entry}; use crate::{Budget, Error, check_entry_count}; @@ -21,11 +22,26 @@ pub struct Limits { /// caller that reads through a clone keeps its own count. #[derive(Clone)] pub struct Archive<'a> { - zip: zip::ZipArchive>, + zip: zip::ZipArchive>>, budget: Budget, duplicates: bool, } +#[derive(Clone)] +enum Input<'a> { + Borrowed(&'a [u8]), + Owned(Arc>), +} + +impl AsRef<[u8]> for Input<'_> { + fn as_ref(&self) -> &[u8] { + match self { + Self::Borrowed(bytes) => bytes, + Self::Owned(bytes) => bytes.as_slice(), + } + } +} + const CENTRAL_HEADER: &[u8; 4] = b"PK\x01\x02"; const CENTRAL_HEADER_LEN: usize = 46; @@ -46,15 +62,20 @@ impl<'a> Archive<'a> { /// Indexes `bytes`. The declared entry count is checked against /// [`Limits::max_entries`] before `zip` parses the central directory, and again after. pub fn open(bytes: &'a [u8], limits: Limits) -> Result { - check_entry_count(bytes, limits.max_entries)?; - let zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| Error::NotZip(e.to_string()))?; + Self::open_input(Input::Borrowed(bytes), limits) + } + + fn open_input(input: Input<'a>, limits: Limits) -> Result { + check_entry_count(input.as_ref(), limits.max_entries)?; + let bytes = input.clone(); + let zip = zip::ZipArchive::new(Cursor::new(input)).map_err(|e| Error::NotZip(e.to_string()))?; let found = zip.len() as u64; if found > limits.max_entries { return Err(Error::TooManyEntries { declared: found, limit: limits.max_entries }); } // `zip` keeps one entry per name, so a directory with more headers than `zip` has entries // repeats a name. - let duplicates = count_central_headers(bytes, zip.central_directory_start(), zip.len().saturating_add(1)) > zip.len(); + let duplicates = count_central_headers(bytes.as_ref(), zip.central_directory_start(), zip.len().saturating_add(1)) > zip.len(); Ok(Archive { zip, budget: Budget::new("archive", limits.max_total_bytes), duplicates }) } @@ -180,3 +201,28 @@ impl<'a> Archive<'a> { &mut self.budget } } + +impl Archive<'static> { + /// Indexes owned bytes without copying them. Clones share the backing storage. + pub fn open_owned(bytes: Vec, limits: Limits) -> Result { + Self::open_input(Input::Owned(Arc::new(bytes)), limits) + } +} + +#[cfg(test)] +mod ownership_tests { + use super::*; + + #[test] + fn owned_storage_lives_until_last_archive_clone_is_dropped() { + let bytes = crate::Writer::new().finish().unwrap(); + let bytes = Arc::new(bytes); + let weak = Arc::downgrade(&bytes); + let archive = Archive::open_input(Input::Owned(bytes), Limits { max_entries: 1, max_total_bytes: 1 }).unwrap(); + let clone = archive.clone(); + drop(archive); + assert!(weak.upgrade().is_some()); + drop(clone); + assert!(weak.upgrade().is_none()); + } +} diff --git a/crates/zip/tests/archive.rs b/crates/zip/tests/archive.rs index 2c4b84113..d3b135a9c 100644 --- a/crates/zip/tests/archive.rs +++ b/crates/zip/tests/archive.rs @@ -182,3 +182,26 @@ fn a_clone_keeps_its_own_count() { assert_eq!((c.budget().used(), z.budget().used()), (3, 0)); assert!(z.read("a", 10).unwrap().is_some()); } + +#[test] +fn owned_archive_reads_and_clones_with_independent_budgets() { + let bytes = archive(&[("a", b"abc", Compression::Deflated)]); + let mut z = Archive::open_owned(bytes, Limits { max_total_bytes: 3, ..ROOMY }).unwrap(); + let mut clone = z.clone(); + assert_eq!(z.read("a", 3).unwrap().as_deref(), Some(&b"abc"[..])); + assert!(matches!(z.read("a", 3), Err(Error::BudgetSpent { .. }))); + assert_eq!(clone.read("a", 3).unwrap().as_deref(), Some(&b"abc"[..])); + assert_eq!(clone.budget().used(), 3); +} + +#[test] +fn owned_archive_applies_directory_guards() { + let mut bytes = archive(&[("a", b"1", Compression::Stored), ("b", b"2", Compression::Stored)]); + assert!(matches!(Archive::open_owned(bytes.clone(), Limits { max_entries: 1, ..ROOMY }), Err(Error::TooManyEntries { .. }))); + let local = find(&bytes, b"PK\x03\x04", 4); + bytes[local + 30] = b'a'; + let central = find(&bytes, b"PK\x01\x02", find(&bytes, b"PK\x01\x02", 0) + 4); + bytes[central + 46] = b'a'; + assert!(Archive::open_owned(bytes, ROOMY).unwrap().has_duplicate_names()); + assert!(Archive::open_owned(b"invalid".to_vec(), ROOMY).is_err()); +} From 182d47f61014734a04e487010b340fd762f9cb4e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:57:48 +0000 Subject: [PATCH 2/4] Keep container bytes in owned ZIP archive --- crates/format/src/container.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/crates/format/src/container.rs b/crates/format/src/container.rs index 32133b2b0..5544f2e9c 100644 --- a/crates/format/src/container.rs +++ b/crates/format/src/container.rs @@ -174,12 +174,10 @@ impl Writer { } } -/// A container being read. Opening reads all bytes into memory (needed because the archive -/// borrows them), checks the directory (the entry count, unique and plain names, every part's +/// A container being read. Opening reads all bytes into memory and checks the directory +/// (the entry count, unique and plain names, every part's /// declared size), not the parts. pub struct Reader { - /// The raw archive bytes, kept alive so the archive can borrow them. - bytes: Vec, archive: craft_zip::Archive<'static>, blobs: BTreeMap>, /// The bytes of payloads resolved so far, repeats counted: bounded by the expanded budget. @@ -198,13 +196,8 @@ impl Reader { let mut bytes = Vec::with_capacity(length as usize); source.read_to_end(&mut bytes).map_err(invalid)?; - // Leak the Vec to get 'static bytes (needed because Archive<'a> borrows them). - // This is acceptable: Reader holds the bytes for its lifetime anyway, and - // ZIP archives are typically small-to-moderate size. - let leaked: &'static [u8] = Box::leak(bytes.into_boxed_slice()); - - let mut archive = craft_zip::Archive::open( - leaked, + let mut archive = craft_zip::Archive::open_owned( + bytes, craft_zip::Limits { max_entries: limits.max_entries as u64, max_total_bytes: (limits.max_expanded)() as u64 }, ) .map_err(invalid)?; @@ -221,7 +214,7 @@ impl Reader { } } - Ok(Self { bytes: leaked.to_vec(), archive, blobs: BTreeMap::new(), hydrated: 0, document: None, limits }) + Ok(Self { archive, blobs: BTreeMap::new(), hydrated: 0, document: None, limits }) } /// The file's own part, `document.json`, read once and kept: provisional caches and the From e7934eaaba16a4ff3afbbe3bc32edd1bc08609fc Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:38:57 +0000 Subject: [PATCH 3/4] Fix format container Clippy diagnostics --- crates/format/src/container.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/format/src/container.rs b/crates/format/src/container.rs index 5544f2e9c..5c4f8b12f 100644 --- a/crates/format/src/container.rs +++ b/crates/format/src/container.rs @@ -82,6 +82,8 @@ pub struct PartDigest { pub digest: [u8; 32], } +type TrackedParts = (fn(&str) -> bool, Vec); + /// A container being written. pub struct Writer { zip: craft_zip::Writer, @@ -90,7 +92,7 @@ pub struct Writer { resolved: usize, limits: Limits, /// Which written parts to record for a signature, and the records so far. - tracked: Option<(fn(&str) -> bool, Vec)>, + tracked: Option, } impl Writer { @@ -111,7 +113,7 @@ impl Writer { /// The container's bytes. pub fn finish(self) -> Result, FormatError> { - Ok(self.zip.finish().map_err(invalid)?) + self.zip.finish().map_err(invalid) } /// Add a part, compressed or stored. A name already written is written once. @@ -206,11 +208,13 @@ impl Reader { // no path components that are "." or "..", and no duplicates. let mut name_set = BTreeSet::new(); for entry_idx in 0..archive.len() { - if let Ok(Some(name)) = archive.entry_name(entry_idx) { - if name.is_empty() || name.contains('\\') || name.split('/').any(|c| c.is_empty() || c == "." || c == "..") || !name_set.insert(name) - { - return Err(invalid("duplicate or invalid archive entry")); - } + if let Ok(Some(name)) = archive.entry_name(entry_idx) + && (name.is_empty() + || name.contains('\\') + || name.split('/').any(|c| c.is_empty() || c == "." || c == "..") + || !name_set.insert(name)) + { + return Err(invalid("duplicate or invalid archive entry")); } } From 76deeb796cbf52dddc6ce503fd927d2819a6caaf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:45:17 +0000 Subject: [PATCH 4/4] Keep one tracked-parts alias after parent merge --- crates/format/src/container.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/format/src/container.rs b/crates/format/src/container.rs index 9920f8431..cfacf20a1 100644 --- a/crates/format/src/container.rs +++ b/crates/format/src/container.rs @@ -82,8 +82,6 @@ pub struct PartDigest { pub digest: [u8; 32], } -type TrackedParts = (fn(&str) -> bool, Vec); - /// A container being written. pub struct Writer { zip: craft_zip::Writer,