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()); +}