fix(xfl): prefer valid ZIP end records over recovered ones

This commit is contained in:
Call4han committed 2026-11-16 21:35:57 -05:00
1 parent 30e5deb660
commit 841e6a726b
2 files changed
+37 -2

No files matched your search

+11 -2
View File
@@ -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)?);
+26
View File
@@ -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::<Vec<_>>();