Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-IMP-FORMATS: reject incomplete declared small RAW strips

radialmonster06dae821892f
diff --git a/crates/raw/src/vendor/nef.rs b/crates/raw/src/vendor/nef.rs
index c404a2e29..10bb24d50 100644
--- a/crates/raw/src/vendor/nef.rs
+++ b/crates/raw/src/vendor/nef.rs
@@ -589,7 +589,9 @@ fn small_raw(bytes: &[u8], tiff: &Tiff, info: &ImageInfo, strip: lightcraft_tiff
let (w, h) = (info.width as usize, info.height as usize);
let stride = w.checked_mul(3).ok_or(RawError::Limit("image too large"))?;
let count = stride.checked_mul(h).filter(|n| *n > 0 && *n <= crate::MAX_SAMPLES).ok_or(RawError::Limit("image too large"))?;
- let src = lightcraft_tiff::image::chunk_bytes(bytes, &strip).ok_or_else(|| RawError::Corrupt("NEF: image data outside the file".into()))?;
+ let src = lightcraft_tiff::image::chunk_bytes(bytes, &strip)
+ .filter(|src| u64::try_from(src.len()).ok() == Some(strip.len))
+ .ok_or_else(|| RawError::Corrupt("NEF: incomplete image strip".into()))?;
let data = match mode {
Mode::Full => {
let mut data = vec![0u16; count];
@@ -1413,4 +1415,15 @@ mod tests {
}
}
}
+
+ #[test]
+ fn small_raw_rejects_tail_truncation_with_intact_directories() {
+ let bytes = nef(34713, 12, vec![vec![0x55; 72]], 8, 3, 3);
+ for keep in [0, 24, 71] {
+ let short = truncated_tail_strip(bytes.clone(), keep);
+ for mode in [Mode::Header, Mode::Full] {
+ assert!(matches!(decode(&short, mode), Err(RawError::Corrupt(_))));
+ }
+ }
+ }
}