Skip to content
Next Next commit
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.
  • Loading branch information
compiler-segfault committed Nov 11, 2026
commit 80dddf66e61870004f9a37bdc3a0eefa26d4b539
116 changes: 115 additions & 1 deletion src/extra_fields/extended_timestamp.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Extended Timestamp extra field
//! Defined in <https://libzip.org/specifications/extrafld.txt>

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]
Expand Down Expand Up @@ -208,6 +209,65 @@ impl ExtendedTimestamp {
pub fn cr_time(&self) -> Option<u32> {
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::<u16>() * 2 + mem::size_of::<u8>() + 4 * self.time_count(is_local_header)
Comment thread
Pr0methean marked this conversation as resolved.
Resolved
}

/// 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<W: 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::<u16>() * 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)]
Expand Down Expand Up @@ -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]);
}
}
16 changes: 9 additions & 7 deletions src/extra_fields/extra_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(())
Expand Down
30 changes: 29 additions & 1 deletion src/extra_fields/ntfs.rs
Original file line number Diff line number Diff line change
@@ -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].
///
Expand Down Expand Up @@ -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::<u16>() * 2 // extra field tag and data size
+ mem::size_of::<u32>() // reserved for future use
+ mem::size_of::<u16>() * 2 // attribute tag and attribute size
+ 3 * mem::size_of::<u64>() // 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<W: Write>(&self, write: &mut W) -> ZipResult<()> {
write.write_all(&UsedExtraField::Ntfs.to_le_bytes())?;
let data_size = (self.full_size() - mem::size_of::<u16>() * 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(())
}
}
97 changes: 97 additions & 0 deletions tests/raw_copy_extra_fields.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
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<u8> {
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<u8> {
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<Cursor<Vec<u8>>>, 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");
}