Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# zipview

Read-only ZIP container reader for Rust. Opens an in-memory archive, lists its
entries, inflates the ones you ask for. That is all it does.
Read-only ZIP container reader for Rust. Opens an archive (in memory, or read a
byte range at a time), lists its entries, inflates the ones you ask for. That is
all it does.

It **cannot** write an archive, extract to disk, decrypt, or touch the
filesystem. That is the point.
Expand All @@ -16,6 +17,42 @@ for entry in archive.entries() {
let xml = archive.read_to_string("docProps/core.xml")?;
```

## Archives that aren't in memory

`ZipReader` needs the whole archive as one slice. For one too big to load, or
read over a network, `ZipIndex` reads it a byte range at a time: the end of the
archive and its central directory to open it (one read when the directory is
small, two for most others), then only the bytes of the entries you read.

```rust
use std::os::unix::fs::FileExt;

let file = std::fs::File::open("huge.zip")?;
let read = |offset: u64, buf: &mut [u8]| file.read_exact_at(buf, offset);
let index = zipview::ZipIndex::open(file.metadata()?.len(), read)?;

for entry in index.entries() {
println!("{} — {} bytes", entry.name(), entry.size());
}
if let Some(entry) = index.by_name("release.nfo") {
let nfo = index.fetch_entry(entry, None, read)?; // two reads: header, entry
let check = index.fetch_password_check(entry, read)?; // two small reads
}
```

`read(offset, buf)` can be anything that fills `buf` from the archive: a file
read by offset, HTTP range requests, pieces fetched from elsewhere. Where reads
are asynchronous, `ZipIndexOpener` does the opening one read at a time —
`next_read` names the bytes it needs, you `supply` them however you fetch, and
`finish` gives the index — and `header_range`, `entry_range` and
`password_check_range` do the same for an entry, with `read_entry` and
`password_check` finishing from the bytes.

In the tests a 50 GiB archive opens with a single 65 KB read. No read is over
16 MiB, so a directory size that lies can't make you allocate more than that at
once, and an entry over the 256 MiB inflate limit is refused before its bytes
are read.

## Why

Written to replace the `zip` crate in a metadata tool that needed about five
Expand Down Expand Up @@ -93,6 +130,12 @@ so a round-trip cannot pass by sharing a wrong constant.
Beyond the usual cases, the suite sweeps **every truncation** of a valid archive
and **flips every single byte**, asserting only that nothing panics.

`ZipIndex` follows `ZipReader`'s steps over byte ranges instead of a slice, so
the suite holds the two to the same result on every fixture (plain, ZIP64,
stub-prefixed, ZipCrypto and AES), every truncation and every single-byte
corruption: the same entries, comment and errors, the same bytes or error from
every entry, the same password checks.

```bash
cargo test
cargo clippy --all-targets -- -D warnings
Expand All @@ -109,7 +152,8 @@ cargo +nightly fuzz run fuzz_zip fuzz/corpus/fuzz_zip fuzz/seeds

The target both lists *and reads* every entry, because a reader that parsed
headers safely but trusted the payload offset would pass an enumeration-only
target.
target. It also opens every input with `ZipIndex` and asserts it agrees with
`ZipReader` on everything.

The corpus directory comes **first** and the seeds second: libFuzzer writes
new inputs to the first directory it is given and treats the rest as read-only.
Expand Down
11 changes: 11 additions & 0 deletions examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ fn main() {
case("open-20000", rounds, 3, || {
zipview::ZipReader::new(&arc_dir).unwrap().len()
});
// The same directory through byte ranges, served from memory.
let read = |offset: u64, buf: &mut [u8]| -> Result<(), zipview::ZipReadError> {
let start = offset as usize;
buf.copy_from_slice(&arc_dir[start..start + buf.len()]);
Ok(())
};
case("index-open-20000", rounds, 3, || {
zipview::ZipIndex::open(arc_dir.len() as u64, read)
.unwrap()
.len()
});
let zip_dir = zipview::ZipReader::new(&arc_dir).unwrap();
case("by_name-100", rounds, 3, || {
(0..100)
Expand Down
41 changes: 36 additions & 5 deletions fuzz/fuzz_targets/fuzz_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,37 @@
//! follows an offset to a local header and inflates. A reader that parsed
//! headers safely but trusted the payload offset would pass an
//! enumeration-only fuzzer.
//!
//! The same bytes also go through [`ZipIndex`], the reader for archives that
//! aren't in memory, which must agree with [`ZipReader`] on everything: the
//! entries, every entry's bytes or error, every password check. A read it
//! asks for past the end of the input panics.

use libfuzzer_sys::fuzz_target;
use zipview::ZipReader;
use zipview::{ZipIndex, ZipReadError, ZipReader};

fuzz_target!(|data: &[u8]| {
let Ok(reader) = ZipReader::new(data) else {
// Rejecting malformed input is correct behaviour, not a finding.
return;
let read = |offset: u64, buf: &mut [u8]| -> Result<(), ZipReadError> {
let start = offset as usize;
buf.copy_from_slice(&data[start..start + buf.len()]);
Ok(())
};
let index = ZipIndex::open(data.len() as u64, read);
let reader = match ZipReader::new(data) {
Ok(reader) => reader,
Err(e) => {
// Rejecting malformed input is correct behaviour, not a finding,
// as long as both readers reject it the same way.
assert_eq!(index.err(), Some(e));
return;
}
};
let index = index.expect("the ranged reader opens what the in-memory one does");
assert_eq!(
format!("{:?}", reader.entries()),
format!("{:?}", index.entries())
);
assert_eq!(reader.comment(), index.comment());

// Archive-level accessors must tolerate whatever parsed.
let _ = reader.comment();
Expand Down Expand Up @@ -60,7 +82,16 @@ fuzz_target!(|data: &[u8]| {

// The payload path: resolves an attacker-controlled offset and
// inflates. Errors are expected and ignored.
let _ = reader.read_entry(entry);
let bytes = reader.read_entry(entry);
assert_eq!(bytes, index.fetch_entry(entry, None, read));
assert_eq!(
reader.read_entry_with_password(entry, Some("password")),
index.fetch_entry(entry, Some("password"), read)
);
assert_eq!(
reader.password_check(entry),
index.fetch_password_check(entry, read).expect("reads succeed")
);

// Name-based lookup must agree that an enumerated entry exists, and
// reading by that name must not panic either.
Expand Down
Loading
Loading