From 80dddf66e61870004f9a37bdc3a0eefa26d4b539 Mon Sep 17 00:00:00 2001 From: compiler-segfault <238626475+compiler-segfault@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:10:10 +0800 Subject: [PATCH 1/5] fix: keep extended timestamp and NTFS extra fields in raw copies `ExtraField::size` and `ExtraField::write` had no arm for the extended timestamp (0x5455) and the NTFS file times (0x000a), so `raw_copy_options` serialized them to nothing and `raw_copy_file` silently dropped them. A copy then only keeps the DOS date-time from the local header, which is local time with two-second resolution. Write both fields again: the NTFS attribute is 32 bytes, and the extended timestamp advertises only the timestamps it stores, since the central header keeps the modification time alone. --- src/extra_fields/extended_timestamp.rs | 116 ++++++++++++++++++++++++- src/extra_fields/extra_field.rs | 16 ++-- src/extra_fields/ntfs.rs | 30 ++++++- tests/raw_copy_extra_fields.rs | 97 +++++++++++++++++++++ 4 files changed, 250 insertions(+), 9 deletions(-) create mode 100644 tests/raw_copy_extra_fields.rs diff --git a/src/extra_fields/extended_timestamp.rs b/src/extra_fields/extended_timestamp.rs index 0042d13b9..7e72d3a36 100644 --- a/src/extra_fields/extended_timestamp.rs +++ b/src/extra_fields/extended_timestamp.rs @@ -1,11 +1,12 @@ //! Extended Timestamp extra field //! Defined in +use crate::extra_fields::UsedExtraField; use crate::result::invalid; use crate::result::{ZipError, ZipResult}; use crate::unstable::LittleEndianReadExt; use core::mem; -use std::io::Read; +use std::io::{Read, Write}; /// `ExtendedTimestamp` Flags #[rustfmt::skip] @@ -208,6 +209,65 @@ impl ExtendedTimestamp { pub fn cr_time(&self) -> Option { self.created } + + /// Number of timestamps the serialized field contains. + /// + /// The central header only stores the modification time, so the access and + /// creation times are left out there. + fn time_count(&self, is_local_header: bool) -> usize { + let mut count = usize::from(self.modified.is_some()); + if is_local_header { + count += usize::from(self.accessed.is_some()); + count += usize::from(self.created.is_some()); + } + count + } + + /// Size of the serialized extra field, including its 4-byte extra field + /// header (tag and data size). + pub(crate) fn full_size(&self, is_local_header: bool) -> usize { + mem::size_of::() * 2 + mem::size_of::() + 4 * self.time_count(is_local_header) + } + + /// Serializes the extra field, including its tag and data size. + /// + /// This keeps the timestamps of a copied entry, whose local header would + /// otherwise only carry the rounded, local-time DOS date-time. + pub(crate) fn write(&self, write: &mut W, is_local_header: bool) -> ZipResult<()> { + // Only the stored timestamps are advertised: the central header keeps + // the modification time only, and flags that don't match the data would + // make the field unreadable. + let mut flags = 0u8; + if self.modified.is_some() { + flags |= u8::from(ExtendedTimestampFlags::Modified); + } + if is_local_header { + if self.accessed.is_some() { + flags |= u8::from(ExtendedTimestampFlags::Accessed); + } + if self.created.is_some() { + flags |= u8::from(ExtendedTimestampFlags::Created); + } + } + + write.write_all(&UsedExtraField::ExtendedTimestamp.to_le_bytes())?; + let data_size = (self.full_size(is_local_header) - mem::size_of::() * 2) as u16; + write.write_all(&data_size.to_le_bytes())?; + write.write_all(&[flags])?; + + if let Some(modified) = self.modified { + write.write_all(&modified.to_le_bytes())?; + } + if is_local_header { + if let Some(accessed) = self.accessed { + write.write_all(&accessed.to_le_bytes())?; + } + if let Some(created) = self.created { + write.write_all(&created.to_le_bytes())?; + } + } + Ok(()) + } } #[cfg(test)] @@ -383,4 +443,58 @@ mod tests { assert_eq!(result.mod_time(), Some(1700000000)); assert_eq!(cursor.position(), 9); } + + /// A copied entry must be able to serialize the field again, with the same + /// timestamps and flags that the source advertised. + #[test] + fn test_write_local_header() { + let field = [ + 0b0000_0111_u8, + 0x01, + 0xF1, + 0x53, + 0x65, + 0x02, + 0xF1, + 0x53, + 0x65, + 0x03, + 0xF1, + 0x53, + 0x65, + ]; + let mut cursor = Cursor::new(&field); + let timestamp = ExtendedTimestamp::try_from_reader(&mut cursor, 13, true).unwrap(); + let mut serialized = Vec::new(); + timestamp.write(&mut serialized, true).unwrap(); + assert_eq!(serialized.len(), 4 + field.len()); + assert_eq!(&serialized[4..], &field[..]); + } + + /// The central header stores the modification time only, so its flags must + /// not advertise the access and creation times. + #[test] + fn test_write_central_header() { + let field = [ + 0b0000_0111_u8, + 0x01, + 0xF1, + 0x53, + 0x65, + 0x02, + 0xF1, + 0x53, + 0x65, + 0x03, + 0xF1, + 0x53, + 0x65, + ]; + let mut cursor = Cursor::new(&field); + let timestamp = ExtendedTimestamp::try_from_reader(&mut cursor, 13, true).unwrap(); + let mut serialized = Vec::new(); + timestamp.write(&mut serialized, false).unwrap(); + assert_eq!(serialized.len(), 4 + 5); + assert_eq!(&serialized[4..], &[0b0000_0001, 0x01, 0xF1, 0x53, 0x65]); + } } diff --git a/src/extra_fields/extra_field.rs b/src/extra_fields/extra_field.rs index e699eb56a..a36a2e540 100644 --- a/src/extra_fields/extra_field.rs +++ b/src/extra_fields/extra_field.rs @@ -201,15 +201,14 @@ impl ExtraField { ExtraField::Zip64ExtendedInformation(zip64_extra) => { zip64_extra.full_size(is_local_header) } - ExtraField::Ntfs(_ntfs) => { + ExtraField::Ntfs(ntfs) => { // NTFS extra field - 0 + ntfs.full_size() } #[cfg(feature = "aes-crypto")] ExtraField::AeXEncryption(aes) => aes.full_size(), - ExtraField::ExtendedTimestamp(_extended_timestamp) => { - // nothing to do - 0 + ExtraField::ExtendedTimestamp(extended_timestamp) => { + extended_timestamp.full_size(is_local_header) } ExtraField::UnicodeComment(unicode_comment) => unicode_comment.full_size(), ExtraField::UnicodePath(unicode_path) => unicode_path.full_size(), @@ -246,8 +245,11 @@ impl ExtraField { ExtraField::DataStreamAlignment(data_stream_alignment) => { data_stream_alignment.write(writer, is_local_header)? } - _ => { - // nothing to do + ExtraField::Ntfs(ntfs) => { + ntfs.write(writer)?; + } + ExtraField::ExtendedTimestamp(extended_timestamp) => { + extended_timestamp.write(writer, is_local_header)?; } } Ok(()) diff --git a/src/extra_fields/ntfs.rs b/src/extra_fields/ntfs.rs index 547b78154..42dd94284 100644 --- a/src/extra_fields/ntfs.rs +++ b/src/extra_fields/ntfs.rs @@ -1,9 +1,11 @@ -use std::io::Read; +use std::io::{Read, Write}; +use crate::extra_fields::UsedExtraField; use crate::{ result::{ZipError, ZipResult}, unstable::LittleEndianReadExt, }; +use core::mem; /// The NTFS extra field as described in [PKWARE's APPNOTE.TXT v6.3.9]. /// @@ -105,4 +107,30 @@ impl Ntfs { pub fn created_file_time(&self) -> nt_time::FileTime { nt_time::FileTime::new(self.ctime) } + + /// Size of the serialized extra field, including its 4-byte extra field + /// header (tag and data size). + pub(crate) fn full_size(&self) -> usize { + mem::size_of::() * 2 // extra field tag and data size + + mem::size_of::() // reserved for future use + + mem::size_of::() * 2 // attribute tag and attribute size + + 3 * mem::size_of::() // mtime, atime and ctime + } + + /// Serializes the extra field, including its tag and data size. + /// + /// This keeps the file times of a copied entry, which a copy that only + /// carries the DOS date-time in the local header would round and shift. + pub(crate) fn write(&self, write: &mut W) -> ZipResult<()> { + write.write_all(&UsedExtraField::Ntfs.to_le_bytes())?; + let data_size = (self.full_size() - mem::size_of::() * 2) as u16; + write.write_all(&data_size.to_le_bytes())?; + write.write_all(&0u32.to_le_bytes())?; // reserved for future use + write.write_all(&0x0001u16.to_le_bytes())?; // attribute tag: file times + write.write_all(&24u16.to_le_bytes())?; // attribute size + write.write_all(&self.mtime.to_le_bytes())?; + write.write_all(&self.atime.to_le_bytes())?; + write.write_all(&self.ctime.to_le_bytes())?; + Ok(()) + } } diff --git a/tests/raw_copy_extra_fields.rs b/tests/raw_copy_extra_fields.rs new file mode 100644 index 000000000..e5f4379f7 --- /dev/null +++ b/tests/raw_copy_extra_fields.rs @@ -0,0 +1,97 @@ +//! A raw copy has to keep the extra fields that the reader parsed for the source entry. +//! +//! `ZipWriter::raw_copy_file` passes them through `ExtraField::write`, which used to have no +//! arm for the extended timestamp (0x5455) and the NTFS file times (0x000a): the serialized +//! field came back empty and the copy silently lost it (see #1285). + +use std::io::{Cursor, Write}; + +use zip::extra_fields::ExtraField; +use zip::write::{FullFileOptions, ZipWriter}; +use zip::{CompressionMethod, ZipArchive}; + +const MODIFIED: u32 = 1_715_000_001; +const MTIME: u64 = 133_000_000_000_000_000; +const ATIME: u64 = 133_000_000_000_000_001; +const CTIME: u64 = 133_000_000_000_000_002; + +/// Extended timestamp (0x5455) with only the modification time, as written in +/// the central directory. +fn extended_timestamp() -> Vec { + let mut data = vec![0x01]; + data.extend_from_slice(&MODIFIED.to_le_bytes()); + data +} + +/// NTFS file times (0x000a): reserved field, file-times attribute tag and +/// size, then the three Windows file times. +fn ntfs_times() -> Vec { + let mut data = Vec::new(); + data.extend_from_slice(&0u32.to_le_bytes()); // reserved for future use + data.extend_from_slice(&0x0001u16.to_le_bytes()); // attribute tag: file times + data.extend_from_slice(&24u16.to_le_bytes()); // attribute size + data.extend_from_slice(&MTIME.to_le_bytes()); + data.extend_from_slice(&ATIME.to_le_bytes()); + data.extend_from_slice(&CTIME.to_le_bytes()); + data +} + +fn source_archive() -> Vec { + let mut options = FullFileOptions::default().compression_method(CompressionMethod::Stored); + options + .add_extra_field(0x5455, extended_timestamp(), false) + .unwrap(); + options + .add_extra_field(0x000a, ntfs_times(), false) + .unwrap(); + let mut writer = ZipWriter::new(Cursor::new(Vec::new())); + writer.start_file("a.txt", options).unwrap(); + writer.write_all(b"hello").unwrap(); + writer.finish().unwrap().into_inner() +} + +fn assert_fields(archive: &mut ZipArchive>>, expected: bool) { + let file = archive.by_index_raw(0).unwrap(); + let mut found_timestamp = false; + let mut found_ntfs = false; + for field in file.extra_data_fields() { + match field { + ExtraField::ExtendedTimestamp(timestamp) => { + found_timestamp = true; + assert_eq!(timestamp.mod_time(), Some(MODIFIED)); + } + ExtraField::Ntfs(ntfs) => { + found_ntfs = true; + assert_eq!(ntfs.mtime(), MTIME); + assert_eq!(ntfs.atime(), ATIME); + assert_eq!(ntfs.ctime(), CTIME); + } + _ => {} + } + } + assert_eq!(found_timestamp, expected, "extended timestamp field"); + assert_eq!(found_ntfs, expected, "NTFS file times field"); +} + +#[test] +fn raw_copy_keeps_the_extended_timestamp_and_ntfs_times() { + let source = source_archive(); + + // The source archive has both fields, so the copy has something to keep. + let mut archive = ZipArchive::new(Cursor::new(source.clone())).unwrap(); + assert_fields(&mut archive, true); + + let mut copy = ZipWriter::new(Cursor::new(Vec::new())); + copy.raw_copy_file(archive.by_index_raw(0).unwrap()) + .unwrap(); + let copied = copy.finish().unwrap().into_inner(); + + let mut copy_archive = ZipArchive::new(Cursor::new(copied)).unwrap(); + assert_fields(&mut copy_archive, true); + + // The copied entry is still readable, and its contents are intact. + let mut file = copy_archive.by_name("a.txt").unwrap(); + let mut contents = Vec::new(); + std::io::Read::read_to_end(&mut file, &mut contents).unwrap(); + assert_eq!(contents, b"hello"); +} From 7e48ca66e3c632224d53249f9240501aa1ee133f Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:03:45 -0800 Subject: [PATCH 2/5] Update src/extra_fields/extended_timestamp.rs Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> --- src/extra_fields/extended_timestamp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extra_fields/extended_timestamp.rs b/src/extra_fields/extended_timestamp.rs index 7e72d3a36..bc0c32b82 100644 --- a/src/extra_fields/extended_timestamp.rs +++ b/src/extra_fields/extended_timestamp.rs @@ -226,7 +226,7 @@ impl ExtendedTimestamp { /// Size of the serialized extra field, including its 4-byte extra field /// header (tag and data size). pub(crate) fn full_size(&self, is_local_header: bool) -> usize { - mem::size_of::() * 2 + mem::size_of::() + 4 * self.time_count(is_local_header) + mem::size_of::() * 2 + mem::size_of::() + mem::size_of::() * self.time_count(is_local_header) } /// Serializes the extra field, including its tag and data size. From 07ce8f27e8ced4f0437609fea2b9d894163fab8f Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:07:32 -0800 Subject: [PATCH 3/5] cargo fmt --all --- src/extra_fields/extended_timestamp.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/extra_fields/extended_timestamp.rs b/src/extra_fields/extended_timestamp.rs index bc0c32b82..048fadff2 100644 --- a/src/extra_fields/extended_timestamp.rs +++ b/src/extra_fields/extended_timestamp.rs @@ -226,7 +226,9 @@ impl ExtendedTimestamp { /// Size of the serialized extra field, including its 4-byte extra field /// header (tag and data size). pub(crate) fn full_size(&self, is_local_header: bool) -> usize { - mem::size_of::() * 2 + mem::size_of::() + mem::size_of::() * self.time_count(is_local_header) + mem::size_of::() * 2 + + mem::size_of::() + + mem::size_of::() * self.time_count(is_local_header) } /// Serializes the extra field, including its tag and data size. From 9f00c6556c02e949831b32ce0fb7d352d102ca4e Mon Sep 17 00:00:00 2001 From: n4n5 Date: Sat, 14 Nov 2026 13:54:40 +0100 Subject: [PATCH 4/5] Fix size calculation for serialized extra field Signed-off-by: n4n5 --- src/extra_fields/extended_timestamp.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/extra_fields/extended_timestamp.rs b/src/extra_fields/extended_timestamp.rs index c2cc6c376..8560aee39 100644 --- a/src/extra_fields/extended_timestamp.rs +++ b/src/extra_fields/extended_timestamp.rs @@ -243,8 +243,14 @@ impl ExtendedTimestamp { /// Size of the serialized extra field, including its 4-byte extra field /// header (tag and data size). pub(crate) fn full_size(&self, is_local_header: bool) -> usize { - mem::size_of::() * 2 - + mem::size_of::() + mem::size_of::() // magic + + mem::size_of::() // length + + self.size() + } + + /// Size of the serialized extra field + pub(crate) fn size(&self, is_local_header: bool) -> usize { + mem::size_of::() + mem::size_of::() * self.time_count(is_local_header) } @@ -270,7 +276,7 @@ impl ExtendedTimestamp { } write.write_all(&UsedExtraField::ExtendedTimestamp.to_le_bytes())?; - let data_size = (self.full_size(is_local_header) - mem::size_of::() * 2) as u16; + let data_size = self.size(is_local_header) as u16; write.write_all(&data_size.to_le_bytes())?; write.write_all(&[flags])?; From 5f2cb8a249cbe94738bf8df211cc9ee582f78ade Mon Sep 17 00:00:00 2001 From: n4n5 Date: Sat, 14 Nov 2026 13:56:51 +0100 Subject: [PATCH 5/5] Fix size calculation in extended_timestamp.rs Signed-off-by: n4n5 --- src/extra_fields/extended_timestamp.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/extra_fields/extended_timestamp.rs b/src/extra_fields/extended_timestamp.rs index 8560aee39..1a975cb8c 100644 --- a/src/extra_fields/extended_timestamp.rs +++ b/src/extra_fields/extended_timestamp.rs @@ -245,13 +245,12 @@ impl ExtendedTimestamp { pub(crate) fn full_size(&self, is_local_header: bool) -> usize { mem::size_of::() // magic + mem::size_of::() // length - + self.size() + + self.size(is_local_header) } /// Size of the serialized extra field pub(crate) fn size(&self, is_local_header: bool) -> usize { - mem::size_of::() - + mem::size_of::() * self.time_count(is_local_header) + mem::size_of::() + mem::size_of::() * self.time_count(is_local_header) } /// Serializes the extra field, including its tag and data size.