From 30e5deb660ee195fe713f2fee973f5764b956339 Mon Sep 17 00:00:00 2001 From: Call4han Date: Mon, 16 Nov 2026 21:19:24 -0500 Subject: [PATCH 1/2] fix(xfl): read ZIPs with overstated central directory sizes --- crates/xfl/src/lib.rs | 13 +++++++++++++ crates/xfl/src/tests.rs | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/crates/xfl/src/lib.rs b/crates/xfl/src/lib.rs index fbcb3ecc2..7283b56a0 100644 --- a/crates/xfl/src/lib.rs +++ b/crates/xfl/src/lib.rs @@ -318,6 +318,19 @@ fn find_end(data: &[u8]) -> Result<(u64, u64, u64, usize), ZipError> { let Some((_, shift)) = locate_directory(data, count.into(), size.into(), offset.into(), candidate) .filter(|&(dir, _)| directory_is_exact(data, dir, size.into(), count.into(), &mut directory_jumps)) else { + // An overstated size is safe to ignore only when the recorded entries fill + // the byte range from the declared offset to this single-disk end record. + // The classic entry count is below MAX_ENTRIES, so the walk checks every entry. + if count != 0 + && u32_at(data, candidate + 4) == Some(0) + && u16_at(data, candidate + 8) == Some(count) + && let Ok(start) = usize::try_from(offset) + && let Some(measured) = candidate.checked_sub(start).and_then(|n| u32::try_from(n).ok()) + && measured < size + && directory_is_exact(data, start, measured.into(), count.into(), &mut directory_jumps) + { + return Ok((count.into(), measured.into(), offset.into(), candidate)); + } continue; }; if count == 0 && shift > 0 { diff --git a/crates/xfl/src/tests.rs b/crates/xfl/src/tests.rs index 5ae0713ba..5611a92ca 100644 --- a/crates/xfl/src/tests.rs +++ b/crates/xfl/src/tests.rs @@ -108,6 +108,23 @@ fn damaged_archives_are_errors() { assert!(Archive::open(&z).is_err()); } +#[test] +fn reads_a_directory_with_an_overstated_size() { + let mut z = zip(&[("DOMDocument.xml", b"", true), ("LIBRARY/ball.xml", b"", false)]); + let end = z.len() - 22; + let size = u32_at(&z, end + 12).unwrap(); + z[end + 12..end + 16].copy_from_slice(&(size + 13).to_le_bytes()); + + let archive = Archive::open(&z).unwrap(); + assert_eq!(archive.counts().0, 2); + assert_eq!(archive.read("DOMDocument.xml").unwrap().unwrap(), b""); + assert_eq!(archive.read("LIBRARY/ball.xml").unwrap().unwrap(), b""); + + // Keep the overstated end record but remove the last byte of the final entry's name. + z.remove(end - 1); + assert_eq!(Archive::open(&z).err(), Some(ZipError::Damaged("the central directory is outside the archive"))); +} + #[test] fn a_truncated_central_directory_is_damaged() { let z = zip(&[("DOMDocument.xml", b"", true), ("b.xml", b"", true), ("c.xml", b"", false)]); From 841e6a726ba4aba563cc0ed9c1465c8345502666 Mon Sep 17 00:00:00 2001 From: Call4han Date: Mon, 16 Nov 2026 21:35:57 -0500 Subject: [PATCH 2/2] fix(xfl): prefer valid ZIP end records over recovered ones --- crates/xfl/src/lib.rs | 13 +++++++++++-- crates/xfl/src/tests.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/xfl/src/lib.rs b/crates/xfl/src/lib.rs index 7283b56a0..f89ec10fe 100644 --- a/crates/xfl/src/lib.rs +++ b/crates/xfl/src/lib.rs @@ -279,6 +279,7 @@ fn find_end(data: &[u8]) -> Result<(u64, u64, u64, usize), ZipError> { // it from an empty EOCD-shaped record inside the preceding archive's comment. Keep it as a // last resort while searching for an older, structurally stronger candidate. let mut empty_sfx = None; + let mut measured_directory = None; let mut fallback = None; // Fake EOCD records in a comment can all describe the same large directory. Cache binary // directory jumps so checking each candidate does not rescan every entry. @@ -329,7 +330,9 @@ fn find_end(data: &[u8]) -> Result<(u64, u64, u64, usize), ZipError> { && measured < size && directory_is_exact(data, start, measured.into(), count.into(), &mut directory_jumps) { - return Ok((count.into(), measured.into(), offset.into(), candidate)); + // Keep scanning for a valid declared size. Among measured candidates, + // prefer the earliest record so a copy in its comment cannot hide it. + measured_directory = Some((count.into(), measured.into(), offset.into(), candidate)); } continue; }; @@ -341,7 +344,13 @@ fn find_end(data: &[u8]) -> Result<(u64, u64, u64, usize), ZipError> { eocd = Some(candidate); break; } - let eocd = eocd.or(empty_sfx).or(fallback).ok_or(ZipError::NotZip)?; + let eocd = eocd.or(empty_sfx); + if eocd.is_none() + && let Some(directory) = measured_directory + { + return Ok(directory); + } + let eocd = eocd.or(fallback).ok_or(ZipError::NotZip)?; let field16 = |o: usize| u16_at(data, eocd + o).ok_or(ZipError::Damaged("the end record is cut short")); let field32 = |o: usize| u32_at(data, eocd + o).ok_or(ZipError::Damaged("the end record is cut short")); let (disk, dir_disk, on_disk, count) = (field16(4)?, field16(6)?, field16(8)?, field16(10)?); diff --git a/crates/xfl/src/tests.rs b/crates/xfl/src/tests.rs index 5611a92ca..6075e4e57 100644 --- a/crates/xfl/src/tests.rs +++ b/crates/xfl/src/tests.rs @@ -323,6 +323,32 @@ fn nonempty_comment_eocd_candidate_does_not_mask_valid_archive() { assert_eq!(archive.read("a.xml").unwrap().unwrap(), b"x"); } +#[test] +fn overstated_comment_eocd_does_not_mask_valid_archive() { + let mut bytes = zip(&[("a.xml", b"x", false)]); + let end = bytes.len() - 22; + let size = u32_at(&bytes, end + 12).unwrap(); + let start = u32_at(&bytes, end + 16).unwrap() as usize; + let fake_start = bytes.len(); + + // Copy the directory and end record into the real comment, then corrupt the copies. + bytes.extend_from_within(start..); + let comment_len = u16::try_from(bytes.len() - fake_start).unwrap(); + bytes[end + 20..end + 22].copy_from_slice(&comment_len.to_le_bytes()); + bytes[fake_start + 42..fake_start + 46].copy_from_slice(&1u32.to_le_bytes()); + let fake_end = bytes.len() - 22; + bytes[fake_end + 12..fake_end + 16].copy_from_slice(&(size + 1).to_le_bytes()); + bytes[fake_end + 16..fake_end + 20].copy_from_slice(&u32::try_from(fake_start).unwrap().to_le_bytes()); + + let archive = Archive::open(&bytes).unwrap(); + assert_eq!(archive.read("a.xml").unwrap().unwrap(), b"x"); + + // If both candidates need recovery, the record before the comment still wins. + bytes[end + 12..end + 16].copy_from_slice(&(size + 1).to_le_bytes()); + let archive = Archive::open(&bytes).unwrap(); + assert_eq!(archive.read("a.xml").unwrap().unwrap(), b"x"); +} + #[test] fn repeated_eocd_directory_candidates_reuse_entry_validation() { let entries = (0..1024).map(|index| Raw::new(&format!("entry-{index:04}.xml"), b"", false)).collect::>();