From 2af7bb2e809ee81b964d7bb9daeb3bc1a9013415 Mon Sep 17 00:00:00 2001 From: ByGh00st <237057630+ByGh00st@users.noreply.github.com> Date: Sun, 15 Nov 2026 02:36:06 +0300 Subject: [PATCH 1/2] fix(fuzz): treat shrinking central-directory extra fields as non-streamable --- fuzz/write/regressions/README.md | 6 ++ ...extended-timestamp-extra-field-shrinks.bin | Bin 0 -> 162 bytes fuzz/write/src/main.rs | 67 +++++++++++++++++- src/write/options.rs | 6 ++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 fuzz/write/regressions/reopen-extended-timestamp-extra-field-shrinks.bin diff --git a/fuzz/write/regressions/README.md b/fuzz/write/regressions/README.md index dfdf51048..32b5fb9ec 100644 --- a/fuzz/write/regressions/README.md +++ b/fuzz/write/regressions/README.md @@ -57,3 +57,9 @@ Replayed by `fuzz/replay.sh` in the `fuzz_regressions` CI job. Each must finish `execute` only tracked `comment_len_at_reopen` for top-level reopens, so the harness still expected `read_zipfile_from_stream` to work on the junk-padded archive. Harness bug (reproduces on master 9926c64); fixed by looking through copy bases for reopens. +- `reopen-extended-timestamp-extra-field-shrinks.bin`: found 2026-11-14 (run 43566959380, --no-default-features). + An entry was written with a `central_only` Extended Timestamp extra field (0x5455) carrying 4 payload bytes + (`flags` + 3 undocumented bytes), then reopened with `ZipWriter::new_append` and finished. `parse_central_header` + drops trailing bytes when fewer than 4 follow `flags`, so the rewritten central directory shrank by 3 bytes and + `finalize` zeroed `dir_start` and moved the footer to EOF. Fixed by treating entries with central-directory + extra fields that shrink on reopen as non-streamable. diff --git a/fuzz/write/regressions/reopen-extended-timestamp-extra-field-shrinks.bin b/fuzz/write/regressions/reopen-extended-timestamp-extra-field-shrinks.bin new file mode 100644 index 0000000000000000000000000000000000000000..0158054a4c974c76c1c2f2a2233983a488b5ff6a GIT binary patch literal 162 zcmb2oI9#e9;LXaWv0hpEKNNr%40`$u93>^j5Wrl*%CL9uUX{JQAfRilZ>`S&1XqAe z#_IhH$r!pBj)w#@>i;iI1{uZ3$au&QNL=G(R9~CTXv1rjRW8MN2neFol-1PL6xFps boI?gcBUf@U=u|NL;nQdR|DRP)|Hpp-At6HZ literal 0 HcmV?d00001 diff --git a/fuzz/write/src/main.rs b/fuzz/write/src/main.rs index b12153dc3..7a7719d27 100644 --- a/fuzz/write/src/main.rs +++ b/fuzz/write/src/main.rs @@ -162,7 +162,7 @@ impl FileOperation<'_, '_> { BasicFileOperation::WriteNormalFile { options, .. } | BasicFileOperation::WriteDirectory(options) | BasicFileOperation::WriteSymlinkWithTarget { options, .. } => { - if options.has_encryption() { + if options.has_encryption() || central_extra_fields_shrink_on_reopen(options) { return false; } } @@ -196,6 +196,34 @@ impl FileOperation<'_, '_> { } } +/// Whether `options` carries a custom extra field whose central-directory representation shrinks +/// when parsed back by `ZipArchive` (on `ZipWriter::new_append`) and written out again. A shorter +/// central directory makes `finalize` zero the old header at `dir_start` and move the footer to +/// EOF, which breaks sequential `read_zipfile_from_stream`. +fn central_extra_fields_shrink_on_reopen(options: &FullFileOptions<'_, '_>) -> bool { + use zip::extra_fields::ExtraFieldId; + options + .extra_fields() + .iter() + .any(|field| match field.header_id { + // `ExtraFields::central_extra_fields` omits parsed alignment fields, and + // `replace_zip64_extra_field` replaces parsed ZIP64 fields. + id if id == ExtraFieldId::DataStreamAlignment.as_u16() + || id == ExtraFieldId::AndroidZipAlignment.as_u16() + || id == ExtraFieldId::Zip64ExtendedInfo.as_u16() => + { + true + } + // `ExtendedTimestamp::parse_central_header` keeps only the 4-byte `mtime` (when at + // least 4 bytes follow `flags`) and re-serializes as 1 byte (`[0]`) or 5 bytes + // (`[1, mtime..]`), dropping any other bytes. + id if id == ExtraFieldId::ExtendedTimestamp.as_u16() => { + field.data.len() != 1 && (field.data.len() != 5 || field.data[0] & 1 == 0) + } + _ => false, + }) +} + #[derive(Arbitrary, Clone, Debug)] pub struct FuzzTestCase<'k, 'n> { initial_junk: Box<[u8]>, @@ -1068,4 +1096,41 @@ mod tests { let archive = writer.finish_into_readable().unwrap(); assert_eq!(archive.len(), 0); } + + #[test] + fn shrinking_central_extra_fields_mark_operation_non_streamable() { + for (header_id, data, expected_streamable) in [ + (0x5455, &[0u8][..], true), + (0x5455, &[1, 10, 0, 0, 0][..], true), + (0x5455, &[47, 117, 255, 255][..], false), + (0x5455, &[43, 117][..], false), + (0x5455, &[2, 1, 2, 3, 4][..], false), + (0x5455, &[3, 10, 0, 0, 0, 20, 0, 0, 0][..], false), + (0xa11e, &[4, 0, 0, 0][..], false), + (0xd935, &[4, 0, 0, 0][..], false), + ] { + let mut options = FullFileOptions::default().validate_extra_fields(true); + options.add_extra_field(header_id, data, true).unwrap(); + let mut op = file_operation( + BasicFileOperation::WriteNormalFile { + contents: vec![b"hello".to_vec().into_boxed_slice()].into_boxed_slice(), + options, + }, + "a.txt", + ); + op.reopen = ReopenOption::ViaFinishIntoReadable; + assert_eq!( + op.is_streamable(), + expected_streamable, + "header_id={header_id:#06x} data={data:?}" + ); + + let case = FuzzTestCase { + initial_junk: Box::new([]), + operations: vec![(op, false)].into_boxed_slice(), + flush_on_finish_file: false, + }; + case.execute(&mut String::new()).unwrap(); + } + } } diff --git a/src/write/options.rs b/src/write/options.rs index da02392d9..13c5853ad 100644 --- a/src/write/options.rs +++ b/src/write/options.rs @@ -666,6 +666,12 @@ impl FileOptions<'_, '_, ExtendedFileOptions> { self } + /// Returns the custom extra fields added to this entry's options. + #[must_use] + pub fn extra_fields(&self) -> &[CustomExtraField] { + &self.extended_options.extra_fields + } + /// Adds an Extended Timestamp extra field (0x5455) to this entry's options. /// /// The timestamps are specified as UNIX epoch seconds. If `modified` is provided, From 6b5096d49426391ffdff6e85b908f34247cdc86a Mon Sep 17 00:00:00 2001 From: ByGh00st <237057630+ByGh00st@users.noreply.github.com> Date: Sun, 15 Nov 2026 02:48:35 +0300 Subject: [PATCH 2/2] test(fuzz): use Stored compression in extra-field shrink unit test --- fuzz/write/src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fuzz/write/src/main.rs b/fuzz/write/src/main.rs index 7a7719d27..eae9775fd 100644 --- a/fuzz/write/src/main.rs +++ b/fuzz/write/src/main.rs @@ -1109,7 +1109,9 @@ mod tests { (0xa11e, &[4, 0, 0, 0][..], false), (0xd935, &[4, 0, 0, 0][..], false), ] { - let mut options = FullFileOptions::default().validate_extra_fields(true); + let mut options = FullFileOptions::default() + .compression_method(zip::CompressionMethod::Stored) + .validate_extra_fields(true); options.add_extra_field(header_id, data, true).unwrap(); let mut op = file_operation( BasicFileOperation::WriteNormalFile {