mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-19 16:45:28 +00:00
Keep refusing damaged OpenEXR headers before a decoder sees them
safe_to_decode accepts a file only when its headers index (or name a compression or part kind this reader doesn't cover); damaged headers, offset tables longer than the file, oversized parts and failed reads are refused again, as the old lenient parse did. Indexing also refuses a channel list the exr crate's validation refused (a zero sampling factor or one not dividing the data window, channels out of order), since headers are now read leniently only. Follows craft-exr's new UnsupportedCompression error kind for HTJ2K and its non-exhaustive error enum. The damaged-file test now checks that the good files index and read and that damaged ones reach the reader.
This commit is contained in:
1 parent
58038903bd
commit
ccd4c75f4f
3 files changed
+84
-7
No files matched your search
@@ -132,23 +132,36 @@ fn index_file(src: &mut dyn ReadSeek) -> Result<seek::Index, seek::Error> {
|
||||
seek::Index::build(src, &limits(), &|| false)
|
||||
}
|
||||
|
||||
/// The headers of the OpenEXR `bytes` are safe to hand to a decoder: no part's data window or
|
||||
/// block is larger than this reader takes. Damaged headers are left to the decoder to report.
|
||||
/// The headers of the OpenEXR `bytes` are safe to hand to a decoder: they can be read and no
|
||||
/// part's data window or block is larger than this reader takes (#3271 review). A file this
|
||||
/// reader doesn't cover (deep parts, HTJ2K) is left to the decoder; damaged headers, a part too
|
||||
/// large and a failed read are refused.
|
||||
pub(crate) fn safe_to_decode(bytes: &[u8]) -> bool {
|
||||
!matches!(index_file(&mut std::io::Cursor::new(bytes)), Err(seek::Error::LimitExceeded(_)))
|
||||
matches!(index_file(&mut std::io::Cursor::new(bytes)), Ok(_) | Err(seek::Error::Unsupported(_) | seek::Error::UnsupportedCompression(_)))
|
||||
}
|
||||
|
||||
/// The channel list of `part` as the `exr` crate's validation took it (#3271): sorted by name,
|
||||
/// every sampling factor non-zero and dividing the data window's position and size. The headers
|
||||
/// are read leniently, and the crate's decompressors divide by the sampling of every channel of
|
||||
/// a chunk, requested or not (where panics abort, in the browser, that would end the app).
|
||||
fn channels_valid(part: &PartInfo) -> bool {
|
||||
let w = part.data_window;
|
||||
let divides = |s: usize, pos: i32, size: u32| i64::try_from(s).ok().filter(|s| *s > 0).is_some_and(|s| i64::from(pos) % s == 0 && i64::from(size) % s == 0);
|
||||
let sorted = part.channels.windows(2).all(|pair| pair.first().zip(pair.get(1)).is_some_and(|(a, b)| a.name.as_bytes() <= b.name.as_bytes()));
|
||||
sorted && part.channels.iter().all(|c| divides(c.sampling.0, w.x, w.width) && divides(c.sampling.1, w.y, w.height))
|
||||
}
|
||||
|
||||
/// Index the OpenEXR file in `src` (identity `stamp`), reading its headers and offset tables
|
||||
/// only.
|
||||
pub(crate) fn build(src: &mut dyn ReadSeek, stamp: FileStamp) -> Result<ExrIndex, IndexError> {
|
||||
let file = index_file(src).map_err(|e| match e {
|
||||
seek::Error::Unsupported(why) if why.contains("HTJ2K") => IndexError::Unsupported(HTJ2K),
|
||||
seek::Error::UnsupportedCompression(name) if name.contains("HTJ2K") => IndexError::Unsupported(HTJ2K),
|
||||
seek::Error::Io(_) => IndexError::Io,
|
||||
_ => IndexError::Unreadable,
|
||||
})?;
|
||||
let parts = file.parts();
|
||||
// Deep data isn't read (the whole file was refused before).
|
||||
if parts.iter().any(|p| p.deep) {
|
||||
if parts.iter().any(|p| p.deep || !channels_valid(p)) {
|
||||
return Err(IndexError::Unreadable);
|
||||
}
|
||||
let display = file.display();
|
||||
|
||||
@@ -864,9 +864,15 @@ fn a_hostile_data_window_is_refused_not_allocated() {
|
||||
fn damaged_files_never_panic() {
|
||||
let files = [single_part_zips(), multipart(), write(vec![part(None, &RGBA, (16, 40), Compression::PIZ)], (16, 40))];
|
||||
for good in &files {
|
||||
// (the good file indexes and reads whole through the same path)
|
||||
let (ix, mut src) = indexed(good);
|
||||
let all = ReadRequest::full((0..ix.channels.len()).collect());
|
||||
assert!(crate::exr_read::read_planes(&mut src, &ix, &all).0.is_ok());
|
||||
assert!(crate::exr_channels::read_exr_channels(good).is_some());
|
||||
let start = chunk_table(good).iter().flatten().map(|(at, _)| *at).min().unwrap() as usize;
|
||||
// From a little before the chunks (the offset tables) to the end, in steps.
|
||||
let from = start.saturating_sub(64);
|
||||
let (mut reads, mut read_ok) = (0, 0);
|
||||
for (i, at) in (from..good.len()).step_by(((good.len() - from) / 150).max(1)).enumerate() {
|
||||
let mut bad = good.clone();
|
||||
bad[at] ^= [0xff, 0x80, 0x01, 0x7f][i % 4];
|
||||
@@ -875,9 +881,66 @@ fn damaged_files_never_panic() {
|
||||
let mut src = std::io::Cursor::new(bad.as_slice());
|
||||
if let Ok(ix) = crate::exr_index::build(&mut src, FileStamp::Virtual) {
|
||||
let req = ReadRequest { rows: Some(3..9), level: 1, ..ReadRequest::full((0..ix.channels.len()).collect()) };
|
||||
let _ = crate::exr_read::read_planes(&mut src, &ix, &req);
|
||||
reads += 1;
|
||||
read_ok += usize::from(crate::exr_read::read_planes(&mut src, &ix, &req).0.is_ok());
|
||||
}
|
||||
}
|
||||
// Most damage lies in the chunks: the index stands and the reader meets the damage.
|
||||
assert!(reads > 50 && read_ok > 0, "{reads} damaged files indexed, {read_ok} read");
|
||||
}
|
||||
}
|
||||
|
||||
/// #3271 review (never crash): damaged headers are refused before a decoder sees them
|
||||
/// ([`crate::exr_index::safe_to_decode`] guards the `image` crate's decoder), and so is a part
|
||||
/// larger than this reader takes; a file the reader doesn't cover but whose headers are sound
|
||||
/// (HTJ2K) is left to the decoders to report.
|
||||
#[test]
|
||||
fn damaged_headers_are_not_safe_to_decode() {
|
||||
let good = write(vec![part(None, &RGBA, (8, 8), Compression::ZIP1)], (8, 8));
|
||||
assert!(crate::exr_index::safe_to_decode(&good));
|
||||
// Cut inside the headers.
|
||||
let cut = good.windows(11).position(|w| w == b"compression").unwrap();
|
||||
assert!(!crate::exr_index::safe_to_decode(&good[..cut]), "headers cut short");
|
||||
// Not an OpenEXR file.
|
||||
let mut magic = good.clone();
|
||||
magic[0] ^= 0xff;
|
||||
assert!(!crate::exr_index::safe_to_decode(&magic), "no signature");
|
||||
// Offset tables longer than the file.
|
||||
let first_chunk = chunk_table(&good)[0][0].0 as usize;
|
||||
assert!(!crate::exr_index::safe_to_decode(&good[..first_chunk - 8]), "tables cut short");
|
||||
assert!(!crate::exr_index::safe_to_decode(&with_data_window_x_max(good.clone(), (1 << 30) - 3)), "a part too large");
|
||||
// HTJ2K (compression 11): sound headers, left to the decoders.
|
||||
let mut htj2k = good.clone();
|
||||
let tag = b"compression\0compression\0\x01\0\0\0";
|
||||
let at = htj2k.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len();
|
||||
htj2k[at] = 11;
|
||||
assert_eq!(
|
||||
crate::exr_index::build(&mut std::io::Cursor::new(htj2k.as_slice()), FileStamp::Virtual).err(),
|
||||
Some(crate::exr_index::IndexError::Unsupported(crate::exr_index::HTJ2K))
|
||||
);
|
||||
assert!(crate::exr_index::safe_to_decode(&htj2k));
|
||||
}
|
||||
|
||||
/// #3271 review (never crash): the headers are read leniently, so a channel list the `exr`
|
||||
/// crate's validation refused (a zero sampling factor, which its decompressors divide by, or
|
||||
/// channels out of order) is refused when indexing, before any chunk is decompressed.
|
||||
#[test]
|
||||
fn an_invalid_channel_list_is_refused() {
|
||||
let good = write(vec![part(None, &[("G", true), ("R", true)], (4, 4), Compression::ZIP1)], (4, 4));
|
||||
let tag = b"channels\0chlist\0";
|
||||
let list = good.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len() + 4;
|
||||
// Each entry: name, NUL, pixel type (4), linear (1), reserved (3), x and y sampling (4 + 4).
|
||||
let mut zero = good.clone();
|
||||
let x_sampling = list + 2 + 4 + 1 + 3;
|
||||
zero[x_sampling..x_sampling + 4].copy_from_slice(&0i32.to_le_bytes());
|
||||
let mut swapped = good.clone();
|
||||
swapped[list] = b'R';
|
||||
swapped[list + 2 + 16] = b'G';
|
||||
assert!(crate::exr_index::build(&mut std::io::Cursor::new(good.as_slice()), FileStamp::Virtual).is_ok());
|
||||
for bad in [zero, swapped] {
|
||||
assert!(crate::exr_index::build(&mut std::io::Cursor::new(bad.as_slice()), FileStamp::Virtual).is_err());
|
||||
assert!(crate::exr_channels::read_exr_channels(&bad).is_none());
|
||||
assert!(crate::exr_channels::layered_image(&bad).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -271,7 +271,8 @@ fn read(src: &mut dyn ReadSeek, index: &ExrIndex, req: &ReadRequest) -> Result<(
|
||||
seek::Error::Cancelled => ReadError::Cancelled,
|
||||
seek::Error::Io(why) => ReadError::Io(why),
|
||||
seek::Error::LimitExceeded(why) => ReadError::TooLarge(format!("its OpenEXR channels need more memory than one file may use ({why})")),
|
||||
seek::Error::Malformed(why) | seek::Error::Unsupported(why) => ReadError::Unreadable(why),
|
||||
seek::Error::Malformed(why) | seek::Error::Unsupported(why) | seek::Error::UnsupportedCompression(why) => ReadError::Unreadable(why),
|
||||
other => ReadError::Unreadable(other.to_string()),
|
||||
})?;
|
||||
let stats = ReadStats { chunks: planes.stats.chunks, bytes: planes.stats.bytes, lines: planes.stats.lines };
|
||||
Ok((planes.planes, stats))
|
||||
|
||||
Reference in new issue
Block a user