raw: refuse lossy DNG tiles that carry Extended XMP (#11042)

* raw: refuse lossy DNG tiles that carry Extended XMP

zune-jpeg keeps every Extended XMP fragment until its set is complete
and re-sorts and re-checks all pending fragments after each marker, so a
lossy (compression 34892) tile of many small incomplete fragments costs
quadratic time before a pixel is decoded. A raw tile has no metadata:
refuse one with an Extended XMP APP1 segment before parsing it.

* address review: bound Extended XMP identifier to APP1 payload
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-03 20:37:07 -05:00
1 parent 4367131008
commit 06bedca787
1 file changed
+54
+54
View File
@@ -467,6 +467,11 @@ fn lossy_jpeg(src: &[u8], s: &Segment, rows: usize, samples: usize, bits: u32, t
3 => ColorSpace::RGB,
n => return Err(RawError::unsupported(format!("lossy DNG with {n} samples per pixel (1 or 3 are supported)"))),
};
// zune-jpeg re-sorts and re-checks every pending Extended XMP fragment after each marker it
// parses, which is quadratic in a tile of many small fragments; a raw tile has no metadata.
if has_extended_xmp(src) {
return Err(RawError::unsupported("lossy JPEG tile with Extended XMP metadata"));
}
let options = DecoderOptions::default().jpeg_set_out_colorspace(out).set_max_width(s.w).set_max_height(s.h);
let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(src), options);
dec.decode_headers().map_err(|e| RawError::malformed(format!("lossy JPEG tile header: {e:?}")))?;
@@ -492,6 +497,36 @@ fn lossy_jpeg(src: &[u8], s: &Segment, rows: usize, samples: usize, bits: u32, t
Ok(v)
}
/// Whether a JPEG stream has an APP1 Extended XMP segment before its first scan. Walks the
/// markers as a header parse does: stray bytes skip to the next `FF`.
fn has_extended_xmp(b: &[u8]) -> bool {
const EXTENDED_XMP: &[u8] = b"http://ns.adobe.com/xmp/extension/\0";
let mut i = 2; // past SOI
while let Some(&byte) = b.get(i) {
if byte != 0xFF {
match b[i..].iter().position(|&v| v == 0xFF) {
Some(n) => i += n,
None => return false,
}
continue;
}
match b.get(i + 1) {
None | Some(0xD9 | 0xDA) => return false,
Some(0xFF) => i += 1,
Some(0x01 | 0xD0..=0xD8) => i += 2,
Some(&code) => {
let Some(len) = b.get(i + 2..i + 4).map(|l| usize::from(u16::from_be_bytes([l[0], l[1]]))) else { return false };
// The identifier must lie inside the segment's declared payload (length counts its own two bytes).
if code == 0xE1 && b.get(i + 4..(i + 2 + len).min(b.len())).is_some_and(|p| p.starts_with(EXTENDED_XMP)) {
return true;
}
i += 2 + len;
}
}
}
false
}
/// Reverses TIFF predictor 2 (horizontal differencing) on interleaved samples: each sample
/// gains its channel's predecessor in the row, wrapping at the sample's container (8 or 16 bits).
fn undelta(mut out: Vec<u16>, plane: usize, row_samples: usize, rows: usize, bits: u32) -> Vec<u16> {
@@ -592,6 +627,25 @@ fn unpack_row_msb(row: &[u8], out: &mut [u16], bits: u32) {
mod tests {
use super::*;
#[test]
fn extended_xmp_is_found_before_the_scan_only() {
let app1 = |payload: &[u8]| {
let mut seg = vec![0xFF, 0xE1];
seg.extend_from_slice(&u16::try_from(payload.len() + 2).unwrap().to_be_bytes());
seg.extend_from_slice(payload);
seg
};
let xmp = app1(b"http://ns.adobe.com/xmp/extension/\0GUID");
let plain = app1(b"http://ns.adobe.com/xap/1.0/\0<x/>");
let jpeg = |segs: &[&[u8]]| [&[0xFF, 0xD8][..], &segs.concat(), &[0xFF, 0xDA, 0, 2, 0xFF, 0xD9]].concat();
assert!(has_extended_xmp(&jpeg(&[&plain, &xmp])));
assert!(!has_extended_xmp(&jpeg(&[&plain])));
// After the scan it is entropy data, not a marker the header parse reads.
assert!(!has_extended_xmp(&[jpeg(&[]), xmp.clone()].concat()));
// A truncated length ends the walk.
assert!(!has_extended_xmp(&[0xFF, 0xD8, 0xFF, 0xE1, 0x00]));
}
/// The grouped 10/12/14-bit readers equal the plain bit reader on every row width.
#[test]
fn grouped_unpack_matches_the_bit_reader() {