From ecc89eccffe8a3a99b9e611c55274469f4b2d35c Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:40:58 +0100 Subject: [PATCH] Add craft-unzip: Craft Anything's hardened ZIP reader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Moves Craft Anything's crates/formats/src/zip.rs, zip_file.rs and the entry decoders from decompress.rs into a pure-Rust crate, keeping the R3.12 hardening: directory and extra-field bounds, ZIP64 end-record recovery after a prepended stub, multi-volume refusal, CRC-checked Info-ZIP Unicode Path names, decoded-size and CRC-32 checks. bzip2, LZMA/XZ and Zstandard entry methods sit behind default features; Serialize sits behind an optional serde feature. Part of #778. Co-authored-by: Afloat Co-authored-by: BFlat Co-authored-by: Brandon Thomas Co-authored-by: GGWA <65288322+rech4210@users.noreply.github.com> Co-authored-by: Max Mittelstädt <62167861+TheGalitube@users.noreply.github.com> Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Co-authored-by: RRFZ <247457381+Plastic-time@users.noreply.github.com> Co-authored-by: Roman Serikoff --- Cargo.lock | 48 ++ Cargo.toml | 1 + README.md | 1 + crates/unzip/Cargo.toml | 34 ++ crates/unzip/src/bytes.rs | 41 ++ crates/unzip/src/decode.rs | 352 +++++++++++ crates/unzip/src/file.rs | 262 +++++++++ crates/unzip/src/lib.rs | 549 ++++++++++++++++++ crates/unzip/src/testkit.rs | 80 +++ crates/unzip/src/tests.rs | 393 +++++++++++++ crates/unzip/tests/common/mod.rs | 41 ++ .../tests/zip64_end_record_compatibility.rs | 169 ++++++ crates/unzip/tests/zip64_extra_boundaries.rs | 64 ++ crates/unzip/tests/zip_decoded_size.rs | 43 ++ crates/unzip/tests/zip_directory_bounds.rs | 74 +++ crates/unzip/tests/zip_entry_detection.rs | 43 ++ crates/unzip/tests/zip_split_volumes.rs | 165 ++++++ crates/unzip/tests/zip_stored_cap.rs | 21 + 18 files changed, 2381 insertions(+) create mode 100644 crates/unzip/Cargo.toml create mode 100644 crates/unzip/src/bytes.rs create mode 100644 crates/unzip/src/decode.rs create mode 100644 crates/unzip/src/file.rs create mode 100644 crates/unzip/src/lib.rs create mode 100644 crates/unzip/src/testkit.rs create mode 100644 crates/unzip/src/tests.rs create mode 100644 crates/unzip/tests/common/mod.rs create mode 100644 crates/unzip/tests/zip64_end_record_compatibility.rs create mode 100644 crates/unzip/tests/zip64_extra_boundaries.rs create mode 100644 crates/unzip/tests/zip_decoded_size.rs create mode 100644 crates/unzip/tests/zip_directory_bounds.rs create mode 100644 crates/unzip/tests/zip_entry_detection.rs create mode 100644 crates/unzip/tests/zip_split_volumes.rs create mode 100644 crates/unzip/tests/zip_stored_cap.rs diff --git a/Cargo.lock b/Cargo.lock index 480e000e5..7a00d57d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -921,6 +921,16 @@ version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" +[[package]] +name = "bzip2-rs" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "beeb59e7e4c811ab37cc73680c798c7a5da77fc9989c62b09138e31ee740f735" +dependencies = [ + "crc32fast", + "tinyvec", +] + [[package]] name = "calloop" version = "0.13.0" @@ -2888,6 +2898,19 @@ dependencies = [ "web-sys", ] +[[package]] +name = "craft-unzip" +version = "0.1.0" +dependencies = [ + "bzip2-rs", + "lzma-rs", + "miniz_oxide 0.8.9", + "ruzstd", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-update" version = "0.1.0" @@ -3096,6 +3119,21 @@ dependencies = [ "zip", ] +[[package]] +name = "crc" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eb8a2a1cd12ab0d987a5d5e825195d372001a4094a0376319d5a0ad71c1ba0d" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "217698eaf96b4a3f0bc4f3662aaa55bdf913cd54d7204591faa790070c6d0853" + [[package]] name = "crc24" version = "0.1.6" @@ -6149,6 +6187,16 @@ version = "0.11.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "373f5eceeeab7925e0c1098212f2fbc4d416adec9d35051a6ab251e824c1854a" +[[package]] +name = "lzma-rs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "297e814c836ae64db86b36cf2a557ba54368d03f6afcd7d947c266692f71115e" +dependencies = [ + "byteorder", + "crc", +] + [[package]] name = "maybe-owned" version = "0.3.4" diff --git a/Cargo.toml b/Cargo.toml index bd08fa725..34ee6095e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -174,6 +174,7 @@ craft-xcf = { path = "crates/xcf" } craft-xml = { path = "crates/xml" } craft-zip = { path = "crates/zip" } craft-y4m = { path = "crates/y4m" } +craft-unzip = { path = "crates/unzip" } craft-epub = { path = "crates/epub" } craft-ews = { path = "crates/ews" } craft-video = { path = "crates/video" } diff --git a/README.md b/README.md index 8150dfea9..8a4c3d327 100644 --- a/README.md +++ b/README.md @@ -393,6 +393,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit - [`craft-xcf`](crates/xcf/README.md): GIMP XCF images (`.xcf`, `.xcf.gz`), read: header, properties, parasites, layers with their item paths, channels, and tiled pixels (uncompressed, RLE, zlib) at every precision, under a caller's byte budget. From PhotoCraft. - [`craft-xml`](crates/xml/README.md): bounded XML for hostile input: an owned element tree with limits on size, depth, nodes and attributes, streaming of repeated elements, namespace handling (as written, by local name, or canonical prefixes by URI), escaping and a writer. No DTDs and no entity expansion. - [`craft-zip`](crates/zip/README.md): ZIP archives from untrusted files: an entry-count check before the archive is indexed (ZIP64 included), entry reads within per-entry and total budgets that count real bytes, duplicate-name detection, a strict structure preflight, and a deterministic stored/deflate writer. The shared ZIP layer for the package formats. +- [`craft-unzip`](crates/unzip/src/lib.rs): ZIP reading over bytes already in memory (or one named entry from a seekable file), from Craft Anything: the central-directory listing with ZIP64, prepended-stub and multi-volume checks, CP437 and Info-ZIP Unicode Path names, and entry reads (stored, DEFLATE, and bzip2, LZMA, XZ and Zstandard behind default features) checked against the declared size and CRC-32. Pure Rust with no `zip` crate underneath; for inspectors that must describe damaged archives rather than refuse them. - [Shared UI component audit](docs/ui-components-audit.md): source inventory, extraction priorities and compatibility boundaries across the Craft suite. - [Theme infrastructure](docs/theme-infrastructure.md): eight-app comparison and the small shared diff --git a/crates/unzip/Cargo.toml b/crates/unzip/Cargo.toml new file mode 100644 index 000000000..8a414277b --- /dev/null +++ b/crates/unzip/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "craft-unzip" +description = "ZIP archives (APK, JAR, IPA, MSIX…) from hostile bytes, from Craft Anything: a central-directory listing with ZIP64, prepended-stub and multi-volume checks, and bounded entry reads checked against the declared size and CRC-32. Pure Rust, no I/O beyond an optional seekable reader." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +thiserror = { workspace = true } +miniz_oxide = { workspace = true } +# Entry methods beyond stored and DEFLATE. All pure Rust and wasm32-safe. +bzip2-rs = { version = "0.1", optional = true } # MIT OR Apache-2.0 +lzma-rs = { version = "0.3", optional = true } # MIT +ruzstd = { version = "0.9.1", default-features = false, features = ["std"], optional = true } # MIT +# `serde`: `Serialize` on the listing types, for apps that report listings as JSON. +serde = { workspace = true, optional = true } + +[features] +default = ["bzip2", "lzma", "zstd"] +# Method 12. +bzip2 = ["dep:bzip2-rs"] +# Methods 14 (ZIP LZMA) and 95 (XZ). +lzma = ["dep:lzma-rs"] +# Method 93. +zstd = ["dep:ruzstd"] +serde = ["dep:serde"] + +[dev-dependencies] +serde_json = { workspace = true } diff --git a/crates/unzip/src/bytes.rs b/crates/unzip/src/bytes.rs new file mode 100644 index 000000000..be7aa3ebe --- /dev/null +++ b/crates/unzip/src/bytes.rs @@ -0,0 +1,41 @@ +//! Checked little-endian reads over untrusted bytes. Every read returns `None` when out of range. + +#[derive(Debug, Clone, Copy)] +pub(crate) struct Reader<'a> { + data: &'a [u8], +} + +impl<'a> Reader<'a> { + pub(crate) fn new(data: &'a [u8]) -> Self { + Reader { data } + } + + pub(crate) fn data(&self) -> &'a [u8] { + self.data + } + + pub(crate) fn slice(&self, off: usize, len: usize) -> Option<&'a [u8]> { + let end = off.checked_add(len)?; + self.data.get(off..end) + } + + pub(crate) fn u8(&self, off: usize) -> Option { + self.data.get(off).copied() + } + + fn arr(&self, off: usize) -> Option<[u8; N]> { + self.slice(off, N)?.try_into().ok() + } + + pub(crate) fn u16_le(&self, off: usize) -> Option { + self.arr(off).map(u16::from_le_bytes) + } + + pub(crate) fn u32_le(&self, off: usize) -> Option { + self.arr(off).map(u32::from_le_bytes) + } + + pub(crate) fn u64_le(&self, off: usize) -> Option { + self.arr(off).map(u64::from_le_bytes) + } +} diff --git a/crates/unzip/src/decode.rs b/crates/unzip/src/decode.rs new file mode 100644 index 000000000..1d41d4a55 --- /dev/null +++ b/crates/unzip/src/decode.rs @@ -0,0 +1,352 @@ +//! Bounded decoders for the bzip2, ZIP LZMA, XZ and Zstandard entry methods, from Craft +//! Anything's `decompress.rs`. Each caps its output at `max_out` bytes (`TooLarge` rather than +//! allocating without limit) and never panics on a corrupt or truncated stream. + +use std::io; +#[cfg(any(feature = "bzip2", feature = "lzma"))] +use std::io::Read; +#[cfg(any(feature = "lzma", feature = "zstd"))] +use std::io::Write; + +/// Why a decode stopped. +#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)] +pub(crate) enum DecompressError { + #[error("the compressed stream is damaged")] + Corrupt, + #[error("the compressed stream ends early")] + #[cfg(any(feature = "lzma", feature = "zstd"))] + Truncated, + #[error("the output would exceed the size limit")] + TooLarge, +} + +/// A `Write` sink that collects at most `cap` bytes, then refuses the rest. The `exceeded` flag lets +/// the caller tell a size-limit stop from a genuine decode error. +#[cfg(any(feature = "lzma", feature = "zstd"))] +struct CapWrite { + buf: Vec, + cap: usize, + exceeded: bool, +} + +#[cfg(any(feature = "lzma", feature = "zstd"))] +impl CapWrite { + fn new(cap: usize) -> Self { + CapWrite { buf: Vec::new(), cap, exceeded: false } + } +} + +#[cfg(any(feature = "lzma", feature = "zstd"))] +impl Write for CapWrite { + fn write(&mut self, data: &[u8]) -> io::Result { + if self.buf.len().saturating_add(data.len()) > self.cap { + self.exceeded = true; + return Err(io::Error::other("output size limit reached")); + } + self.buf.extend_from_slice(data); + Ok(data.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +fn cap_of(max_out: u64) -> usize { + usize::try_from(max_out).unwrap_or(usize::MAX) +} + +/// Read a `Read` source into a `Vec`, capped at `cap` bytes. +#[cfg(feature = "bzip2")] +fn read_capped(mut src: impl Read, cap: usize) -> Result, DecompressError> { + let mut out = Vec::new(); + let mut chunk = [0u8; 64 * 1024]; + loop { + match src.read(&mut chunk) { + Ok(0) => return Ok(out), + Ok(n) => { + let slice = chunk.get(..n).unwrap_or(&[]); + if out.len().saturating_add(slice.len()) > cap { + return Err(DecompressError::TooLarge); + } + out.extend_from_slice(slice); + } + Err(e) if e.kind() == io::ErrorKind::Interrupted => continue, + Err(_) => return Err(DecompressError::Corrupt), + } + } +} + +#[cfg(feature = "lzma")] +/// Decompress every xz stream, including standard stream padding, within one output budget. +pub(crate) fn xz(input: &[u8], max_out: u64) -> Result, DecompressError> { + let ranges = xz_stream_ranges(input, max_out)?; + let mut out = CapWrite::new(cap_of(max_out)); + for range in ranges.into_iter().rev() { + let stream = input.get(range).ok_or(DecompressError::Truncated)?; + let mut cursor = io::Cursor::new(stream); + match lzma_rs::xz_decompress(&mut cursor, &mut out) { + Ok(()) => {} + Err(_) if out.exceeded => return Err(DecompressError::TooLarge), + Err(lzma_rs::error::Error::HeaderTooShort(_)) => return Err(DecompressError::Truncated), + Err(_) => return Err(DecompressError::Corrupt), + } + } + Ok(out.buf) +} + +#[cfg(feature = "lzma")] +/// Find stream boundaries backwards through their checked footer/index sizes, without +/// repeatedly trying the decoder at candidate magic bytes inside compressed data. +fn xz_stream_ranges(input: &[u8], max_out: u64) -> Result>, DecompressError> { + let mut ranges = Vec::new(); + let mut end = input.len(); + let mut declared = 0u64; + while end > 0 { + let padded_end = end; + while end > 0 && input.get(end - 1) == Some(&0) { + end -= 1; + } + if end == 0 || !(padded_end - end).is_multiple_of(4) { + return Err(DecompressError::Corrupt); + } + let footer_at = end.checked_sub(12).ok_or(DecompressError::Truncated)?; + let footer = input.get(footer_at..end).ok_or(DecompressError::Truncated)?; + let read = crate::bytes::Reader::new(footer); + let footer_fields = footer.get(4..10).ok_or(DecompressError::Truncated)?; + if footer.get(10..) != Some(b"YZ") || read.u32_le(0) != Some(crate::crc32(footer_fields)) { + return Err(DecompressError::Corrupt); + } + let backward = u64::from(read.u32_le(4).ok_or(DecompressError::Truncated)?); + let index_len = usize::try_from((backward + 1) * 4).map_err(|_| DecompressError::Corrupt)?; + let index_at = footer_at.checked_sub(index_len).ok_or(DecompressError::Truncated)?; + let index = input.get(index_at..footer_at).ok_or(DecompressError::Truncated)?; + let crc_at = index.len().checked_sub(4).ok_or(DecompressError::Truncated)?; + let index_read = crate::bytes::Reader::new(index); + let index_fields = index.get(..crc_at).ok_or(DecompressError::Truncated)?; + if index.first() != Some(&0) || index_read.u32_le(crc_at) != Some(crate::crc32(index_fields)) { + return Err(DecompressError::Corrupt); + } + let mut records = index.get(1..crc_at).ok_or(DecompressError::Corrupt)?; + let count = xz_vli(&mut records)?; + if count > (records.len() / 2) as u64 { + return Err(DecompressError::Corrupt); + } + let mut block_bytes = 0usize; + for _ in 0..count { + let unpadded = xz_vli(&mut records)?; + if unpadded == 0 { + return Err(DecompressError::Corrupt); + } + let padded = usize::try_from(unpadded.checked_add(3).ok_or(DecompressError::Corrupt)? & !3).map_err(|_| DecompressError::Corrupt)?; + block_bytes = block_bytes.checked_add(padded).filter(|bytes| *bytes <= index_at).ok_or(DecompressError::Corrupt)?; + declared = declared.checked_add(xz_vli(&mut records)?).filter(|size| *size <= max_out).ok_or(DecompressError::TooLarge)?; + } + if records.len() > 3 || records.iter().any(|&byte| byte != 0) { + return Err(DecompressError::Corrupt); + } + let start = index_at.checked_sub(block_bytes).and_then(|at| at.checked_sub(12)).ok_or(DecompressError::Corrupt)?; + let magic_end = start.checked_add(6).ok_or(DecompressError::Corrupt)?; + if input.get(start..magic_end) != Some(b"\xfd7zXZ\0") { + return Err(DecompressError::Corrupt); + } + ranges.push(start..end); + end = start; + } + if ranges.is_empty() { + return Err(DecompressError::Corrupt); + } + Ok(ranges) +} + +#[cfg(feature = "lzma")] +/// XZ variable-length integers have at most nine bytes and no redundant final zero. +fn xz_vli(input: &mut &[u8]) -> Result { + let mut value = 0u64; + for shift in (0..63).step_by(7) { + let (&byte, rest) = input.split_first().ok_or(DecompressError::Truncated)?; + *input = rest; + value |= u64::from(byte & 0x7f) << shift; + if byte & 0x80 == 0 { + return if shift != 0 && byte == 0 { Err(DecompressError::Corrupt) } else { Ok(value) }; + } + } + Err(DecompressError::Corrupt) +} + +#[cfg(feature = "lzma")] +/// Decode the property bytes and raw stream of a ZIP method-14 member. +pub(crate) fn lzma_zip(input: &[u8], expected_size: u64, has_end_marker: bool, max_out: u64) -> Result, DecompressError> { + let cap = cap_of(max_out).min(isize::MAX as usize); + if expected_size > cap as u64 { + return Err(DecompressError::TooLarge); + } + let (properties, stream) = input.split_at_checked(5).ok_or(DecompressError::Truncated)?; + let [property, d0, d1, d2, d3] = <[u8; 5]>::try_from(properties).map_err(|_| DecompressError::Truncated)?; + let window_limit = u32::try_from(cap).unwrap_or(u32::MAX).max(4096); + let dictionary = u32::from_le_bytes([d0, d1, d2, d3]).max(4096).min(window_limit); + let [d0, d1, d2, d3] = dictionary.to_le_bytes(); + let properties = [property, d0, d1, d2, d3]; + let size = if has_end_marker { None } else { Some(expected_size) }; + let options = lzma_rs::decompress::Options { + unpacked_size: lzma_rs::decompress::UnpackedSize::UseProvided(size), + memlimit: Some(dictionary as usize), + ..Default::default() + }; + let mut out = CapWrite::new(cap); + let mut cursor = io::Cursor::new(properties).chain(io::Cursor::new(stream)); + match lzma_rs::lzma_decompress_with_options(&mut cursor, &mut out, &options) { + Ok(()) if cursor.get_ref().1.position() == stream.len() as u64 => Ok(out.buf), + Ok(()) => Err(DecompressError::Corrupt), + Err(_) if out.exceeded => Err(DecompressError::TooLarge), + Err(lzma_rs::error::Error::HeaderTooShort(_)) => Err(DecompressError::Truncated), + Err(_) => Err(DecompressError::Corrupt), + } +} + +#[cfg(feature = "bzip2")] +/// Decompress a bzip2 stream, producing at most `max_out` bytes. +pub(crate) fn bzip2(input: &[u8], max_out: u64) -> Result, DecompressError> { + read_capped(bzip2_rs::DecoderReader::new(input), cap_of(max_out)) +} + +#[cfg(feature = "zstd")] +/// Decompress a Zstandard stream, producing at most `max_out` bytes. +pub(crate) fn zstd(mut input: &[u8], max_out: u64) -> Result, DecompressError> { + if input.is_empty() { + return Err(DecompressError::Corrupt); + } + let mut out = CapWrite::new(cap_of(max_out)); + while !input.is_empty() { + if matches!(input.get(..4), Some([0x50..=0x5f, 0x2a, 0x4d, 0x18])) { + let header = input.get(4..8).and_then(|s| s.try_into().ok()).ok_or(DecompressError::Truncated)?; + let len = u32::from_le_bytes(header) as usize; + input = input.get(8..).and_then(|body| body.get(len..)).ok_or(DecompressError::Truncated)?; + continue; + } + let mut decoder = ruzstd::decoding::StreamingDecoder::new(&mut input).map_err(|_| DecompressError::Corrupt)?; + if io::copy(&mut decoder, &mut out).is_err() { + return Err(if out.exceeded { DecompressError::TooLarge } else { DecompressError::Corrupt }); + } + } + Ok(out.buf) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::testkit as fx; + + #[cfg(feature = "lzma")] + #[test] + fn xz_round_trip() { + let c = fx::XZ_FIXTURE; + assert_eq!(xz(c, 1 << 20).unwrap(), fx::XZ_PLAIN); + // A tiny cap reports TooLarge, never a partial lie. + assert_eq!(xz(c, 4), Err(DecompressError::TooLarge)); + // Truncation never panics. + for len in 0..c.len() { + let _ = xz(&c[..len], 1 << 20); + } + } + + #[cfg(feature = "lzma")] + #[test] + fn xz_concatenated_streams_and_padding_share_one_budget() { + let input = [fx::XZ_FIXTURE, &[0; 4], fx::XZ_FIXTURE, &[0; 8]].concat(); + let expected = [fx::XZ_PLAIN, fx::XZ_PLAIN].concat(); + assert_eq!(xz(&input, expected.len() as u64).unwrap(), expected); + assert_eq!(xz(&input, expected.len() as u64 - 1), Err(DecompressError::TooLarge)); + let mut empty = Vec::new(); + lzma_rs::xz_compress(&mut io::Cursor::new(b""), &mut empty).unwrap(); + let with_empty = [&empty[..], fx::XZ_FIXTURE, &empty[..]].concat(); + assert_eq!(xz(&with_empty, fx::XZ_PLAIN.len() as u64).unwrap(), fx::XZ_PLAIN); + assert!(xz(&empty, 0).unwrap().is_empty()); + } + + #[cfg(feature = "lzma")] + #[test] + fn xz_rejects_incomplete_members_and_nonstandard_padding() { + for padding in [1, 2, 3, 5] { + let input = [fx::XZ_FIXTURE, &vec![0; padding]].concat(); + assert!(xz(&input, 1000).is_err()); + } + assert!(xz(&[&[0; 4][..], fx::XZ_FIXTURE].concat(), 1000).is_err()); + assert!(xz(&[fx::XZ_FIXTURE, b"junk"].concat(), 1000).is_err()); + for missing in 1..fx::XZ_FIXTURE.len() { + let input = [fx::XZ_FIXTURE, &fx::XZ_FIXTURE[..fx::XZ_FIXTURE.len() - missing]].concat(); + assert!(xz(&input, 1000).is_err(), "missing {missing} bytes"); + } + for at in [fx::XZ_FIXTURE.len() - 13, fx::XZ_FIXTURE.len() - 12] { + let mut damaged = fx::XZ_FIXTURE.to_vec(); + damaged[at] ^= 1; + assert!(xz(&damaged, 1000).is_err()); + } + } + + #[cfg(feature = "lzma")] + #[test] + fn xz_index_sizes_and_integer_encodings_are_checked() { + assert_eq!(xz_vli(&mut &[0x80, 0][..]), Err(DecompressError::Corrupt)); + assert_eq!(xz_vli(&mut &[0xff; 10][..]), Err(DecompressError::Corrupt)); + assert_eq!(xz_vli(&mut &[0x81, 1][..]), Ok(129)); + let mut damaged = fx::XZ_FIXTURE.to_vec(); + let footer = damaged.len() - 12; + let backward = u32::from_le_bytes(damaged[footer + 4..footer + 8].try_into().unwrap()); + let index = footer - (backward as usize + 1) * 4; + damaged[index + 2] = 0; + let checksum = crate::crc32(&damaged[index..footer - 4]); + damaged[footer - 4..footer].copy_from_slice(&checksum.to_le_bytes()); + assert!(xz(&damaged, 1000).is_err()); + } + + #[cfg(feature = "bzip2")] + #[test] + fn bzip2_round_trip() { + let c = fx::BZIP2_FIXTURE; + assert_eq!(bzip2(c, 1 << 20).unwrap(), fx::BZIP2_PLAIN); + assert_eq!(bzip2(c, 2), Err(DecompressError::TooLarge)); + for len in 0..c.len() { + let _ = bzip2(&c[..len], 1 << 20); + } + } + + #[cfg(feature = "zstd")] + #[test] + fn zstd_round_trip() { + let c = fx::ZSTD_FIXTURE; + assert_eq!(zstd(c, 1 << 20).unwrap(), fx::ZSTD_PLAIN); + assert_eq!(zstd(c, 2), Err(DecompressError::TooLarge)); + for len in 0..c.len() { + let _ = zstd(&c[..len], 1 << 20); + } + } + + #[cfg(feature = "zstd")] + #[test] + fn zstandard_members_can_contain_multiple_frames() { + let frame = fx::ZSTD_FIXTURE; + let plain = fx::ZSTD_PLAIN; + let skip = [0x50, 0x2a, 0x4d, 0x18, 3, 0, 0, 0, 1, 2, 3]; + let input = [frame, &skip, frame].concat(); + let expected = [plain, plain].concat(); + assert_eq!(zstd(&input, expected.len() as u64).unwrap(), expected); + assert_eq!(zstd(&input, plain.len() as u64), Err(DecompressError::TooLarge)); + assert!(zstd(&[frame, &skip[..9]].concat(), u64::MAX).is_err()); + assert!(zstd(&[frame, &frame[..5]].concat(), u64::MAX).is_err()); + } + + #[test] + fn garbage_never_panics() { + for seed in 0..64u64 { + let n = fx::noise(seed, 400); + #[cfg(feature = "lzma")] + let _ = xz(&n, 1 << 20); + #[cfg(feature = "bzip2")] + let _ = bzip2(&n, 1 << 20); + #[cfg(feature = "zstd")] + let _ = zstd(&n, 1 << 20); + let _ = &n; + } + } +} diff --git a/crates/unzip/src/file.rs b/crates/unzip/src/file.rs new file mode 100644 index 000000000..1bb4a3662 --- /dev/null +++ b/crates/unzip/src/file.rs @@ -0,0 +1,262 @@ +//! One entry read straight from a ZIP file without loading the file: only its end records, its +//! central directory and that entry's bytes are read. For archives too big to hold in memory just +//! to look at one small entry (a JAR's manifest). + +use crate::bytes::Reader; +use crate::{self as zip, ZipError}; +use std::io::{Read, Seek, SeekFrom}; + +/// Most central directory read (some hundreds of thousands of entries); a bigger one is refused. +const MAX_DIRECTORY: u64 = 16 << 20; +/// The ZIP64 end record as `zip::parse` reads it, without any extensible data after it. +const ZIP64_RECORD: u64 = 56; + +/// The entry named `name` (not a folder), decompressed to at most `max_bytes` and checked against +/// its CRC-32. `None`: the archive has no such entry. +pub fn read_named_entry(file: &mut (impl Read + Seek), name: &str, max_bytes: u64) -> Result>, ZipError> { + let len = file.seek(SeekFrom::End(0)).map_err(io)?; + let tail_start = len.saturating_sub(zip::EOCD_SEARCH as u64); + let tail = read_at(file, tail_start, len - tail_start)?; + let eocd = zip::find_eocd(&tail).ok_or(ZipError::NotZip)?; + let mut end = tail.get(eocd..).ok_or(ZipError::Truncated("end record"))?.to_vec(); + let r = Reader::new(&end); + let field = |at| r.u16_le(at).ok_or(ZipError::Truncated("end record")); + let (size, classic_offset) = (r.u32_le(12).ok_or(ZipError::Truncated("end record"))?, r.u32_le(16).ok_or(ZipError::Truncated("end record"))?); + // Any of these saturated makes `zip::parse` read the ZIP64 records, as below. + let saturated = [field(4)?, field(6)?, field(8)?, field(10)?].contains(&0xffff) || size == 0xffff_ffff || classic_offset == 0xffff_ffff; + let end_at = tail_start + eocd as u64; + // A ZIP64 archive puts its own end record and a locator between the directory and the classic + // end record, whether or not the classic fields are saturated. + let locator_at = end_at.checked_sub(20); + let locator = match locator_at { + Some(at) => read_at(file, at, 20)?, + None => Vec::new(), + }; + // Its stated offset doesn't count a prepended stub; it then sits right before the locator. A + // locator signature with no record at either place is other data (an entry's comment). + let record_at = match (locator_at, locator.starts_with(b"PK\x06\x07")) { + (Some(at), true) => { + let declared = Reader::new(&locator).u64_le(8).ok_or(ZipError::Truncated("zip64 locator"))?; + [declared, at.saturating_sub(ZIP64_RECORD)].into_iter().find(|&at| read_at(file, at, 4).is_ok_and(|sig| sig == b"PK\x06\x06")) + } + _ => None, + }; + let zip64 = match record_at { + Some(record_at) => { + let record = read_at(file, record_at, ZIP64_RECORD)?; + let rr = Reader::new(&record); + let size = rr.u64_le(40).ok_or(ZipError::Truncated("zip64 record"))?; + let offset = rr.u64_le(48).ok_or(ZipError::Truncated("zip64 record"))?; + Some((record_at, size, offset, record)) + } + None => None, + }; + // Unsaturated classic fields must agree with a real ZIP64 record, as `zip::parse` requires; + // one that disagrees is other data shaped like it. + let zip64 = zip64.filter(|&(_, z_size, z_offset, _)| saturated || (z_size == u64::from(size) && z_offset == u64::from(classic_offset))); + let (directory_end, size, offset, zip64) = match zip64 { + Some((record_at, size, offset, record)) => (record_at, size, offset, saturated.then_some((record, locator))), + None => (end_at, u64::from(size), u64::from(classic_offset), None), + }; + if size > MAX_DIRECTORY { + return Err(ZipError::TooLarge(MAX_DIRECTORY)); + } + // The directory ends where the end records start; anything before its stated offset is a + // prepended stub, as `zip::parse` allows. + let start = directory_end.checked_sub(size).ok_or(ZipError::Corrupt("directory starts before the file".into()))?; + let prefix = start.checked_sub(offset).ok_or(ZipError::Corrupt("directory sits before its stated offset".into()))?; + // The directory and its end records alone, their offsets rewritten for that buffer. The + // ZIP64 records go along only when the classic fields need them, as `zip::parse` reads them. + let mut directory = read_at(file, start, size)?; + if let Some((mut record, mut locator)) = zip64 { + put(&mut record, 4, &(ZIP64_RECORD - 12).to_le_bytes())?; + put(&mut record, 48, &0u64.to_le_bytes())?; + put(&mut locator, 8, &size.to_le_bytes())?; + directory.extend_from_slice(&record); + directory.extend_from_slice(&locator); + } + // A saturated classic offset stays so: `zip::parse` then takes the rewritten ZIP64 one. + if classic_offset != 0xffff_ffff { + put(&mut end, 16, &[0; 4])?; + } + directory.extend_from_slice(&end); + // `MAX_DIRECTORY` already bounds how many entries there can be. + let archive = zip::parse(&directory, usize::MAX)?; + let Some(mut entry) = archive.entries.into_iter().find(|e| e.name == name && !e.is_dir) else { return Ok(None) }; + if entry.compressed_size > max_bytes.saturating_add(1 << 16) { + return Err(ZipError::TooLarge(max_bytes)); + } + let at = entry.local_header_offset.checked_add(prefix).ok_or(ZipError::Corrupt("local header offset".into()))?; + let header = read_at(file, at, 30)?; + let fields = Reader::new(&header); + let name_len = u64::from(fields.u16_le(26).ok_or(ZipError::Truncated("local header"))?); + let extra_len = u64::from(fields.u16_le(28).ok_or(ZipError::Truncated("local header"))?); + let local_len = (30 + name_len + extra_len).checked_add(entry.compressed_size).ok_or(ZipError::Corrupt("entry size".into()))?; + let local = read_at(file, at, local_len)?; + entry.local_header_offset = 0; + zip::read_entry(&local, &entry, max_bytes).map(Some) +} + +/// Overwrite `bytes` at `at` in a record read from the file. +fn put(record: &mut [u8], at: usize, bytes: &[u8]) -> Result<(), ZipError> { + record.get_mut(at..at + bytes.len()).ok_or(ZipError::Truncated("end record"))?.copy_from_slice(bytes); + Ok(()) +} + +/// Exactly `len` bytes from `at`; a file that ends sooner is truncated. +fn read_at(file: &mut (impl Read + Seek), at: u64, len: u64) -> Result, ZipError> { + file.seek(SeekFrom::Start(at)).map_err(io)?; + let mut out = Vec::new(); + file.take(len).read_to_end(&mut out).map_err(io)?; + if (out.len() as u64) < len { + return Err(ZipError::Truncated("file")); + } + Ok(out) +} + +fn io(e: std::io::Error) -> ZipError { + ZipError::Corrupt(format!("couldn't read it: {e}")) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Cursor; + + const MANIFEST: &[u8] = b"Manifest-Version: 1.0\r\nMain-Class: app.Main\r\n\r\n"; + + fn read(bytes: &[u8], name: &str) -> Result>, ZipError> { + read_named_entry(&mut Cursor::new(bytes.to_vec()), name, 1 << 20) + } + + /// The same archive with a ZIP64 end record and locator before its classic end record, whose + /// fields at these offsets are saturated (entry counts and disk numbers 2 bytes, others 4). + fn as_zip64(archive: &[u8], saturate: &[usize]) -> Vec { + let end = archive.len() - 22; + let r = Reader::new(archive); + let (entries, size, offset) = + (u64::from(r.u16_le(end + 10).unwrap()), u64::from(r.u32_le(end + 12).unwrap()), u64::from(r.u32_le(end + 16).unwrap())); + let mut out = archive[..end].to_vec(); + let record_at = out.len() as u64; + out.extend_from_slice(b"PK\x06\x06"); + out.extend_from_slice(&44u64.to_le_bytes()); + out.extend_from_slice(&[45, 0, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + for v in [entries, entries, size, offset] { + out.extend_from_slice(&v.to_le_bytes()); + } + out.extend_from_slice(b"PK\x06\x07\0\0\0\0"); + out.extend_from_slice(&record_at.to_le_bytes()); + out.extend_from_slice(&1u32.to_le_bytes()); + let mut classic = archive[end..].to_vec(); + for &at in saturate { + let width = if at < 12 { 2 } else { 4 }; + classic[at..at + width].fill(0xff); + } + out.extend_from_slice(&classic); + out + } + + #[test] + fn an_entry_is_read_from_the_directory_without_the_whole_file() { + let filler = vec![7u8; 200_000]; + let archive = crate::testkit::zip_stored(&[("app/Big.class", &filler), ("META-INF/MANIFEST.MF", MANIFEST), ("META-INF/", b"")]); + assert_eq!(read(&archive, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST)); + assert_eq!(read(&archive, "META-INF/MISSING").unwrap(), None); + assert_eq!(read(&archive, "META-INF/").unwrap(), None, "a folder isn't an entry to read"); + + let mut stubbed = b"#!/bin/sh\nexec java -jar \"$0\" \"$@\"\n".to_vec(); + stubbed.extend_from_slice(&archive); + assert_eq!(read(&stubbed, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST), "a launcher stub before the archive"); + + assert_eq!(read(b"not a zip", "META-INF/MANIFEST.MF"), Err(ZipError::NotZip)); + assert!(read(&archive[..archive.len() - 30], "META-INF/MANIFEST.MF").is_err(), "a cut-off file"); + assert_eq!(read_named_entry(&mut Cursor::new(archive), "app/Big.class", 1000), Err(ZipError::TooLarge(1000))); + } + + #[test] + fn a_directory_bigger_than_the_searched_tail_is_read() { + let names: Vec = (0..3000).map(|i| format!("com/example/generated/Class{i:05}.class")).collect(); + let mut files: Vec<(&str, &[u8])> = names.iter().map(|n| (n.as_str(), &b"x"[..])).collect(); + files.push(("META-INF/MANIFEST.MF", MANIFEST)); + let archive = crate::testkit::zip_stored(&files); + assert_eq!(read(&archive, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST)); + } + + /// A one-entry archive whose entry is deflated, as `jar` writes a manifest. + fn zip_deflated(name: &str, body: &[u8]) -> Vec { + let packed = miniz_oxide::deflate::compress_to_vec(body, 6); + let fixed = |out: &mut Vec| { + out.extend_from_slice(&[20, 0, 0, 0, 8, 0, 0, 0, 0, 0]); + out.extend_from_slice(&zip::crc32(body).to_le_bytes()); + out.extend_from_slice(&(packed.len() as u32).to_le_bytes()); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend_from_slice(&(name.len() as u16).to_le_bytes()); + out.extend_from_slice(&[0, 0]); + }; + let mut out = b"PK\x03\x04".to_vec(); + fixed(&mut out); + out.extend_from_slice(name.as_bytes()); + out.extend_from_slice(&packed); + let directory_at = out.len() as u32; + out.extend_from_slice(b"PK\x01\x02\x14\0"); + fixed(&mut out); + out.extend_from_slice(&[0; 14]); + out.extend_from_slice(name.as_bytes()); + let directory_len = out.len() as u32 - directory_at; + out.extend_from_slice(b"PK\x05\x06\0\0\0\0\x01\0\x01\0"); + out.extend_from_slice(&directory_len.to_le_bytes()); + out.extend_from_slice(&directory_at.to_le_bytes()); + out.extend_from_slice(&[0, 0]); + out + } + + #[test] + fn a_deflated_entry_and_a_comment_that_looks_like_an_end_record() { + let deflated = zip_deflated("META-INF/MANIFEST.MF", MANIFEST); + assert_eq!(read(&deflated, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST)); + + let mut commented = crate::testkit::zip_stored(&[("META-INF/MANIFEST.MF", MANIFEST)]); + let comment = b"built by PK\x05\x06 tools"; + let at = commented.len() - 2; + commented[at..].copy_from_slice(&(comment.len() as u16).to_le_bytes()); + commented.extend_from_slice(comment); + assert_eq!(read(&commented, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST)); + } + + /// The last entry's comment ends like a ZIP64 record and locator, saying the directory is huge; + /// the classic fields disagree with it and win, as they do for `zip::parse`. + #[test] + fn a_comment_shaped_like_zip64_records_is_not_read_as_them() { + let mut archive = crate::testkit::zip_stored(&[("META-INF/MANIFEST.MF", MANIFEST)]); + let end = archive.len() - 22; + let last = archive.windows(4).rposition(|w| w == b"PK\x01\x02").unwrap(); + let mut fake = b"PK\x06\x06".to_vec(); + fake.extend_from_slice(&44u64.to_le_bytes()); + fake.extend_from_slice(&[0; 28]); + fake.extend_from_slice(&(MAX_DIRECTORY + 1).to_le_bytes()); + fake.extend_from_slice(&0u64.to_le_bytes()); + fake.extend_from_slice(b"PK\x06\x07\0\0\0\0"); + fake.extend_from_slice(&0u64.to_le_bytes()); + fake.extend_from_slice(&1u32.to_le_bytes()); + archive[last + 32..last + 34].copy_from_slice(&(fake.len() as u16).to_le_bytes()); + let size = Reader::new(&archive).u32_le(end + 12).unwrap() + fake.len() as u32; + archive[end + 12..end + 16].copy_from_slice(&size.to_le_bytes()); + archive.splice(end..end, fake); + assert!(zip::parse(&archive, 10).is_ok(), "precondition: a valid archive"); + assert_eq!(read(&archive, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST)); + } + + /// ZIP64 end records are read with any classic fields saturated or none (`zip -fz` saturates + /// only the offset), after a stub too. + #[test] + fn zip64_archives_are_read_in_parts() { + let archive = crate::testkit::zip_stored(&[("app/Main.class", b"x"), ("META-INF/MANIFEST.MF", MANIFEST)]); + for saturate in [&[][..], &[8, 10, 12, 16], &[16], &[12], &[10], &[4]] { + let zip64 = as_zip64(&archive, saturate); + assert_eq!(read(&zip64, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST), "saturated {saturate:?}"); + let mut stubbed = b"#!/bin/sh\n".to_vec(); + stubbed.extend_from_slice(&zip64); + assert_eq!(read(&stubbed, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST), "saturated {saturate:?}, after a stub"); + } + } +} diff --git a/crates/unzip/src/lib.rs b/crates/unzip/src/lib.rs new file mode 100644 index 000000000..35dfbbfcf --- /dev/null +++ b/crates/unzip/src/lib.rs @@ -0,0 +1,549 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! ZIP archives (also APK, JAR, IPA, MSIX, HAP…): central directory listing and bounded entry reads. +//! +//! Only the central directory is trusted for the listing. Entry data is located through the local +//! header, decompressed with a hard output cap, and checked against the recorded CRC-32. Stored, +//! DEFLATE, bzip2, ZIP LZMA, XZ and Zstandard entries are supported; other methods and encrypted entries are listed. +//! +//! The bzip2, LZMA/XZ and Zstandard methods sit behind the default `bzip2`, `lzma` and `zstd` +//! features; without one, entries using it are listed and reading them is +//! [`ZipError::Unsupported`]. [`file::read_named_entry`] reads one entry from a seekable file +//! without loading the archive. With the `serde` feature the listing types are `Serialize`. +//! +//! From Craft Anything (`crates/formats/src/zip.rs` and `zip_file.rs`), with its hardening: +//! directory and extra-field bounds, ZIP64 end-record recovery after a prepended stub, refusal of +//! multi-volume archives, Info-ZIP Unicode Path names checked by CRC, decoded sizes checked +//! against the directory, and CRC-32 verification. + +mod bytes; +#[cfg(any(feature = "bzip2", feature = "lzma", feature = "zstd"))] +mod decode; +pub mod file; + +use crate::bytes::Reader; + +/// Biggest tail we search for the end-of-central-directory record (64 KiB comment + record). +pub const EOCD_SEARCH: usize = 22 + 0xffff; +const ZIP64_FIXED_SIZE: usize = 56; +/// Bound the search for a record shifted by a prepended stub, including extensible data. +const ZIP64_RECOVERY_SEARCH: usize = 64 * 1024; + +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum ZipError { + #[error("not a ZIP archive (no end-of-central-directory record)")] + NotZip, + #[error("ZIP is truncated: {0}")] + Truncated(&'static str), + #[error("ZIP is corrupt: {0}")] + Corrupt(String), + #[error("unsupported ZIP feature: {0}")] + Unsupported(String), + #[error("entry is larger than the {0}-byte limit")] + TooLarge(u64), + #[error("CRC-32 mismatch (expected {expected:08x}, found {found:08x})")] + CrcMismatch { expected: u32, found: u32 }, +} + +/// One central-directory entry. +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +pub struct ZipEntry { + /// Entry name as stored (forward slashes). Bit 11 selects lossy UTF-8; otherwise + /// a valid Unicode Path extra field takes precedence over the UTF-8/CP437 fallback. + pub name: String, + pub method: u16, + pub flags: u16, + pub crc32: u32, + pub compressed_size: u64, + pub uncompressed_size: u64, + /// Offset of the local header, already adjusted for any prepended data. + pub local_header_offset: u64, + pub is_dir: bool, + pub encrypted: bool, + /// Unix mode from the external attributes (when stored by Unix or macOS). + #[cfg_attr(feature = "serde", serde(skip))] + pub unix_mode: Option, +} + +impl ZipEntry { + /// Why this entry is not a regular file or directory (symlink, device, FIFO, socket), if so. + pub fn special_kind(&self) -> Option<&'static str> { + match self.unix_mode? & 0o170000 { + 0o120000 => Some("symlink"), + 0o020000 => Some("character device"), + 0o060000 => Some("block device"), + 0o010000 => Some("FIFO"), + 0o140000 => Some("socket"), + _ => None, + } + } + + /// Human name of the compression method. + pub fn method_name(&self) -> &'static str { + match self.method { + 0 => "stored", + 8 => "deflate", + 9 => "deflate64", + 12 => "bzip2", + 14 => "lzma", + 93 => "zstd", + 95 => "xz", + 99 => "aes", + _ => "other", + } + } +} + +/// The parsed central directory. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +pub struct ZipArchive { + pub entries: Vec, + /// Entries the directory declares (may exceed `entries.len()` when truncated). + pub declared_entries: u64, + /// True when `max_entries` stopped the listing early. + pub truncated: bool, + pub zip64: bool, + /// Bytes before the archive proper (self-extracting stubs). + pub prefix_bytes: u64, + /// Inconsistent end-record metadata retained without hiding a readable directory. + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Vec::is_empty"))] + pub warnings: Vec, +} + +/// True when the bytes start like a ZIP (local header or empty archive). +pub fn looks_like_zip(data: &[u8]) -> bool { + data.starts_with(b"PK\x03\x04") || data.starts_with(b"PK\x05\x06") || data.starts_with(b"PK\x07\x08") +} + +/// Recover a shifted end record only when its fixed fields still describe the classic +/// directory. Longer records must frame their extension exactly; the adjacent fixed +/// record can still report an inconsistent size through the normal warning path. +fn recover_zip64_record(data: &[u8], loc: usize, disk_entries: u64, total: u64, cd_size: u64, cd_offset: u64) -> Option { + let r = Reader::new(data); + let last = loc.checked_sub(ZIP64_FIXED_SIZE)?; + let first = loc.saturating_sub(ZIP64_RECOVERY_SEARCH); + let candidates = data.get(first..last.checked_add(4)?)?; + candidates.windows(4).enumerate().rev().find_map(|(relative, signature)| { + if signature != b"PK\x06\x06" { + return None; + } + let rec = first.checked_add(relative)?; + let fixed = Reader::new(r.slice(rec, ZIP64_FIXED_SIZE)?); + let payload_size = fixed.u64_le(4)?; + if payload_size < 44 { + return None; + } + let declared_end = (rec as u64).checked_add(12)?.checked_add(payload_size)?; + if rec != last && declared_end != loc as u64 { + return None; + } + let record_disk_entries = fixed.u64_le(24)?; + let record_total = fixed.u64_le(32)?; + let record_cd_size = fixed.u64_le(40)?; + let record_cd_offset = fixed.u64_le(48)?; + if fixed.u32_le(16)? != 0 + || fixed.u32_le(20)? != 0 + || record_disk_entries != record_total + || (disk_entries != 0xffff && disk_entries != record_disk_entries) + || (total != 0xffff && total != record_total) + || (cd_size != 0xffff_ffff && cd_size != record_cd_size) + || (cd_offset != 0xffff_ffff && cd_offset != record_cd_offset) + || record_cd_offset.checked_add(record_cd_size)? > rec as u64 + { + return None; + } + if record_total > 0 { + let directory = rec.checked_sub(usize::try_from(record_cd_size).ok()?)?; + if record_cd_size < 46 || r.slice(directory, 4) != Some(b"PK\x01\x02") { + return None; + } + } + Some(rec) + }) +} + +/// Parse the central directory, keeping at most `max_entries`. +pub fn parse(data: &[u8], max_entries: usize) -> Result { + parse_directory(data, max_entries, None) +} + +/// Search the validated directory while retaining only the requested entry. +pub fn has_entry(data: &[u8], name: &str) -> bool { + parse_directory(data, 1, Some(name)).is_ok_and(|archive| !archive.entries.is_empty()) +} + +/// List directory entries, optionally retaining only a matching decoded name. +fn parse_directory(data: &[u8], max_entries: usize, name_filter: Option<&str>) -> Result { + let r = Reader::new(data); + let eocd = find_eocd(data).ok_or(ZipError::NotZip)?; + let mut disk = u32::from(r.u16_le(eocd + 4).ok_or(ZipError::Truncated("end record"))?); + let mut directory_disk = u32::from(r.u16_le(eocd + 6).ok_or(ZipError::Truncated("end record"))?); + let mut disk_entries = u64::from(r.u16_le(eocd + 8).ok_or(ZipError::Truncated("end record"))?); + if !matches!(disk, 0 | 0xffff) || !matches!(directory_disk, 0 | 0xffff) { + return Err(ZipError::Unsupported("multi-volume ZIP archives".into())); + } + let mut total = u64::from(r.u16_le(eocd + 10).ok_or(ZipError::Truncated("end record"))?); + let mut cd_size = u64::from(r.u32_le(eocd + 12).ok_or(ZipError::Truncated("end record"))?); + let mut cd_offset = u64::from(r.u32_le(eocd + 16).ok_or(ZipError::Truncated("end record"))?); + let mut zip64 = false; + let mut warnings = Vec::new(); + let mut eocd_record_start = eocd as u64; + if total == 0xffff || disk_entries == 0xffff || disk == 0xffff || directory_disk == 0xffff || cd_size == 0xffff_ffff || cd_offset == 0xffff_ffff { + // ZIP64 locator sits right before the classic record. + if let Some(loc) = eocd.checked_sub(20) + && r.slice(loc, 4) == Some(b"PK\x06\x07") + { + if r.u32_le(loc + 4) != Some(0) || r.u32_le(loc + 16) != Some(1) { + return Err(ZipError::Unsupported("multi-volume ZIP archives".into())); + } + let rec = r.u64_le(loc + 8).ok_or(ZipError::Truncated("zip64 locator"))?; + let rec = usize::try_from(rec).ok().filter(|&rec| r.slice(rec, 4) == Some(b"PK\x06\x06")); + let rec = rec.or_else(|| { + let recovered = recover_zip64_record(data, loc, disk_entries, total, cd_size, cd_offset); + if recovered.is_some() { + warnings.push("ZIP64 locator offset does not identify its end record; recovered the record before the locator".into()); + } + recovered + }); + if let Some(rec) = rec { + let payload_size = r.u64_le(rec + 4).ok_or(ZipError::Truncated("zip64 record size"))?; + if payload_size < 44 { + return Err(ZipError::Corrupt("ZIP64 end record is smaller than its fixed fields".into())); + } + let declared_end = (rec as u64) + .checked_add(12) + .and_then(|start| start.checked_add(payload_size)) + .ok_or_else(|| ZipError::Corrupt("ZIP64 end record extent overflows".into()))?; + // Fixed fields must be present before the locator. A bad declared extension + // length cannot make the reader borrow locator or classic end-record bytes. + if rec.checked_add(ZIP64_FIXED_SIZE).is_none_or(|end| end > loc) { + return Err(ZipError::Truncated("zip64 record fixed fields")); + } + if declared_end != loc as u64 { + warnings.push("ZIP64 end record's declared extent does not end at its locator; using bounded fixed fields".into()); + } + disk = r.u32_le(rec + 16).ok_or(ZipError::Truncated("zip64 record"))?; + directory_disk = r.u32_le(rec + 20).ok_or(ZipError::Truncated("zip64 record"))?; + let zip64_disk_entries = r.u64_le(rec + 24).ok_or(ZipError::Truncated("zip64 record"))?; + let zip64_total = r.u64_le(rec + 32).ok_or(ZipError::Truncated("zip64 record"))?; + if (disk_entries != 0xffff && disk_entries != zip64_disk_entries) || (total != 0xffff && total != zip64_total) { + return Err(ZipError::Unsupported("multi-volume ZIP archives".into())); + } + disk_entries = zip64_disk_entries; + total = zip64_total; + let zip64_cd_size = r.u64_le(rec + 40).ok_or(ZipError::Truncated("zip64 record"))?; + let zip64_cd_offset = r.u64_le(rec + 48).ok_or(ZipError::Truncated("zip64 record"))?; + if (cd_size != 0xffff_ffff && cd_size != zip64_cd_size) || (cd_offset != 0xffff_ffff && cd_offset != zip64_cd_offset) { + return Err(ZipError::Corrupt("ZIP64 and classic directory bounds disagree".into())); + } + cd_size = zip64_cd_size; + cd_offset = zip64_cd_offset; + zip64 = true; + eocd_record_start = rec as u64; + } else { + // A prepended stub can invalidate the locator's absolute offset. Keep the + // classic-directory fallback, including its existing prefix adjustment. + warnings.push("ZIP64 locator does not point to an end record; using the classic end record".into()); + } + } + } + if disk != 0 || directory_disk != 0 || disk_entries != total { + return Err(ZipError::Unsupported("multi-volume ZIP archives".into())); + } + // Self-extracting archives: the directory sits later than it claims by the stub length. + let expected_end = cd_offset.checked_add(cd_size).ok_or_else(|| ZipError::Corrupt("directory bounds overflow".into()))?; + let prefix = eocd_record_start.saturating_sub(expected_end); + let start = usize::try_from(cd_offset.saturating_add(prefix)).map_err(|_| ZipError::Corrupt("directory offset".into()))?; + let directory_end = (start as u64) + .checked_add(cd_size) + .filter(|&end| end <= eocd_record_start) + .and_then(|end| usize::try_from(end).ok()) + .ok_or_else(|| ZipError::Corrupt("directory extends into its end record".into()))?; + let mut entries = Vec::new(); + let mut pos = start; + let mut truncated = false; + let mut seen = 0u64; + while seen < total { + if entries.len() >= max_entries { + truncated = true; + break; + } + if pos.checked_add(46).is_none_or(|end| end > directory_end) { + return Err(ZipError::Truncated("directory entry")); + } + if r.slice(pos, 4) != Some(b"PK\x01\x02") { + if entries.is_empty() { + return Err(ZipError::Corrupt(format!("no central directory header at offset {pos}"))); + } + truncated = true; + break; + } + let flags = r.u16_le(pos + 8).ok_or(ZipError::Truncated("directory entry"))?; + let method = r.u16_le(pos + 10).ok_or(ZipError::Truncated("directory entry"))?; + let crc32 = r.u32_le(pos + 16).ok_or(ZipError::Truncated("directory entry"))?; + let mut csize = u64::from(r.u32_le(pos + 20).ok_or(ZipError::Truncated("directory entry"))?); + let mut usize_ = u64::from(r.u32_le(pos + 24).ok_or(ZipError::Truncated("directory entry"))?); + let name_len = usize::from(r.u16_le(pos + 28).ok_or(ZipError::Truncated("directory entry"))?); + let extra_len = usize::from(r.u16_le(pos + 30).ok_or(ZipError::Truncated("directory entry"))?); + let comment_len = usize::from(r.u16_le(pos + 32).ok_or(ZipError::Truncated("directory entry"))?); + let entry_end = pos + .checked_add(46 + name_len + extra_len + comment_len) + .filter(|&end| end <= directory_end) + .ok_or(ZipError::Truncated("directory entry fields"))?; + let host = r.slice(pos + 5, 1).and_then(|b| b.first().copied()).unwrap_or(0); + let attrs = r.u32_le(pos + 38).ok_or(ZipError::Truncated("entry attributes"))?; + let unix_mode = matches!(host, 3 | 19).then_some(attrs >> 16); + let mut local = u64::from(r.u32_le(pos + 42).ok_or(ZipError::Truncated("directory entry"))?); + let mut entry_disk = u32::from(r.u16_le(pos + 34).ok_or(ZipError::Truncated("directory entry"))?); + let name_bytes = r.slice(pos + 46, name_len).ok_or(ZipError::Truncated("entry name"))?; + let extra = r.slice(pos + 46 + name_len, extra_len).ok_or(ZipError::Truncated("entry extra fields"))?; + // Each extra field owns its payload: ZIP64 values cannot borrow a following field's bytes. + let needs_zip64 = usize_ == 0xffff_ffff || csize == 0xffff_ffff || local == 0xffff_ffff || entry_disk == 0xffff; + let mut found_zip64 = false; + let mut unicode_name = None; + let mut name_crc = None; + let mut x = 0usize; + let er = Reader::new(extra); + // Trailing bytes too short for a field header are padding, not truncation. + while x.saturating_add(4) <= extra.len() { + let id = er.u16_le(x).ok_or(ZipError::Truncated("extra field header"))?; + let len = usize::from(er.u16_le(x + 2).ok_or(ZipError::Truncated("extra field header"))?); + let body = x + 4; + let field = er.slice(body, len).ok_or(ZipError::Truncated("extra field payload"))?; + if id == 0x0001 && !found_zip64 { + let values = Reader::new(field); + let mut f = 0; + if usize_ == 0xffff_ffff { + usize_ = values.u64_le(f).ok_or(ZipError::Truncated("zip64 uncompressed size"))?; + f += 8; + } + if csize == 0xffff_ffff { + csize = values.u64_le(f).ok_or(ZipError::Truncated("zip64 compressed size"))?; + f += 8; + } + if local == 0xffff_ffff { + local = values.u64_le(f).ok_or(ZipError::Truncated("zip64 local header offset"))?; + f += 8; + } + if entry_disk == 0xffff { + entry_disk = values.u32_le(f).ok_or(ZipError::Truncated("zip64 entry disk"))?; + } + found_zip64 = true; + } else if id == 0x7075 && flags & (1 << 11) == 0 && unicode_name.is_none() { + let field = Reader::new(field); + if field.u8(0) == Some(1) && field.u32_le(1) == Some(*name_crc.get_or_insert_with(|| self::crc32(name_bytes))) { + unicode_name = field.data().get(5..).and_then(|bytes| std::str::from_utf8(bytes).ok()).filter(|s| !s.is_empty()); + } + } + x = body + len; + } + if needs_zip64 && !found_zip64 { + return Err(ZipError::Corrupt("missing ZIP64 extended information".into())); + } + if entry_disk != 0 { + return Err(ZipError::Unsupported("multi-volume ZIP archives".into())); + } + let name = unicode_name.map(str::to_owned).unwrap_or_else(|| decode_name(name_bytes, flags)).replace('\\', "/"); + if name_filter.is_none_or(|wanted| wanted == name) { + entries.push(ZipEntry { + is_dir: name.ends_with('/') + || unix_mode.is_some_and(|m| m & 0o170000 == 0o040000) + || (matches!(host, 0 | 10 | 14) && attrs & 0x10 != 0), + name, + method, + flags, + crc32, + compressed_size: csize, + uncompressed_size: usize_, + local_header_offset: local.saturating_add(prefix), + encrypted: flags & 1 != 0, + unix_mode, + }); + } + seen += 1; + pos = entry_end; + } + Ok(ZipArchive { entries, declared_entries: total, truncated, zip64, prefix_bytes: prefix, warnings }) +} + +fn decode_name(bytes: &[u8], flags: u16) -> String { + if flags & 0x0800 != 0 || bytes.is_ascii() { + return String::from_utf8_lossy(bytes).into_owned(); + } + if let Ok(name) = std::str::from_utf8(bytes) { + return name.to_owned(); + } + const CP437: [char; 128] = [ + 'Ç', 'ü', 'é', 'â', 'ä', 'à', 'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å', 'É', 'æ', 'Æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'Ö', 'Ü', '¢', + '£', '¥', '₧', 'ƒ', 'á', 'í', 'ó', 'ú', 'ñ', 'Ñ', 'ª', 'º', '¿', '⌐', '¬', '½', '¼', '¡', '«', '»', '░', '▒', '▓', '│', '┤', '╡', '╢', '╖', + '╕', '╣', '║', '╗', '╝', '╜', '╛', '┐', '└', '┴', '┬', '├', '─', '┼', '╞', '╟', '╚', '╔', '╩', '╦', '╠', '═', '╬', '╧', '╨', '╤', '╥', '╙', + '╘', '╒', '╓', '╫', '╪', '┘', '┌', '█', '▄', '▌', '▐', '▀', 'α', 'ß', 'Γ', 'π', 'Σ', 'σ', 'µ', 'τ', 'Φ', 'Θ', 'Ω', 'δ', '∞', 'φ', 'ε', '∩', + '≡', '±', '≥', '≤', '⌠', '⌡', '÷', '≈', '°', '∙', '·', '√', 'ⁿ', '²', '■', '\u{00a0}', + ]; + bytes + .iter() + .map(|&byte| if byte.is_ascii() { char::from(byte) } else { CP437.get(usize::from(byte - 0x80)).copied().unwrap_or('\u{fffd}') }) + .collect() +} + +/// Where the end-of-central-directory record starts, searching backwards through the last +/// [`EOCD_SEARCH`] bytes. +pub fn find_eocd(data: &[u8]) -> Option { + if data.len() < 22 { + return None; + } + let from = data.len().saturating_sub(EOCD_SEARCH); + let tail = data.get(from..)?; + // Search backwards so a stray signature in the comment can't shadow the real record. + let mut i = tail.len().checked_sub(22)?; + let mut found = None; + let mut last_signature = None; + loop { + if tail.get(i..i + 4) == Some(b"PK\x05\x06") { + last_signature.get_or_insert(from + i); + if let Some(comment_len) = tail.get(i + 20..i + 22).and_then(|bytes| <[u8; 2]>::try_from(bytes).ok()).map(u16::from_le_bytes) + && i + 22 + usize::from(comment_len) == tail.len() + { + found = Some(from + i); + } + } + if i == 0 { + return found.or(last_signature); + } + i -= 1; + } +} + +/// The raw (possibly compressed) bytes of an entry. +pub fn raw_entry_data<'a>(data: &'a [u8], e: &ZipEntry) -> Result<&'a [u8], ZipError> { + let r = Reader::new(data); + let off = usize::try_from(e.local_header_offset).map_err(|_| ZipError::Corrupt("local header offset".into()))?; + if r.slice(off, 4) != Some(b"PK\x03\x04") { + return Err(ZipError::Corrupt(format!("no local header for {}", e.name))); + } + let n = usize::from(r.u16_le(off + 26).ok_or(ZipError::Truncated("local header"))?); + let x = usize::from(r.u16_le(off + 28).ok_or(ZipError::Truncated("local header"))?); + let start = off.checked_add(30 + n + x).ok_or(ZipError::Truncated("local header"))?; + let len = usize::try_from(e.compressed_size).map_err(|_| ZipError::TooLarge(e.compressed_size))?; + r.slice(start, len).ok_or(ZipError::Truncated("entry data")) +} + +/// Read and decompress one entry, refusing to produce more than `max_bytes`. +pub fn read_entry(data: &[u8], e: &ZipEntry, max_bytes: u64) -> Result, ZipError> { + let out = read_entry_unverified(data, e, max_bytes)?; + let found = crc32(&out); + if found != e.crc32 { + return Err(ZipError::CrcMismatch { expected: e.crc32, found }); + } + Ok(out) +} + +/// Like [`read_entry`] but without comparing the stored CRC-32, so a caller that accepts a mismatch can still get the bytes. +pub fn read_entry_unverified(data: &[u8], e: &ZipEntry, max_bytes: u64) -> Result, ZipError> { + if e.encrypted { + return Err(ZipError::Unsupported(format!("{} is encrypted", e.name))); + } + if e.uncompressed_size > max_bytes { + return Err(ZipError::TooLarge(max_bytes)); + } + let raw = raw_entry_data(data, e)?; + let cap = usize::try_from(max_bytes).unwrap_or(usize::MAX); + let out = match e.method { + 0 if raw.len() as u64 > max_bytes => return Err(ZipError::TooLarge(max_bytes)), + 0 => raw.to_vec(), + 8 => miniz_oxide::inflate::decompress_to_vec_with_limit(raw, cap).map_err(|err| match err.status { + miniz_oxide::inflate::TINFLStatus::HasMoreOutput => ZipError::TooLarge(max_bytes), + _ => ZipError::Corrupt(format!("deflate stream for {} is damaged", e.name)), + })?, + 12 | 95 => decode_bzip2_or_xz(raw, e, max_bytes)?, + 14 => decode_lzma(raw, e, max_bytes)?, + 93 => decode_zstd(raw, e, max_bytes)?, + m => return Err(ZipError::Unsupported(format!("compression method {m} ({}) for {}", e.method_name(), e.name))), + }; + if out.len() as u64 != e.uncompressed_size { + return Err(ZipError::Corrupt(format!( + "decoded size for {} is {}, but its directory entry declares {}", + e.name, + out.len(), + e.uncompressed_size + ))); + } + Ok(out) +} + +#[cfg(any(feature = "bzip2", feature = "lzma"))] +fn decode_bzip2_or_xz(raw: &[u8], e: &ZipEntry, max_bytes: u64) -> Result, ZipError> { + let decoded = match e.method { + #[cfg(feature = "bzip2")] + 12 => decode::bzip2(raw, max_bytes), + #[cfg(feature = "lzma")] + 95 => decode::xz(raw, max_bytes), + _ => return Err(not_built(e)), + }; + decoded.map_err(|error| match error { + decode::DecompressError::TooLarge => ZipError::TooLarge(max_bytes), + _ => ZipError::Corrupt(format!("{} stream for {} is damaged: {error}", e.method_name(), e.name)), + }) +} + +#[cfg(not(any(feature = "bzip2", feature = "lzma")))] +fn decode_bzip2_or_xz(_: &[u8], e: &ZipEntry, _: u64) -> Result, ZipError> { + Err(not_built(e)) +} + +#[cfg(feature = "lzma")] +fn decode_lzma(raw: &[u8], e: &ZipEntry, max_bytes: u64) -> Result, ZipError> { + let header = Reader::new(raw); + let properties_size = header.u16_le(2).ok_or(ZipError::Truncated("LZMA properties header"))?; + if properties_size != 5 { + return Err(ZipError::Unsupported(format!("LZMA properties size {properties_size} for {}", e.name))); + } + let stream = raw.get(4..).ok_or(ZipError::Truncated("LZMA properties"))?; + decode::lzma_zip(stream, e.uncompressed_size, e.flags & 2 != 0, max_bytes).map_err(|error| match error { + decode::DecompressError::TooLarge => ZipError::TooLarge(max_bytes), + _ => ZipError::Corrupt(format!("LZMA stream for {} is damaged: {error}", e.name)), + }) +} + +#[cfg(not(feature = "lzma"))] +fn decode_lzma(_: &[u8], e: &ZipEntry, _: u64) -> Result, ZipError> { + Err(not_built(e)) +} + +#[cfg(feature = "zstd")] +fn decode_zstd(raw: &[u8], e: &ZipEntry, max_bytes: u64) -> Result, ZipError> { + decode::zstd(raw, max_bytes).map_err(|err| ZipError::Corrupt(format!("zstd stream for {} is damaged: {err}", e.name))) +} + +#[cfg(not(feature = "zstd"))] +fn decode_zstd(_: &[u8], e: &ZipEntry, _: u64) -> Result, ZipError> { + Err(not_built(e)) +} + +/// An entry whose method this build leaves out (see the crate features). +#[cfg_attr(all(feature = "bzip2", feature = "lzma", feature = "zstd"), allow(dead_code))] +fn not_built(e: &ZipEntry) -> ZipError { + ZipError::Unsupported(format!("compression method {} ({}) for {} is not built in", e.method, e.method_name(), e.name)) +} + +/// CRC-32 (IEEE, as used by ZIP and PNG). +pub fn crc32(data: &[u8]) -> u32 { + let mut crc = 0xffff_ffffu32; + for &b in data { + crc ^= u32::from(b); + for _ in 0..8 { + let mask = (crc & 1).wrapping_neg(); + crc = (crc >> 1) ^ (0xedb8_8320 & mask); + } + } + !crc +} + +#[cfg(test)] +mod testkit; + +#[cfg(test)] +mod tests; diff --git a/crates/unzip/src/testkit.rs b/crates/unzip/src/testkit.rs new file mode 100644 index 000000000..ae55cc052 --- /dev/null +++ b/crates/unzip/src/testkit.rs @@ -0,0 +1,80 @@ +//! Test fixtures from Craft Anything's testkit: known-good compressed streams built with the +//! reference encoders (`xz`, `bzip2`, `zstd`), a stored-only ZIP writer and a noise generator. + +#![allow(clippy::cast_possible_truncation, dead_code)] + +use crate::crc32; + +/// Plaintext of [`XZ_FIXTURE`] and [`LZMA_FIXTURE`]. +pub const XZ_PLAIN: &[u8] = b"Hello, xz compression world!\n"; +/// An xz stream of [`XZ_PLAIN`]. +pub const XZ_FIXTURE: &[u8] = &[ + 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x04, 0xe6, 0xd6, 0xb4, 0x46, 0x02, 0x00, 0x21, 0x01, 0x16, 0x00, 0x00, 0x00, 0x74, 0x2f, 0xe5, 0xa3, + 0x01, 0x00, 0x1c, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x78, 0x7a, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, + 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x5f, 0x47, 0xab, 0x2a, 0x4c, 0xd2, 0xb2, 0x00, 0x01, 0x35, 0x1d, + 0x44, 0x1b, 0xb1, 0xe1, 0x1f, 0xb6, 0xf3, 0x7d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x59, 0x5a, +]; +/// Plaintext of [`BZIP2_FIXTURE`]. +pub const BZIP2_PLAIN: &[u8] = b"Hello, bzip2!\n"; +/// A bzip2 stream of [`BZIP2_PLAIN`]. +pub const BZIP2_FIXTURE: &[u8] = &[ + 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x04, 0xad, 0x5a, 0x6b, 0x00, 0x00, 0x02, 0xdd, 0x80, 0x00, 0x10, 0x60, 0x04, 0x10, + 0x00, 0x00, 0x40, 0x12, 0x24, 0xc0, 0x10, 0x20, 0x00, 0x22, 0x00, 0x03, 0x42, 0x01, 0xa0, 0x05, 0x67, 0xde, 0x89, 0xa1, 0xa0, 0x80, 0xf1, 0x77, + 0x24, 0x53, 0x85, 0x09, 0x00, 0x4a, 0xd5, 0xa6, 0xb0, +]; +/// Plaintext of [`ZSTD_FIXTURE`]. +pub const ZSTD_PLAIN: &[u8] = b"Hello, zstd!\n"; +/// A Zstandard frame of [`ZSTD_PLAIN`]. +pub const ZSTD_FIXTURE: &[u8] = &[ + 0x28, 0xb5, 0x2f, 0xfd, 0x24, 0x0d, 0x69, 0x00, 0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x7a, 0x73, 0x74, 0x64, 0x21, 0x0a, 0x41, 0xb4, + 0x53, 0xb2, +]; + +/// A stored-only ZIP of `files`. +pub fn zip_stored(files: &[(&str, &[u8])]) -> Vec { + let mut out = Vec::new(); + let mut cd = Vec::new(); + for (name, body) in files { + let off = out.len() as u32; + let crc = crc32(body); + let len = body.len() as u32; + out.extend_from_slice(b"PK\x03\x04\x14\0\0\0\0\0\0\0\0\0"); + out.extend_from_slice(&crc.to_le_bytes()); + out.extend_from_slice(&len.to_le_bytes()); + out.extend_from_slice(&len.to_le_bytes()); + out.extend_from_slice(&(name.len() as u16).to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(name.as_bytes()); + out.extend_from_slice(body); + cd.extend_from_slice(b"PK\x01\x02\x14\0\x14\0\0\0\0\0\0\0\0\0"); + cd.extend_from_slice(&crc.to_le_bytes()); + cd.extend_from_slice(&len.to_le_bytes()); + cd.extend_from_slice(&len.to_le_bytes()); + cd.extend_from_slice(&(name.len() as u16).to_le_bytes()); + cd.extend_from_slice(&[0; 12]); + cd.extend_from_slice(&off.to_le_bytes()); + cd.extend_from_slice(name.as_bytes()); + } + let cd_off = out.len() as u32; + out.extend_from_slice(&cd); + out.extend_from_slice(b"PK\x05\x06\0\0\0\0"); + out.extend_from_slice(&(files.len() as u16).to_le_bytes()); + out.extend_from_slice(&(files.len() as u16).to_le_bytes()); + out.extend_from_slice(&(cd.len() as u32).to_le_bytes()); + out.extend_from_slice(&cd_off.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out +} + +/// Deterministic pseudo-random bytes (xorshift) for fuzz-like loops. +pub fn noise(seed: u64, len: usize) -> Vec { + let mut x = seed.wrapping_mul(0x9e37_79b9_7f4a_7c15) | 1; + (0..len) + .map(|_| { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + x as u8 + }) + .collect() +} diff --git a/crates/unzip/src/tests.rs b/crates/unzip/src/tests.rs new file mode 100644 index 000000000..3ea92afe4 --- /dev/null +++ b/crates/unzip/src/tests.rs @@ -0,0 +1,393 @@ +//! The reader's tests, from Craft Anything's `zip.rs`. + +use super::*; + +/// Minimal stored-only ZIP writer for tests. +pub(crate) fn build(files: &[(&str, &[u8])]) -> Vec { + let mut out = Vec::new(); + let mut cd = Vec::new(); + for (name, body) in files { + let off = out.len() as u32; + let crc = crc32(body); + out.extend_from_slice(b"PK\x03\x04"); + out.extend_from_slice(&[20, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + out.extend_from_slice(&crc.to_le_bytes()); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend_from_slice(&(name.len() as u16).to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(name.as_bytes()); + out.extend_from_slice(body); + cd.extend_from_slice(b"PK\x01\x02"); + cd.extend_from_slice(&[20, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + cd.extend_from_slice(&crc.to_le_bytes()); + cd.extend_from_slice(&(body.len() as u32).to_le_bytes()); + cd.extend_from_slice(&(body.len() as u32).to_le_bytes()); + cd.extend_from_slice(&(name.len() as u16).to_le_bytes()); + cd.extend_from_slice(&[0; 12]); + cd.extend_from_slice(&off.to_le_bytes()); + cd.extend_from_slice(name.as_bytes()); + } + let cd_off = out.len() as u32; + out.extend_from_slice(&cd); + out.extend_from_slice(b"PK\x05\x06\0\0\0\0"); + out.extend_from_slice(&(files.len() as u16).to_le_bytes()); + out.extend_from_slice(&(files.len() as u16).to_le_bytes()); + out.extend_from_slice(&(cd.len() as u32).to_le_bytes()); + out.extend_from_slice(&cd_off.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out +} + +#[cfg(all(feature = "bzip2", feature = "lzma"))] +fn build_codec_entry(method: u16, compressed: &[u8], plain: &[u8]) -> Vec { + let mut file = build(&[("entry", compressed)]); + let central = 30 + "entry".len() + compressed.len(); + let version: u16 = if method == 12 { 46 } else { 63 }; + file[4..6].copy_from_slice(&version.to_le_bytes()); + file[8..10].copy_from_slice(&method.to_le_bytes()); + file[14..18].copy_from_slice(&crc32(plain).to_le_bytes()); + file[22..26].copy_from_slice(&(plain.len() as u32).to_le_bytes()); + file[central + 6..central + 8].copy_from_slice(&version.to_le_bytes()); + file[central + 10..central + 12].copy_from_slice(&method.to_le_bytes()); + file[central + 16..central + 20].copy_from_slice(&crc32(plain).to_le_bytes()); + file[central + 24..central + 28].copy_from_slice(&(plain.len() as u32).to_le_bytes()); + file +} + +#[cfg(all(feature = "bzip2", feature = "lzma"))] +#[test] +fn bzip2_and_xz_zip_entries_keep_size_crc_and_output_checks() { + use crate::testkit::{BZIP2_FIXTURE, BZIP2_PLAIN, XZ_FIXTURE, XZ_PLAIN}; + for (method, compressed, plain) in [(12, BZIP2_FIXTURE, BZIP2_PLAIN), (95, XZ_FIXTURE, XZ_PLAIN)] { + let file = build_codec_entry(method, compressed, plain); + let original = parse(&file, 1).unwrap().entries.remove(0); + assert_eq!(read_entry(&file, &original, plain.len() as u64).unwrap(), plain); + let mut entry = original.clone(); + entry.crc32 ^= 1; + assert!(matches!(read_entry(&file, &entry, 100), Err(ZipError::CrcMismatch { .. }))); + assert_eq!(read_entry_unverified(&file, &entry, 100).unwrap(), plain); + entry = original.clone(); + entry.uncompressed_size = 0; + assert!(matches!(read_entry(&file, &entry, 100), Err(ZipError::Corrupt(_)))); + assert!(matches!(read_entry(&file, &entry, plain.len() as u64 - 1), Err(ZipError::TooLarge(_)))); + let mut damaged = file.clone(); + damaged[30 + "entry".len()] ^= 0xff; + assert!(matches!(read_entry(&damaged, &original, 100), Err(ZipError::Corrupt(_)))); + entry = original; + entry.encrypted = true; + assert!(matches!(read_entry(&file, &entry, 100), Err(ZipError::Unsupported(_)))); + } +} + +#[cfg(feature = "lzma")] +fn zip_lzma_member(plain: &[u8], end_marker: bool) -> Vec { + let mut encoded = Vec::new(); + let size = if end_marker { None } else { Some(plain.len() as u64) }; + let options = lzma_rs::compress::Options { unpacked_size: lzma_rs::compress::UnpackedSize::WriteToHeader(size) }; + lzma_rs::lzma_compress_with_options(&mut std::io::Cursor::new(plain), &mut encoded, &options).unwrap(); + let mut compressed = vec![9, 4, 5, 0]; + compressed.extend_from_slice(&encoded[..5]); + compressed.extend_from_slice(&encoded[13..]); + let mut file = build(&[("entry", &compressed)]); + let central = 30 + "entry".len() + compressed.len(); + let flags: u16 = if end_marker { 2 } else { 0 }; + for (version_at, flags_at, method_at, crc_at, size_at) in + [(4, 6, 8, 14, 22), (central + 6, central + 8, central + 10, central + 16, central + 24)] + { + file[version_at..version_at + 2].copy_from_slice(&63u16.to_le_bytes()); + file[flags_at..flags_at + 2].copy_from_slice(&flags.to_le_bytes()); + file[method_at..method_at + 2].copy_from_slice(&14u16.to_le_bytes()); + file[crc_at..crc_at + 4].copy_from_slice(&crc32(plain).to_le_bytes()); + file[size_at..size_at + 4].copy_from_slice(&(plain.len() as u32).to_le_bytes()); + } + file +} + +#[cfg(feature = "lzma")] +#[test] +fn zip_lzma_reads_size_delimited_and_end_marked_streams() { + for end_marker in [false, true] { + for plain in [&b"ZIP LZMA contents"[..], &[0; 23][..]] { + let file = zip_lzma_member(plain, end_marker); + let original = parse(&file, 1).unwrap().entries.remove(0); + assert_eq!(read_entry(&file, &original, plain.len() as u64).unwrap(), plain); + assert!(matches!(read_entry(&file, &original, plain.len() as u64 - 1), Err(ZipError::TooLarge(_)))); + let mut entry = original.clone(); + entry.crc32 ^= 1; + assert!(matches!(read_entry(&file, &entry, 100), Err(ZipError::CrcMismatch { .. }))); + entry = original.clone(); + entry.uncompressed_size -= 1; + assert!(read_entry(&file, &entry, 100).is_err()); + entry = original.clone(); + entry.compressed_size -= 1; + assert!(read_entry(&file, &entry, 100).is_err()); + entry = original; + entry.compressed_size += 1; + assert!(read_entry(&file, &entry, 100).is_err()); + } + } +} + +#[cfg(feature = "lzma")] +#[test] +fn zip_lzma_rejects_bad_properties_without_unbounded_dictionaries() { + let mut file = zip_lzma_member(b"small", true); + let entry = parse(&file, 1).unwrap().entries.remove(0); + let body = 30 + "entry".len(); + file[body + 5..body + 9].copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(read_entry(&file, &entry, 5).unwrap(), b"small"); + file[body + 4] = 255; + assert!(read_entry(&file, &entry, 5).is_err()); + file[body + 2..body + 4].copy_from_slice(&6u16.to_le_bytes()); + assert!(matches!(read_entry(&file, &entry, 5), Err(ZipError::Unsupported(_)))); +} + +fn build_named_entry(name: &[u8], flags: u16) -> Vec { + let mut zip = build(&[(&"x".repeat(name.len()), b"")]); + let central = 30 + name.len(); + zip[6..8].copy_from_slice(&flags.to_le_bytes()); + zip[30..central].copy_from_slice(name); + zip[central + 8..central + 10].copy_from_slice(&flags.to_le_bytes()); + zip[central + 46..central + 46 + name.len()].copy_from_slice(name); + zip +} + +#[test] +fn verified_unicode_path_overrides_a_legacy_zip_name() { + let raw = b"notes.txt"; + let unicode = "笔记.txt"; + for (flags, version, checksum, expected) in + [(0, 1, crc32(raw), unicode), (0, 1, crc32(b"old-name"), "notes.txt"), (0, 2, crc32(raw), "notes.txt"), (1 << 11, 1, crc32(raw), "notes.txt")] + { + let mut zip = build_named_entry(raw, flags); + let central = 30 + raw.len(); + let end = central + 46 + raw.len(); + let mut extra = 0x7075_u16.to_le_bytes().to_vec(); + extra.extend_from_slice(&((5 + unicode.len()) as u16).to_le_bytes()); + extra.push(version); + extra.extend_from_slice(&checksum.to_le_bytes()); + extra.extend_from_slice(unicode.as_bytes()); + zip[central + 30..central + 32].copy_from_slice(&(extra.len() as u16).to_le_bytes()); + zip[end + 12..end + 16].copy_from_slice(&((end - central + extra.len()) as u32).to_le_bytes()); + zip.splice(end..end, extra); + assert_eq!(parse(&zip, 1).unwrap().entries[0].name, expected); + } +} + +#[test] +fn directory_markers_do_not_require_a_trailing_slash() { + let sample = build(&[("folder", b"")]); + let central = 30 + "folder".len(); + for (host, attrs, directory, special) in [ + (0, 0x10u32, true, None), + (10, 0x10, true, None), + (3, 0o040755 << 16, true, None), + (3, 0o120777 << 16, false, Some("symlink")), + (0, 0x20, false, None), + (14, 0x10, true, None), + (14, 0x20, false, None), + (19, 0o040755 << 16, true, None), + (19, 0o100644 << 16, false, None), + (19, 0o120777 << 16, false, Some("symlink")), + (42, 0x10, false, None), + ] { + let mut file = sample.clone(); + file[central + 5] = host; + file[central + 38..central + 42].copy_from_slice(&attrs.to_le_bytes()); + let archive = parse(&file, 1).unwrap(); + let entry = &archive.entries[0]; + assert_eq!((entry.is_dir, entry.special_kind()), (directory, special), "host {host}, attributes {attrs:#x}"); + } +} + +#[test] +fn a_cp437_name_without_the_utf8_flag_is_decoded() { + let zip = build_named_entry(b"caf\x82.txt", 0); + assert_eq!(parse(&zip, 10).unwrap().entries[0].name, "café.txt"); +} + +#[test] +fn a_cp437_box_drawing_name_without_the_utf8_flag_is_decoded() { + let zip = build_named_entry(b"\xc9.txt", 0); + assert_eq!(parse(&zip, 10).unwrap().entries[0].name, "╔.txt"); +} + +#[test] +fn a_utf8_name_with_the_utf8_flag_is_preserved() { + let zip = build_named_entry("한글/é.txt".as_bytes(), 0x0800); + assert_eq!(parse(&zip, 10).unwrap().entries[0].name, "한글/é.txt"); +} + +#[test] +fn a_valid_utf8_name_without_the_utf8_flag_is_preserved() { + let zip = build_named_entry("é.txt".as_bytes(), 0); + assert_eq!(parse(&zip, 10).unwrap().entries[0].name, "é.txt"); +} + +#[test] +fn an_ascii_name_is_preserved() { + let zip = build_named_entry(b"dir/file.txt", 0); + assert_eq!(parse(&zip, 10).unwrap().entries[0].name, "dir/file.txt"); +} + +#[test] +fn valid_entry_survives_eocd_signature_in_comment() { + let mut zip = build(&[("a.txt", b"hello")]); + let comment = b"PK\x05\x06\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; + let eocd = zip.len() - 22; + zip[eocd + 20..eocd + 22].copy_from_slice(&(comment.len() as u16).to_le_bytes()); + zip.extend_from_slice(comment); + + assert_eq!(parse(&zip, 10).unwrap().entries.len(), 1); +} + +#[test] +fn valid_entry_survives_bytes_after_eocd_comment() { + let mut zip = build(&[("a.txt", b"hello")]); + zip.extend_from_slice(b"tail"); + + assert_eq!(parse(&zip, 10).unwrap().entries.len(), 1); +} + +#[test] +fn checks_the_zip64_record_extent_including_extension_bytes() { + let mut record = vec![0; 56]; + record[..4].copy_from_slice(b"PK\x06\x06"); + record[4..12].copy_from_slice(&46u64.to_le_bytes()); + record.extend([1, 2]); // extensible data + record.extend(b"PK\x06\x07"); + record.extend([0; 12]); + record.extend(1u32.to_le_bytes()); + record.extend(b"PK\x05\x06"); + record.extend([0; 4]); + record.extend([0xff; 4]); + record.extend([0; 10]); + assert!(parse(&record, 1).unwrap().zip64); + for len in [44u64, 47] { + record[4..12].copy_from_slice(&len.to_le_bytes()); + let archive = parse(&record, 1).unwrap(); + assert!(archive.zip64); + assert_eq!(archive.warnings.len(), 1); + } + for len in [43u64, u64::MAX] { + record[4..12].copy_from_slice(&len.to_le_bytes()); + assert!(parse(&record, 1).is_err(), "{len}"); + } +} + +#[test] +fn lists_and_reads_stored_entries() { + let z = build(&[("a.txt", b"hello"), ("dir/b.js", b"console.log(1)")]); + let a = parse(&z, 100).unwrap(); + assert_eq!(a.entries.len(), 2); + assert_eq!(a.entries[1].name, "dir/b.js"); + assert_eq!(read_entry(&z, &a.entries[0], 100).unwrap(), b"hello"); + assert!(matches!(read_entry(&z, &a.entries[1], 3), Err(ZipError::TooLarge(3)))); +} + +#[cfg(feature = "zstd")] +#[test] +fn zstandard_members_are_read_with_their_limits_and_checks() { + // A raw Zstandard frame holding "hello zip" (single segment, one raw block). + let body = b"hello zip"; + let mut frame = vec![0x28, 0xb5, 0x2f, 0xfd, 0x20, body.len() as u8]; + let block_header = ((body.len() as u32) << 3) | 1; + frame.extend(&block_header.to_le_bytes()[..3]); + frame.extend(body); + let mut file = vec![0u8; 30]; + file[..4].copy_from_slice(b"PK\x03\x04"); + file.extend(&frame); + let mut e = ZipEntry { + name: "x".into(), + method: 93, + flags: 0, + crc32: crc32(body), + compressed_size: frame.len() as u64, + uncompressed_size: body.len() as u64, + local_header_offset: 0, + is_dir: false, + encrypted: false, + unix_mode: None, + }; + assert_eq!(read_entry(&file, &e, 100).unwrap(), body); + assert!(matches!(read_entry(&file, &e, 3), Err(ZipError::TooLarge(3)))); + e.crc32 ^= 1; + assert!(matches!(read_entry(&file, &e, 100), Err(ZipError::CrcMismatch { .. }))); +} + +#[test] +fn deflate_round_trip_and_crc_check() { + let body = b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".repeat(20); + let comp = miniz_oxide::deflate::compress_to_vec(&body, 6); + let e = ZipEntry { + name: "x".into(), + method: 8, + flags: 0, + crc32: crc32(&body), + compressed_size: comp.len() as u64, + uncompressed_size: body.len() as u64, + local_header_offset: 0, + is_dir: false, + encrypted: false, + unix_mode: None, + }; + let mut z = b"PK\x03\x04".to_vec(); + z.extend_from_slice(&[0; 26]); + z.extend_from_slice(&comp); + assert_eq!(read_entry(&z, &e, 1 << 20).unwrap(), body); + // A lying uncompressed size can't make us exceed the cap. + let liar = ZipEntry { uncompressed_size: 10, ..e.clone() }; + assert!(matches!(read_entry(&z, &liar, 10), Err(ZipError::TooLarge(10)))); + let bad = ZipEntry { crc32: 1, ..e }; + assert!(matches!(read_entry(&z, &bad, 1 << 20), Err(ZipError::CrcMismatch { .. }))); +} + +#[test] +fn hostile_inputs_do_not_panic() { + let z = build(&[("a", b"1"), ("b", b"22")]); + for cut in 0..z.len() { + if let Ok(a) = parse(&z[..cut], 10) { + for e in &a.entries { + let _ = read_entry(&z[..cut], e, 100); + } + } + } + for i in 0..z.len() { + let mut m = z.clone(); + m[i] ^= 0xff; + if let Ok(a) = parse(&m, 10) { + for e in &a.entries { + let _ = read_entry(&m, e, 100); + } + } + } + assert_eq!(parse(b"", 1), Err(ZipError::NotZip)); +} + +#[test] +fn crc_known_value() { + assert_eq!(crc32(b"123456789"), 0xcbf4_3926); +} + +#[cfg(feature = "serde")] +#[test] +fn listings_serialize_without_empty_warnings_or_unix_mode() { + let data = crate::testkit::zip_stored(&[("a.txt", b"hi")]); + let archive = parse(&data, 10).unwrap(); + let value = serde_json::to_value(&archive).unwrap(); + assert!(value.get("warnings").is_none()); + assert_eq!(value["entries"][0]["name"], "a.txt"); + assert!(value["entries"][0].get("unix_mode").is_none()); +} + +#[cfg(not(feature = "zstd"))] +#[test] +fn a_method_left_out_of_the_build_is_unsupported() { + let mut data = crate::testkit::zip_stored(&[("a", b"abc")]); + let archive = parse(&data, 10).unwrap(); + let mut entry = archive.entries[0].clone(); + entry.method = 93; + data[8..10].copy_from_slice(&93u16.to_le_bytes()); + assert!(matches!(read_entry_unverified(&data, &entry, 100), Err(ZipError::Unsupported(m)) if m.contains("not built in"))); +} diff --git a/crates/unzip/tests/common/mod.rs b/crates/unzip/tests/common/mod.rs new file mode 100644 index 000000000..ba0f3d778 --- /dev/null +++ b/crates/unzip/tests/common/mod.rs @@ -0,0 +1,41 @@ +//! The stored-only ZIP writer the integration tests share, as in Craft Anything's testkit. + +#![allow(dead_code, clippy::cast_possible_truncation)] + +use craft_unzip::crc32; + +/// A stored-only ZIP of `files`. +pub fn zip_stored(files: &[(&str, &[u8])]) -> Vec { + let mut out = Vec::new(); + let mut cd = Vec::new(); + for (name, body) in files { + let off = out.len() as u32; + let crc = crc32(body); + let len = body.len() as u32; + out.extend_from_slice(b"PK\x03\x04\x14\0\0\0\0\0\0\0\0\0"); + out.extend_from_slice(&crc.to_le_bytes()); + out.extend_from_slice(&len.to_le_bytes()); + out.extend_from_slice(&len.to_le_bytes()); + out.extend_from_slice(&(name.len() as u16).to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(name.as_bytes()); + out.extend_from_slice(body); + cd.extend_from_slice(b"PK\x01\x02\x14\0\x14\0\0\0\0\0\0\0\0\0"); + cd.extend_from_slice(&crc.to_le_bytes()); + cd.extend_from_slice(&len.to_le_bytes()); + cd.extend_from_slice(&len.to_le_bytes()); + cd.extend_from_slice(&(name.len() as u16).to_le_bytes()); + cd.extend_from_slice(&[0; 12]); + cd.extend_from_slice(&off.to_le_bytes()); + cd.extend_from_slice(name.as_bytes()); + } + let cd_off = out.len() as u32; + out.extend_from_slice(&cd); + out.extend_from_slice(b"PK\x05\x06\0\0\0\0"); + out.extend_from_slice(&(files.len() as u16).to_le_bytes()); + out.extend_from_slice(&(files.len() as u16).to_le_bytes()); + out.extend_from_slice(&(cd.len() as u32).to_le_bytes()); + out.extend_from_slice(&cd_off.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out +} diff --git a/crates/unzip/tests/zip64_end_record_compatibility.rs b/crates/unzip/tests/zip64_end_record_compatibility.rs new file mode 100644 index 000000000..37f63cf76 --- /dev/null +++ b/crates/unzip/tests/zip64_end_record_compatibility.rs @@ -0,0 +1,169 @@ +// From Craft Anything crates/formats/tests/zip64_end_record_compatibility.rs. +use craft_unzip::{self as zip, ZipArchive, ZipError}; +mod common; +use common::zip_stored; + +fn warning_contains(archive: &ZipArchive, text: &str) -> bool { + archive.warnings.iter().any(|warning| warning.contains(text)) +} + +fn zip64_with_extension(extension: &[u8]) -> (Vec, usize) { + let mut data = zip_stored(&[("a.txt", b"retained payload")]); + let end = data.len() - 22; + let size = u32::from_le_bytes(data[end + 12..end + 16].try_into().unwrap()); + let offset = u32::from_le_bytes(data[end + 16..end + 20].try_into().unwrap()); + let mut record = b"PK\x06\x06".to_vec(); + record.extend((44 + extension.len() as u64).to_le_bytes()); + record.extend([45, 0, 45, 0]); + record.extend([0; 8]); + record.extend(1u64.to_le_bytes()); + record.extend(1u64.to_le_bytes()); + record.extend(u64::from(size).to_le_bytes()); + record.extend(u64::from(offset).to_le_bytes()); + record.extend(extension); + record.extend(b"PK\x06\x07"); + record.extend(0u32.to_le_bytes()); + record.extend((end as u64).to_le_bytes()); + record.extend(1u32.to_le_bytes()); + data[end + 8..end + 12].fill(0xff); + data.splice(end..end, record); + (data, end) +} + +#[test] +fn readable_fixed_fields_survive_inconsistent_declared_extents() { + let (original, record) = zip64_with_extension(&[1, 2, 3, 4]); + for declared in [44u64, 47, 49, 4096] { + let mut data = original.clone(); + data[record + 4..record + 12].copy_from_slice(&declared.to_le_bytes()); + let archive = zip::parse(&data, 10).unwrap(); + assert!(archive.zip64); + assert_eq!(archive.declared_entries, 1); + assert_eq!(archive.entries.len(), 1); + assert_eq!(archive.prefix_bytes, 0); + assert!(warning_contains(&archive, "extent"), "declared payload {declared}"); + assert_eq!(zip::read_entry(&data, &archive.entries[0], 100).unwrap(), b"retained payload"); + } +} + +#[test] +fn correctly_sized_extensions_need_no_warning() { + for extension in [&[][..], &[1, 2, 3, 4][..]] { + let (data, _) = zip64_with_extension(extension); + let archive = zip::parse(&data, 10).unwrap(); + assert!(archive.zip64); + assert!(archive.warnings.is_empty()); + assert_eq!(zip::read_entry(&data, &archive.entries[0], 100).unwrap(), b"retained payload"); + } +} + +#[test] +fn too_small_and_overflowing_record_extents_remain_untrusted() { + let (original, record) = zip64_with_extension(&[]); + for declared in [0u64, 43, u64::MAX - 12, u64::MAX] { + let mut data = original.clone(); + data[record + 4..record + 12].copy_from_slice(&declared.to_le_bytes()); + assert!(matches!(zip::parse(&data, 10), Err(ZipError::Corrupt(_))), "declared payload {declared}"); + } +} + +#[test] +fn fixed_fields_cannot_borrow_bytes_from_the_locator() { + let (mut data, record) = zip64_with_extension(&[]); + data.drain(record + 48..record + 56); + assert!(zip::parse(&data, 10).is_err()); +} + +#[test] +fn prepended_stubs_keep_real_zip64_directories_and_entry_offsets() { + for extension in [&[][..], &[1, 2, 3, 4][..], &[0; 1024][..]] { + let (original, _) = zip64_with_extension(extension); + for prefix in [&b"MZ"[..], &b"MZ synthetic self-extracting prefix"[..]] { + for saturated_bounds in [false, true] { + let mut data = prefix.to_vec(); + data.extend(&original); + if saturated_bounds { + let end = data.len() - 22; + data[end + 12..end + 20].fill(0xff); + } + let archive = zip::parse(&data, 10).unwrap(); + assert!(archive.zip64); + assert_eq!(archive.declared_entries, 1); + assert_eq!(archive.entries.len(), 1); + assert_eq!(archive.prefix_bytes, prefix.len() as u64); + assert_eq!(archive.entries[0].local_header_offset, prefix.len() as u64); + assert!(warning_contains(&archive, "recovered")); + assert_eq!(zip::read_entry(&data, &archive.entries[0], 100).unwrap(), b"retained payload"); + } + } + } +} + +#[test] +fn shifted_fixed_records_keep_extent_warnings() { + let (original, record) = zip64_with_extension(&[]); + let prefix = b"MZ synthetic self-extracting prefix"; + for declared in [45u64, 4096] { + let mut data = prefix.to_vec(); + data.extend(&original); + let record = record + prefix.len(); + data[record + 4..record + 12].copy_from_slice(&declared.to_le_bytes()); + let archive = zip::parse(&data, 10).unwrap(); + assert_eq!(archive.prefix_bytes, prefix.len() as u64); + assert!(warning_contains(&archive, "recovered")); + assert!(warning_contains(&archive, "extent")); + assert_eq!(zip::read_entry(&data, &archive.entries[0], 100).unwrap(), b"retained payload"); + } +} + +#[test] +fn shifted_record_recovery_has_a_bounded_lookbehind() { + let (original, _) = zip64_with_extension(&[0; 65_536]); + let mut data = b"MZ synthetic self-extracting prefix".to_vec(); + data.extend(&original); + assert!(zip::parse(&data, 10).is_err()); +} + +#[test] +fn an_unusable_locator_keeps_the_classic_directory_and_prefix_adjustment() { + // 65,535 is a valid classic count as well as a ZIP64 sentinel. A final entry comment + // can look like a locator without actually pointing to a ZIP64 end record. + let files = vec![("a.txt", &b"retained payload"[..]); 65_535]; + let original = zip_stored(&files); + let end = original.len() - 22; + let size = u32::from_le_bytes(original[end + 12..end + 16].try_into().unwrap()); + let last_entry = end - 46 - "a.txt".len(); + let mut locator = b"PK\x06\x07".to_vec(); + locator.extend(0u32.to_le_bytes()); + locator.extend(1u64.to_le_bytes()); + locator.extend(1u32.to_le_bytes()); + // A nearby record-shaped comment is still not a ZIP64 directory when its bounds + // disagree with the explicit classic fields. + let mut fake_record = [0; 56]; + fake_record[..4].copy_from_slice(b"PK\x06\x06"); + fake_record[4..12].copy_from_slice(&44u64.to_le_bytes()); + for record_shaped_comment in [&[][..], &fake_record[..]] { + let mut comment = record_shaped_comment.to_vec(); + comment.extend(&locator); + let mut archive_data = original.clone(); + archive_data[last_entry + 32..last_entry + 34].copy_from_slice(&(comment.len() as u16).to_le_bytes()); + archive_data[end + 12..end + 16].copy_from_slice(&(size + comment.len() as u32).to_le_bytes()); + archive_data.splice(end..end, comment); + for prefix in [&[][..], &b"MZ synthetic self-extracting prefix"[..]] { + for offset in [1u64, u64::MAX] { + let mut data = prefix.to_vec(); + data.extend(&archive_data); + let locator = data.len() - 22 - 20; + data[locator + 8..locator + 16].copy_from_slice(&offset.to_le_bytes()); + let archive = zip::parse(&data, 2).unwrap(); + assert!(!archive.zip64); + assert_eq!(archive.declared_entries, 65_535); + assert_eq!(archive.entries.len(), 2); + assert!(archive.truncated); + assert_eq!(archive.prefix_bytes, prefix.len() as u64); + assert!(warning_contains(&archive, "classic end record")); + assert_eq!(zip::read_entry(&data, &archive.entries[0], 100).unwrap(), b"retained payload"); + } + } + } +} diff --git a/crates/unzip/tests/zip64_extra_boundaries.rs b/crates/unzip/tests/zip64_extra_boundaries.rs new file mode 100644 index 000000000..a8c673d08 --- /dev/null +++ b/crates/unzip/tests/zip64_extra_boundaries.rs @@ -0,0 +1,64 @@ +// From Craft Anything crates/formats/tests/zip64_extra_boundaries.rs. + +mod common; +use craft_unzip as zip; + +fn archive(extra: &[u8], sentinel: usize) -> Vec { + let mut data = common::zip_stored(&[("a", b"x")]); + let central = data.windows(4).position(|w| w == b"PK\x01\x02").unwrap(); + let end = data.len() - 22; + data[central + sentinel..central + sentinel + 4].copy_from_slice(&u32::MAX.to_le_bytes()); + data[central + 30..central + 32].copy_from_slice(&(extra.len() as u16).to_le_bytes()); + data.splice(end..end, extra.iter().copied()); + let end = data.len() - 22; + data[end + 12..end + 16].copy_from_slice(&((47 + extra.len()) as u32).to_le_bytes()); + data +} + +#[test] +fn a_short_zip64_field_cannot_borrow_values_from_its_neighbor() { + for field in [20, 24, 42] { + let mut extra = vec![1, 0, 0, 0, 2, 0, 8, 0]; + extra.extend_from_slice(&1u64.to_le_bytes()); + assert!(zip::parse(&archive(&extra, field), 10).is_err(), "field {field}"); + } +} + +#[test] +fn zip64_sentinels_require_complete_values() { + for field in [20, 24, 42] { + assert!(zip::parse(&archive(&[], field), 10).is_err()); + assert!(zip::parse(&archive(&[1, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0], field), 10).is_err()); + } +} + +#[test] +fn a_complete_zip64_field_preserves_the_value() { + for field in [20, 24, 42] { + let value = if field == 42 { 0u64 } else { 1u64 }; + let mut extra = vec![1, 0, 8, 0]; + extra.extend_from_slice(&value.to_le_bytes()); + let data = archive(&extra, field); + let parsed = zip::parse(&data, 10).unwrap(); + assert_eq!(zip::read_entry(&data, &parsed.entries[0], 1).unwrap(), b"x"); + } +} + +#[test] +fn a_legitimate_64_bit_value_equal_to_the_32_bit_sentinel_is_preserved() { + let mut extra = vec![1, 0, 8, 0]; + extra.extend_from_slice(&u64::from(u32::MAX).to_le_bytes()); + let archive = zip::parse(&archive(&extra, 24), 10).unwrap(); + assert_eq!(archive.entries[0].uncompressed_size, u64::from(u32::MAX)); +} + +#[test] +fn trailing_bytes_too_short_for_a_field_header_are_padding() { + for stray in 1..=3usize { + let mut extra = vec![0xaa, 0xbb, 0, 0]; + extra.extend(std::iter::repeat_n(0u8, stray)); + let data = archive(&extra, 36); + let parsed = zip::parse(&data, 10).unwrap(); + assert_eq!(zip::read_entry(&data, &parsed.entries[0], 1).unwrap(), b"x", "stray {stray}"); + } +} diff --git a/crates/unzip/tests/zip_decoded_size.rs b/crates/unzip/tests/zip_decoded_size.rs new file mode 100644 index 000000000..5bf15ea94 --- /dev/null +++ b/crates/unzip/tests/zip_decoded_size.rs @@ -0,0 +1,43 @@ +// From Craft Anything crates/formats/tests/zip_decoded_size.rs. + +mod common; +use craft_unzip::{self as zip, ZipEntry, ZipError}; + +fn deflated(body: &[u8]) -> (Vec, ZipEntry) { + let compressed = miniz_oxide::deflate::compress_to_vec(body, 6); + let mut data = vec![0; 30]; + data[..4].copy_from_slice(b"PK\x03\x04"); + data.extend_from_slice(&compressed); + let entry = ZipEntry { + name: "payload".into(), + method: 8, + flags: 0, + crc32: zip::crc32(body), + compressed_size: compressed.len() as u64, + uncompressed_size: body.len() as u64, + local_header_offset: 0, + is_dir: false, + encrypted: false, + unix_mode: None, + }; + (data, entry) +} + +#[test] +fn decoded_length_must_match_the_directory_even_when_the_crc_matches() { + let (data, good) = deflated(b"complete body"); + for declared in [1, good.uncompressed_size + 1] { + let entry = ZipEntry { uncompressed_size: declared, ..good.clone() }; + assert!(matches!(zip::read_entry(&data, &entry, 100), Err(ZipError::Corrupt(_)))); + assert!(matches!(zip::read_entry_unverified(&data, &entry, 100), Err(ZipError::Corrupt(_)))); + } + assert_eq!(zip::read_entry(&data, &good, 100).unwrap(), b"complete body"); +} + +#[test] +fn stored_length_must_also_match_the_directory() { + let data = common::zip_stored(&[("payload", b"hello")]); + let mut entry = zip::parse(&data, 10).unwrap().entries.remove(0); + entry.uncompressed_size = 4; + assert!(matches!(zip::read_entry(&data, &entry, 100), Err(ZipError::Corrupt(_)))); +} diff --git a/crates/unzip/tests/zip_directory_bounds.rs b/crates/unzip/tests/zip_directory_bounds.rs new file mode 100644 index 000000000..3f594dd74 --- /dev/null +++ b/crates/unzip/tests/zip_directory_bounds.rs @@ -0,0 +1,74 @@ +// From Craft Anything crates/formats/tests/zip_directory_bounds.rs. +use craft_unzip::{self as zip, ZipError}; +mod common; +use common::zip_stored; + +#[test] +fn a_short_declared_entry_is_rejected_before_its_signature_is_checked() { + for remaining in 0..46 { + let mut data = zip_stored(&[("a", b"x")]); + let end = data.len() - 22; + let size = u32::from_le_bytes(data[end + 12..end + 16].try_into().unwrap()); + data[end + 8..end + 12].copy_from_slice(&[2, 0, 2, 0]); + data[end + 12..end + 16].copy_from_slice(&(size + remaining).to_le_bytes()); + data.splice(end..end, vec![0; remaining as usize]); + assert!(matches!(zip::parse(&data, 10), Err(ZipError::Truncated("directory entry"))), "remaining {remaining} bytes"); + } +} + +#[test] +fn directory_names_and_comments_cannot_borrow_end_record_bytes() { + for field in [28, 32] { + let mut data = zip_stored(&[("a", b"x")]); + let central = data.windows(4).position(|w| w == b"PK\x01\x02").unwrap(); + let length = if field == 28 { 2u16 } else { 1 }; + data[central + field..central + field + 2].copy_from_slice(&length.to_le_bytes()); + assert!(zip::parse(&data, 10).is_err(), "field {field}"); + } +} + +#[test] +fn directory_extra_fields_cannot_borrow_end_record_bytes() { + let mut data = zip_stored(&[("a", b"x")]); + let central = data.windows(4).position(|w| w == b"PK\x01\x02").unwrap(); + data[central + 30..central + 32].copy_from_slice(&2u16.to_le_bytes()); + assert_eq!(zip::parse(&data, 10), Err(ZipError::Truncated("directory entry fields"))); +} + +#[test] +fn a_directory_cannot_extend_into_its_end_record() { + let mut data = zip_stored(&[("a", b"x")]); + let end = data.len() - 22; + let size = u32::from_le_bytes(data[end + 12..end + 16].try_into().unwrap()); + data[end + 12..end + 16].copy_from_slice(&(size + 1).to_le_bytes()); + assert!(zip::parse(&data, 10).is_err()); +} + +#[test] +fn self_extracting_archives_keep_both_offset_conventions() { + for adjusted in [false, true] { + let mut data = zip_stored(&[("a", b"x")]); + let central = data.windows(4).position(|w| w == b"PK\x01\x02").unwrap(); + let end = data.len() - 22; + let stub = b"executable stub"; + if adjusted { + data[central + 42..central + 46].copy_from_slice(&(stub.len() as u32).to_le_bytes()); + data[end + 16..end + 20].copy_from_slice(&((central + stub.len()) as u32).to_le_bytes()); + } + let mut file = stub.to_vec(); + file.extend(data); + let archive = zip::parse(&file, 10).unwrap(); + assert_eq!(zip::read_entry(&file, &archive.entries[0], 10).unwrap(), b"x"); + } +} + +#[test] +fn a_directory_signature_after_the_entries_is_not_an_extra_file() { + let mut data = zip_stored(&[("a", b"x")]); + let end = data.len() - 22; + let size = u32::from_le_bytes(data[end + 12..end + 16].try_into().unwrap()); + let signature = b"PK\x05\x05\x01\x00x"; + data[end + 12..end + 16].copy_from_slice(&(size + signature.len() as u32).to_le_bytes()); + data.splice(end..end, signature.iter().copied()); + assert_eq!(zip::parse(&data, 10).unwrap().entries.len(), 1); +} diff --git a/crates/unzip/tests/zip_entry_detection.rs b/crates/unzip/tests/zip_entry_detection.rs new file mode 100644 index 000000000..8a74ecf63 --- /dev/null +++ b/crates/unzip/tests/zip_entry_detection.rs @@ -0,0 +1,43 @@ +// From Craft Anything crates/formats/tests/zip_entry_detection.rs. +use craft_unzip::has_entry as zip_has_entry; +mod common; +use common::zip_stored; + +/// A directory-shaped payload is ordinary file content, not an archive member. +#[test] +fn embedded_archive_does_not_supply_outer_entries() { + let inner = zip_stored(&[("AndroidManifest.xml", b"manifest")]); + let outer = zip_stored(&[("backup.zip", &inner)]); + assert!(!zip_has_entry(&outer, "AndroidManifest.xml")); + assert!(zip_has_entry(&outer, "backup.zip")); +} + +/// A ZIP comment can contain another directory without changing the archive's entries. +#[test] +fn comment_does_not_supply_entries() { + let mut outer = zip_stored(&[("readme.txt", b"notes")]); + let inner = zip_stored(&[("AndroidManifest.xml", b"manifest")]); + let len = outer.len(); + outer[len - 2..].copy_from_slice(&(inner.len() as u16).to_le_bytes()); + outer.extend(inner); + assert!(!zip_has_entry(&outer, "AndroidManifest.xml")); + assert!(zip_has_entry(&outer, "readme.txt")); +} + +/// Actual manifests are found regardless of member order, including prefixed archives. +#[test] +fn actual_entries_are_found() { + let data = zip_stored(&[("classes.dex", b"dex"), ("AndroidManifest.xml", b"manifest")]); + assert!(zip_has_entry(&data, "AndroidManifest.xml")); + let mut prefixed = b"stub".to_vec(); + prefixed.extend(data); + assert!(zip_has_entry(&prefixed, "AndroidManifest.xml")); + assert!(!zip_has_entry(&prefixed, "missing")); +} + +/// A stray central-directory signature without an archive is not an entry. +#[test] +fn incomplete_archive_is_not_a_manifest() { + let data = zip_stored(&[("AndroidManifest.xml", b"manifest")]); + assert!(!zip_has_entry(&data[..data.len() - 22], "AndroidManifest.xml")); +} diff --git a/crates/unzip/tests/zip_split_volumes.rs b/crates/unzip/tests/zip_split_volumes.rs new file mode 100644 index 000000000..c5412b72e --- /dev/null +++ b/crates/unzip/tests/zip_split_volumes.rs @@ -0,0 +1,165 @@ +// From Craft Anything crates/formats/tests/zip_split_volumes.rs. +use craft_unzip::{self as zip, ZipError}; +mod common; +use common::zip_stored; + +fn assert_split(data: &[u8]) { + assert!(matches!(zip::parse(data, 10), Err(ZipError::Unsupported(_)))); +} + +#[test] +fn classic_end_records_must_describe_one_complete_volume() { + for field in [4, 6, 8] { + let mut data = zip_stored(&[("a", b"x")]); + let end = data.len() - 22; + let value = if field == 8 { 0u16 } else { 1 }; + data[end + field..end + field + 2].copy_from_slice(&value.to_le_bytes()); + assert_split(&data); + } +} + +#[test] +fn central_entries_cannot_point_to_another_volume() { + let mut data = zip_stored(&[("a", b"x")]); + let central = data.windows(4).position(|w| w == b"PK\x01\x02").unwrap(); + data[central + 34..central + 36].copy_from_slice(&1u16.to_le_bytes()); + assert_split(&data); +} + +fn zip64_entry_disk(disk: u32, with_sizes_and_offset: bool) -> Vec { + let mut data = zip_stored(&[("a", b"x")]); + let central = data.windows(4).position(|w| w == b"PK\x01\x02").unwrap(); + let end = data.len() - 22; + data[central + 34..central + 36].copy_from_slice(&u16::MAX.to_le_bytes()); + let mut extra = vec![1, 0, 4, 0]; + if with_sizes_and_offset { + data[central + 20..central + 28].fill(0xff); + data[central + 42..central + 46].fill(0xff); + extra[2..4].copy_from_slice(&28u16.to_le_bytes()); + // Uncompressed size, compressed size, and local header offset precede the disk. + for value in [1u64, 1, 0] { + extra.extend(value.to_le_bytes()); + } + } + extra.extend(disk.to_le_bytes()); + data[central + 30..central + 32].copy_from_slice(&(extra.len() as u16).to_le_bytes()); + let directory_size = (end - central + extra.len()) as u32; + data.splice(end..end, extra); + let end = data.len() - 22; + data[end + 12..end + 16].copy_from_slice(&directory_size.to_le_bytes()); + data +} + +#[test] +fn a_zip64_disk_sentinel_is_resolved_before_rejecting_the_entry() { + let data = zip64_entry_disk(0, false); + let archive = zip::parse(&data, 10).unwrap(); + assert_eq!(zip::read_entry(&data, &archive.entries[0], 10).unwrap(), b"x"); + assert_split(&zip64_entry_disk(1, false)); +} + +#[test] +fn a_zip64_disk_sentinel_follows_sizes_and_the_local_offset() { + let data = zip64_entry_disk(0, true); + let archive = zip::parse(&data, 10).unwrap(); + assert_eq!(archive.entries[0].uncompressed_size, 1); + assert_eq!(archive.entries[0].compressed_size, 1); + assert_eq!(archive.entries[0].local_header_offset, 0); + assert_eq!(zip::read_entry(&data, &archive.entries[0], 10).unwrap(), b"x"); + assert_split(&zip64_entry_disk(1, true)); +} + +fn zip64_archive(record_disk: u32, central_disk: u32, locator_disk: u32, disks: u32, entries_on_disk: u64) -> Vec { + let mut data = zip_stored(&[("a", b"x")]); + let end = data.len() - 22; + let central = u32::from_le_bytes(data[end + 16..end + 20].try_into().unwrap()); + let size = u32::from_le_bytes(data[end + 12..end + 16].try_into().unwrap()); + let mut record = b"PK\x06\x06".to_vec(); + record.extend(44u64.to_le_bytes()); + record.extend([45, 0, 45, 0]); + record.extend(record_disk.to_le_bytes()); + record.extend(central_disk.to_le_bytes()); + record.extend(entries_on_disk.to_le_bytes()); + record.extend(1u64.to_le_bytes()); + record.extend(u64::from(size).to_le_bytes()); + record.extend(u64::from(central).to_le_bytes()); + record.extend(b"PK\x06\x07"); + record.extend(locator_disk.to_le_bytes()); + record.extend((end as u64).to_le_bytes()); + record.extend(disks.to_le_bytes()); + data[end + 8..end + 12].fill(0xff); + data.splice(end..end, record); + data +} + +#[test] +fn zip64_end_records_and_locators_must_also_describe_one_volume() { + for fields in [(1, 0, 0, 1, 1), (0, 1, 0, 1, 1), (0, 0, 1, 1, 1), (0, 0, 0, 2, 1), (0, 0, 0, 1, 0)] { + assert_split(&zip64_archive(fields.0, fields.1, fields.2, fields.3, fields.4)); + } + assert_eq!(zip::parse(&zip64_archive(0, 0, 0, 1, 1), 10).unwrap().entries.len(), 1); +} + +#[test] +fn zip64_does_not_erase_explicit_classic_volume_markers() { + for field in [4, 6] { + let mut data = zip64_archive(0, 0, 0, 1, 1); + let end = data.len() - 22; + data[end + field..end + field + 2].copy_from_slice(&1u16.to_le_bytes()); + assert_split(&data); + data[end + field..end + field + 2].fill(0xff); + assert_eq!(zip::parse(&data, 10).unwrap().entries.len(), 1); + } +} + +#[test] +fn zip64_does_not_erase_explicit_classic_entry_counts() { + for field in [8, 10] { + for count in [0u16, 2] { + let mut data = zip64_archive(0, 0, 0, 1, 1); + let end = data.len() - 22; + data[end + field..end + field + 2].copy_from_slice(&count.to_le_bytes()); + assert!(matches!(zip::parse(&data, 10), Err(ZipError::Unsupported(_))), "field {field}, count {count}"); + } + } +} + +#[test] +fn mixed_classic_and_zip64_entry_counts_can_describe_one_volume() { + for field in [8, 10] { + let mut data = zip64_archive(0, 0, 0, 1, 1); + let end = data.len() - 22; + data[end + field..end + field + 2].copy_from_slice(&1u16.to_le_bytes()); + let archive = zip::parse(&data, 10).unwrap(); + assert_eq!(archive.entries.len(), 1, "field {field}"); + assert_eq!(zip::read_entry(&data, &archive.entries[0], 10).unwrap(), b"x"); + } +} + +#[test] +fn zip64_cannot_replace_explicit_classic_directory_bounds() { + for field in [12, 16] { + let mut data = zip64_archive(0, 0, 0, 1, 1); + let end = data.len() - 22; + let value = u32::from_le_bytes(data[end + field..end + field + 4].try_into().unwrap()); + data[end + field..end + field + 4].copy_from_slice(&(value + 1).to_le_bytes()); + assert!(matches!(zip::parse(&data, 10), Err(ZipError::Corrupt(_))), "field {field}"); + } +} + +#[test] +fn each_zip64_field_can_resolve_its_own_classic_sentinel() { + for (field, width) in [(4, 2), (6, 2), (8, 2), (10, 2), (12, 4), (16, 4)] { + let mut data = zip64_archive(0, 0, 0, 1, 1); + let end = data.len() - 22; + data[end + 8..end + 10].copy_from_slice(&1u16.to_le_bytes()); + data[end + 10..end + 12].copy_from_slice(&1u16.to_le_bytes()); + data[end + field..end + field + width].fill(0xff); + let archive = zip::parse(&data, 10).unwrap(); + assert!(archive.zip64, "field {field}"); + assert_eq!(archive.declared_entries, 1, "field {field}"); + assert_eq!(archive.entries.len(), 1, "field {field}"); + assert_eq!(archive.prefix_bytes, 0, "field {field}"); + assert_eq!(zip::read_entry(&data, &archive.entries[0], 10).unwrap(), b"x", "field {field}"); + } +} diff --git a/crates/unzip/tests/zip_stored_cap.rs b/crates/unzip/tests/zip_stored_cap.rs new file mode 100644 index 000000000..61bb06d50 --- /dev/null +++ b/crates/unzip/tests/zip_stored_cap.rs @@ -0,0 +1,21 @@ +// From Craft Anything crates/formats/tests/zip_stored_cap.rs. + +mod common; +use craft_unzip::{self as zip, ZipError}; + +#[test] +fn a_stored_entry_cannot_understate_its_size_to_bypass_the_output_limit() { + let data = common::zip_stored(&[("payload", &[0x5a; 64])]); + let mut entry = zip::parse(&data, 10).unwrap().entries.remove(0); + entry.uncompressed_size = 1; + assert!(matches!(zip::read_entry(&data, &entry, 8), Err(ZipError::TooLarge(8)))); + assert!(matches!(zip::read_entry_unverified(&data, &entry, 8), Err(ZipError::TooLarge(8)))); +} + +#[test] +fn stored_entries_at_the_limit_and_empty_entries_still_read() { + let data = common::zip_stored(&[("payload", b"hello"), ("empty", b"")]); + let entries = zip::parse(&data, 10).unwrap().entries; + assert_eq!(zip::read_entry(&data, &entries[0], 5).unwrap(), b"hello"); + assert_eq!(zip::read_entry(&data, &entries[1], 0).unwrap(), b""); +}