mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
craft-heif: a protected Exif or XMP item is unreadable, not absent
A nonzero protection index made `item_info` return before it read the item type, so a protected `Exif` or XMP item vanished: `scan` was complete and empty, as if the file held no metadata. The protection index doesn't hide the type, so read it, and return the item from `scan` as `Unreadable::Protected`. `read` still leaves it out.
This commit is contained in:
@@ -145,6 +145,7 @@ impl Iterator for Scan<'_> {
|
||||
Gathered::NotPlaced => continue,
|
||||
Gathered::Unreadable => Err(Unreadable::Placement),
|
||||
Gathered::TooLarge => Err(Unreadable::TooLarge),
|
||||
Gathered::Protected => Err(Unreadable::Protected),
|
||||
};
|
||||
return Some(Item { id, kind, data });
|
||||
}
|
||||
@@ -184,6 +185,9 @@ pub enum Unreadable {
|
||||
Placement,
|
||||
/// More than [`MAX_ITEM`] bytes, or more than [`scan`] has left of [`MAX_SCANNED_BYTES`].
|
||||
TooLarge,
|
||||
/// The item is protected (encrypted): its `infe` entry names a protection scheme, so its
|
||||
/// stored bytes are not the metadata in the clear.
|
||||
Protected,
|
||||
}
|
||||
|
||||
/// The TIFF stream of an `Exif` item: past its 4-byte offset field and the bytes that offset
|
||||
@@ -214,6 +218,8 @@ struct Candidate {
|
||||
describes: bool,
|
||||
/// Tied to the primary image or an image it is made from.
|
||||
describes_primary: bool,
|
||||
/// Protected (encrypted): there, but not readable.
|
||||
protected: bool,
|
||||
}
|
||||
|
||||
impl Candidate {
|
||||
@@ -308,6 +314,9 @@ impl<'a> Container<'a> {
|
||||
|
||||
/// The bytes of `items[i]`, at most `limit` of them.
|
||||
fn gather(&self, i: usize, limit: usize) -> Gathered {
|
||||
if self.items.get(i).is_some_and(|c| c.protected) {
|
||||
return Gathered::Protected;
|
||||
}
|
||||
match (&self.iloc, self.places.get(i)) {
|
||||
(Some(iloc), Some(Some(place))) => iloc.gather(self.file, self.idat, place, limit),
|
||||
_ if self.iloc_complete => Gathered::NotPlaced,
|
||||
@@ -326,6 +335,8 @@ enum Gathered {
|
||||
Unreadable,
|
||||
/// More than the limit.
|
||||
TooLarge,
|
||||
/// A protected (encrypted) item.
|
||||
Protected,
|
||||
}
|
||||
|
||||
/// A full box's version: the first byte of its own payload (`None` for an empty box).
|
||||
@@ -370,14 +381,14 @@ fn item_infos(bytes: &[u8], iinf: &BoxRef, cap: usize, budget: &mut usize) -> It
|
||||
catalog.complete = entries.last().map_or(first, |b| b.end) == iinf.end && u32::try_from(entries.len()).ok() == Some(count);
|
||||
for b in &entries {
|
||||
let info = bytes.get(b.body..b.end).filter(|_| &b.kind == b"infe").and_then(item_info);
|
||||
let Some((id, kind)) = info else {
|
||||
let Some((id, kind, protected)) = info else {
|
||||
catalog.complete = false;
|
||||
continue;
|
||||
};
|
||||
catalog.ids.push(id);
|
||||
match kind {
|
||||
Some(_) if catalog.items.len() >= cap => catalog.more = true,
|
||||
Some(kind) => catalog.items.push(Candidate { id, kind, describes: false, describes_primary: false }),
|
||||
Some(kind) => catalog.items.push(Candidate { id, kind, describes: false, describes_primary: false, protected }),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
@@ -386,9 +397,11 @@ fn item_infos(bytes: &[u8], iinf: &BoxRef, cap: usize, budget: &mut usize) -> It
|
||||
catalog
|
||||
}
|
||||
|
||||
/// An `infe` entry's item id, and its kind when it is an unprotected `Exif` or XMP item (`mime`,
|
||||
/// `application/rdf+xml`). `None` when the entry can't be read to its last field.
|
||||
fn item_info(infe: &[u8]) -> Option<(u32, Option<Kind>)> {
|
||||
/// An `infe` entry's item id, its kind when it is an `Exif` or XMP item (`mime`,
|
||||
/// `application/rdf+xml`), and whether it is protected (encrypted). A protected entry still names
|
||||
/// its type in the clear, so protected metadata is found too. `None` when the entry can't be read
|
||||
/// to its last field.
|
||||
fn item_info(infe: &[u8]) -> Option<(u32, Option<Kind>, bool)> {
|
||||
let version = *infe.first()?;
|
||||
// version and flags, the item id (32 bits in version 3), the protection index
|
||||
let (id, at) = match version {
|
||||
@@ -396,9 +409,7 @@ fn item_info(infe: &[u8]) -> Option<(u32, Option<Kind>)> {
|
||||
3 => (be32(infe, 4)?, 8usize),
|
||||
_ => return None,
|
||||
};
|
||||
if be16(infe, at)? != 0 {
|
||||
return Some((id, None)); // protected (encrypted)
|
||||
}
|
||||
let protected = be16(infe, at)? != 0;
|
||||
let at = at.checked_add(2)?;
|
||||
// versions 0 and 1 have no item type and describe MIME items; 2 and 3 name the type
|
||||
let (kind, at) = if version >= 2 { (infe.get(at..at.checked_add(4)?)?, at.checked_add(4)?) } else { (b"mime".as_slice(), at) };
|
||||
@@ -421,7 +432,7 @@ fn item_info(infe: &[u8]) -> Option<(u32, Option<Kind>)> {
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
Some((id, kind))
|
||||
Some((id, kind, protected))
|
||||
}
|
||||
|
||||
/// The NUL-terminated string at `at` and where the next field starts.
|
||||
|
||||
@@ -707,14 +707,15 @@ fn malformed_bounds_are_refused() {
|
||||
let mut iref = vec![0, 0, 0, 0];
|
||||
iref.extend(bx(b"cdsc", &[0, 2, 0xff, 0xff]));
|
||||
let iref = bx(b"iref", &iref);
|
||||
let mut items = vec![Candidate { id: 2, kind: Kind::Exif, describes: false, describes_primary: false }];
|
||||
let mut items = vec![Candidate { id: 2, kind: Kind::Exif, describes: false, describes_primary: false, protected: false }];
|
||||
let mut budget = MAX_BOXES;
|
||||
let b = boxes(&iref, 0, iref.len(), &mut budget);
|
||||
link(&iref, &b[0], Some(1), &mut items, &mut budget);
|
||||
assert!(!items[0].describes);
|
||||
assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 0, b'E', b'x']), None, "cut short: the item type is unknown");
|
||||
assert_eq!(item_info(&[4, 0, 0, 0, 0, 2, 0, 0, b'E', b'x', b'i', b'f', 0]), None, "unknown infe version");
|
||||
assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 1, b'E', b'x', b'i', b'f', 0]), Some((2, None)), "protected");
|
||||
assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 1, b'E', b'x', b'i', b'f', 0]), Some((2, Some(Kind::Exif), true)), "protected");
|
||||
assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 1, b'E', b'x']), None, "protected and cut short");
|
||||
// a full box's version is its own first byte, never the next box's
|
||||
let two = [bx(b"iref", &[]), bx(b"free", &[1, 2, 3])].concat();
|
||||
let mut budget = MAX_BOXES;
|
||||
@@ -946,7 +947,7 @@ fn item_catalog_uncertainty_keeps_readable_exif() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Catalogs without metadata items, and protected items, leave a complete, empty scan.
|
||||
/// Catalogs without metadata items leave a complete, empty scan.
|
||||
#[test]
|
||||
fn item_catalog_benign_entries_and_empty_tables_are_complete() {
|
||||
let h = Heif::new();
|
||||
@@ -965,15 +966,35 @@ fn item_catalog_benign_entries_and_empty_tables_are_complete() {
|
||||
assert_eq!(sure(&with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry))))), Some(Vec::new()));
|
||||
}
|
||||
}
|
||||
// A protected (encrypted) item is not a metadata item this reader can read.
|
||||
let mut entry = catalog_entry(2, 2, b"Exif", b"\0");
|
||||
// A protected (encrypted) image item is no metadata item.
|
||||
let mut entry = catalog_entry(2, 1, b"hvc1", b"\0");
|
||||
entry[7] = 1;
|
||||
let h = h.item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1);
|
||||
assert_eq!(sure(&with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry))))), Some(Vec::new()));
|
||||
// Complete top-level files without an item meta box have no item catalog to inspect.
|
||||
assert_eq!(sure(&bx(b"ftyp", b"msf1\0\0\0\0msf1")), Some(Vec::new()));
|
||||
}
|
||||
|
||||
/// A protected (encrypted) `Exif` or XMP item is in the scan, as unreadable: the file holds
|
||||
/// metadata that can't be checked. [`read`] leaves it out.
|
||||
#[test]
|
||||
fn protected_metadata_is_unreadable_not_absent() {
|
||||
let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1);
|
||||
for (kind, fields, expected) in [(b"Exif", b"\0".as_slice(), Kind::Exif), (b"mime", b"name\0application/rdf+xml\0", Kind::Xmp)] {
|
||||
for version in [2, 3] {
|
||||
let mut entry = catalog_entry(version, 2, kind, fields);
|
||||
let protection_at = if version == 3 { 9 } else { 7 };
|
||||
entry[protection_at] = 1;
|
||||
let f = with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry))));
|
||||
let s = scan(&f).unwrap();
|
||||
assert!(s.complete(), "{kind:?} v{version}");
|
||||
let found: Vec<Item> = s.collect();
|
||||
assert_eq!(found, [Item { id: 2, kind: expected, data: Err(Unreadable::Protected) }], "{kind:?} v{version}");
|
||||
assert_eq!(sure(&f), None, "{kind:?} v{version}");
|
||||
assert_eq!(read(&f).unwrap(), (None, None), "{kind:?} v{version}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Running out of box budget, or a walk that stops before the end of its box, never makes a
|
||||
/// scan complete.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user