Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 20 additions & 17 deletions src/read/zip_iterable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,26 @@ impl<R: Read + Seek> ZipIterable<R> {
Err(e) => return Err(last_err.unwrap_or(e)),
};

match CentralDirectoryInfo::try_from(&cde) {
let candidate = CentralDirectoryInfo::try_from(&cde).and_then(|info| {
if info.disk_number != info.disk_with_central_directory {
return Err(ZipError::UnsupportedArchive(
"Support for multi-disk files is not implemented",

@Pr0methean Pr0methean Nov 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep this check separate. Otherwise ZIP cloaking is possible if one of the EOCDs declares the archive to be multi-disk and the other doesn't.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed in ea7231b3. Moved the multi-disk check back outside the fallback loop and added a dual-EOCD regression test (multidisk_zip64_eocd_does_not_fall_back_to_earlier_eocd) verifying a trailing multi-disk ZIP64 EOCD hard-fails with UnsupportedArchive instead of falling back to an earlier single-disk EOCD.

Side note: ZipArchive::get_metadata currently hits the same check inside read_central_header within its EOCD fallback loop (src/read/zip_archive.rs:339), so a non-abutting multi-disk ZIP64 EOCD can still fall back there too. Happy to tighten that in a separate PR if you want.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: ZipArchive::get_metadata currently hits the same check inside read_central_header within its EOCD fallback loop (src/read/zip_archive.rs:339), so a non-abutting multi-disk ZIP64 EOCD can still fall back there too. Happy to tighten that in a separate PR if you want.

Yes please.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #1350 with the fix and a dual-EOCD regression test for ZipArchive.

));
}

// A central directory header is at least 46 bytes, so this bounds the declared
// entry count by the space the directory actually occupies.
let max_entries = usize::try_from(info.directory_size / 46).unwrap_or(usize::MAX);
if info.number_of_files > max_entries {
return Err(invalid!(
"Central directory declares more entries than its size allows"
));
}

Ok(info)
});

match candidate {
Ok(info) => break info,
Err(e) => {
// Other readers use a record whose CD ends at it, so don't fall back past it.
Expand All @@ -85,22 +104,6 @@ impl<R: Read + Seek> ZipIterable<R> {
}
};

if central_directory.disk_number != central_directory.disk_with_central_directory {
return Err(ZipError::UnsupportedArchive(
"Support for multi-disk files is not implemented",
));
}

// A central directory header is at least 46 bytes, so this bounds the declared entry
// count by the space the directory actually occupies.
let max_entries =
usize::try_from(central_directory.directory_size / 46).unwrap_or(usize::MAX);
if central_directory.number_of_files > max_entries {
return Err(invalid!(
"Central directory declares more entries than its size allows"
));
}

Ok(Self {
files: ZipIterableFiles::new(reader, central_directory),
})
Expand Down
37 changes: 37 additions & 0 deletions tests/iterable_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,40 @@ fn enforce_header_consistency_still_yields_duplicate_names() {
assert_eq!(read_entry(second, files), b"second");
assert!(files.next().is_none());
}

#[test]
fn fake_eocd_in_comment_falls_back_to_real_eocd() {
use std::io::Write;
use zip::CompressionMethod;
use zip::write::SimpleFileOptions;

// 22-byte fake EOCD inside the archive comment declaring 65,535 entries in a size too small
// to hold them, with a 0-byte comment so it aligns with EOF.
let fake_eocd: [u8; 22] = [
0x50, 0x4b, 0x05, 0x06, // EOCD magic
0x00, 0x00, // disk number
0x00, 0x00, // disk with CD
0xff, 0xff, // entries on this disk (65535)
0xff, 0xff, // total entries (65535)
0x00, 0x05, 0x06, 0x00, // CD size (394,496 bytes < 65535 * 46)
0x06, 0x00, 0x00, 0x00, // CD offset
0x00, 0x00, // comment length (0)
];

let mut writer = zip::ZipWriter::new(Cursor::new(Vec::new()));
writer
.set_raw_comment(fake_eocd.to_vec().into_boxed_slice())
.unwrap();
let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
writer.start_file("a.txt", stored).unwrap();
writer.write_all(b"alpha").unwrap();
let bytes = writer.finish().unwrap().into_inner();

let mut archive = ZipIterable::try_new(Cursor::new(bytes), Config::default()).unwrap();
assert_eq!(archive.len(), 1);
let files = archive.files().unwrap();
let entry = files.next().unwrap().unwrap();
assert_eq!(entry.name().unwrap(), "a.txt");
assert_eq!(read_entry(entry, files), b"alpha");
assert!(files.next().is_none());
}
Loading