mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 16:24:38 +00:00
Merge pull request #1986 from Call4han/fix/xfl-overstated-directory-size
fix(xfl): read ZIPs with overstated central directory sizes
This commit is contained in:
2 files changed
+66
-1
No files matched your search
+23
-1
@@ -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.
|
||||
@@ -318,6 +319,21 @@ 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)
|
||||
{
|
||||
// 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;
|
||||
};
|
||||
if count == 0 && shift > 0 {
|
||||
@@ -328,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)?);
|
||||
|
||||
@@ -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"<DOMDocument/>", true), ("LIBRARY/ball.xml", b"<DOMSymbolItem/>", 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"<DOMDocument/>");
|
||||
assert_eq!(archive.read("LIBRARY/ball.xml").unwrap().unwrap(), b"<DOMSymbolItem/>");
|
||||
|
||||
// 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"<DOMDocument/>", true), ("b.xml", b"<b/>", true), ("c.xml", b"<c/>", false)]);
|
||||
@@ -306,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<_>>();
|
||||
|
||||
Reference in new issue
Block a user