PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(format): ignore accidental ZIP directory signatures in comments
Jason Xiang3264c2fa6f31
diff --git a/crates/format/src/zip.rs b/crates/format/src/zip.rsindex 9ec92c2fb..5057e5216 100644--- a/crates/format/src/zip.rs+++ b/crates/format/src/zip.rs@@ -127,11 +127,24 @@ impl<'a> ZipReader<'a> {if bytes.len() < 22 {return Err(FormatError::corrupt("not a zip archive (too short)"));}- // End of central directory: scan back over a possible comment.+ // A comment may itself contain the signature. Only accept a record whose+ // declared comment reaches the end of the archive (APPNOTE 4.3.16).let min = bytes.len().saturating_sub(22 + 65535);let eocd = (min..=bytes.len() - 22).rev()- .find(|&i| u32_at(bytes, i).ok() == Some(EOCD_SIG))+ .find(|&i| {+ if u32_at(bytes, i).ok() != Some(EOCD_SIG) {+ return false;+ }+ let comment_end = u16_at(bytes, i + 20).ok().and_then(|length| i.checked_add(22)?.checked_add(usize::from(length)));+ let directory_end = u32_at(bytes, i + 16)+ .ok()+ .zip(u32_at(bytes, i + 12).ok())+ .and_then(|(offset, size)| usize::try_from(offset).ok()?.checked_add(usize::try_from(size).ok()?));+ // In the supported non-ZIP64 layout, the central directory immediately+ // precedes this record. A zeroed signature in a comment is not an empty ZIP.+ comment_end == Some(bytes.len()) && directory_end == Some(i)+ }).ok_or_else(|| FormatError::corrupt("zip end-of-central-directory not found"))?;let count = u16_at(bytes, eocd + 10)? as usize;let cd_size = u32_at(bytes, eocd + 12)? as usize;@@ -263,6 +276,56 @@ mod tests {assert!(matches!(r.read_by_name("a.txt", 3), Err(FormatError::LimitExceeded(_))));}+ fn archive_with_comment(comment: &[u8]) -> Vec<u8> {+ let mut writer = ZipWriter::new();+ writer.add("manifest.json", b"project content").unwrap();+ let mut bytes = writer.finish().unwrap();+ let length_offset = bytes.len() - 2;+ bytes[length_offset..].copy_from_slice(&u16::try_from(comment.len()).unwrap().to_le_bytes());+ bytes.extend_from_slice(comment);+ bytes+ }++ #[test]+ fn directory_signature_inside_comment_does_not_hide_entries() {+ for length in [22, 32, usize::from(u16::MAX)] {+ let mut comment = vec![0; length];+ comment[..4].copy_from_slice(&EOCD_SIG.to_le_bytes());+ let bytes = archive_with_comment(&comment);+ let reader = ZipReader::new(&bytes).unwrap();+ assert_eq!(reader.entries.len(), 1, "comment length {length}");+ assert_eq!(reader.read_by_name("manifest.json", 100).unwrap(), b"project content");+ }+ }++ #[test]+ fn archive_comments_support_empty_short_and_maximum_lengths() {+ for length in [0, 1, 21, 22, 100, usize::from(u16::MAX)] {+ let bytes = archive_with_comment(&vec![b'x'; length]);+ let reader = ZipReader::new(&bytes).unwrap();+ assert_eq!(reader.read_by_name("manifest.json", 100).unwrap(), b"project content", "comment length {length}");+ }+ }++ #[test]+ fn central_directory_digital_signature_remains_supported() {+ let mut bytes = archive_with_comment(b"signed archive");+ let eocd = bytes.len() - 22 - b"signed archive".len();+ let signature = [0x50, 0x4b, 0x05, 0x05, 3, 0, 1, 2, 3];+ let size = u32_at(&bytes, eocd + 12).unwrap() + signature.len() as u32;+ bytes[eocd + 12..eocd + 16].copy_from_slice(&size.to_le_bytes());+ bytes.splice(eocd..eocd, signature);+ let reader = ZipReader::new(&bytes).unwrap();+ assert_eq!(reader.read_by_name("manifest.json", 100).unwrap(), b"project content");+ }++ #[test]+ fn truncated_archive_comment_is_rejected() {+ let mut bytes = archive_with_comment(b"comment");+ bytes.pop();+ assert!(ZipReader::new(&bytes).is_err());+ }+#[test]fn crc_matches_bitwise_reference() {// Bit-at-a-time IEEE CRC-32 as the oracle, over lengths that cross