mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 20:19:40 +00:00
fix(codecs): Place still refuses a header-only image within the allowance
decode_within mapped every LimitExceeded to Ok(None) ("oversized, accept
unread"). craft-image-codecs raises that for its decompression-bomb guard
when a tiny file declares a large image, so Place accepted a header-only
23,170² gray PNG instead of reporting that its pixels can't be read.
Ok(None) now only when the header says the image would take more than the
allowance as samples or as RGBA. A decode that hits a limit stays an error.
This commit is contained in:
@@ -23,8 +23,9 @@ pub fn decode_image(format: craft_image_codecs::Format, bytes: &[u8], allowance:
|
||||
/// `bytes` decoded within `max` bytes, counted as the file's samples and as RGBA (#1717), the
|
||||
/// pixels as stored and as the image crate gave them, with the ICC profile the file embeds.
|
||||
/// Image blobs read their pixels with it. `Ok(None)` when they would take more than `max` as
|
||||
/// samples or as RGBA, or when the decode needs more than `max`. `Err` when the bytes aren't an
|
||||
/// image the decoder reads.
|
||||
/// samples or as RGBA (the header alone decides; the pixels are not read). `Err` when the bytes
|
||||
/// aren't an image the decoder reads, including a file whose header fits the allowance but whose
|
||||
/// pixels can't be decoded (a cut-short file, or one the decoder refuses as a decompression bomb).
|
||||
pub fn decode_within(bytes: &[u8], max: u64) -> Result<Option<DecodedImage>, craft_image_codecs::CodecError> {
|
||||
let format = craft_image_codecs::detect(bytes).ok_or(craft_image_codecs::CodecError::UnknownFormat)?;
|
||||
let (w, h, per_pixel) =
|
||||
@@ -33,11 +34,7 @@ pub fn decode_within(bytes: &[u8], max: u64) -> Result<Option<DecodedImage>, cra
|
||||
if pixels.saturating_mul(per_pixel) > max || pixels.saturating_mul(4) > max {
|
||||
return Ok(None);
|
||||
}
|
||||
match decode_image(format, bytes, max) {
|
||||
Ok(img) => Ok(Some(img)),
|
||||
Err(craft_image_codecs::CodecError::LimitExceeded(_)) => Ok(None),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
decode_image(format, bytes, max).map(Some)
|
||||
}
|
||||
|
||||
/// A decoded image as the image crate's: 8- and 16-bit gray, gray and alpha, RGB and RGBA as they
|
||||
|
||||
+36
-3
@@ -145,9 +145,9 @@ impl ImageBlob {
|
||||
/// This blob after its pixels are decoded once, within [`crate::MAX_RASTER_ALLOC`], to check
|
||||
/// that they can be read; with `proxy`, it also gets the preview [`Self::with_proxy`] makes,
|
||||
/// made from that decode. `None` when the pixels can't be read, as with a file cut short that
|
||||
/// has a header but not all its pixels. An image whose pixels would take more than the
|
||||
/// allowance as RGBA is not decoded, and one whose decode needs more than the allowance stops
|
||||
/// decoding; either is returned as it is.
|
||||
/// has a header but not all its pixels, or one the decoder refuses as a decompression bomb.
|
||||
/// An image whose pixels would take more than the allowance as RGBA is not decoded and is
|
||||
/// returned as it is.
|
||||
pub fn checked_with_proxy(self, proxy: bool) -> Option<Self> {
|
||||
self.checked_with_pixels(proxy).map(|(blob, _)| blob)
|
||||
}
|
||||
@@ -733,4 +733,37 @@ mod tests {
|
||||
assert!(p.pixels().all(|q| q.0 == [200, 30, 30, 128]), "{:?}", p.get_pixel(0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
/// A PNG that says it is `w` × `h` 1-bit grey pixels (header and a stub IDAT, no rows).
|
||||
fn png_header_1bit(w: u32, h: u32) -> Vec<u8> {
|
||||
let mut out = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
let push = |out: &mut Vec<u8>, chunk: &[u8]| {
|
||||
out.extend((chunk.len() as u32 - 4).to_be_bytes());
|
||||
out.extend(chunk);
|
||||
out.extend(crc32(chunk).to_be_bytes());
|
||||
};
|
||||
let mut ihdr = b"IHDR".to_vec();
|
||||
ihdr.extend(w.to_be_bytes());
|
||||
ihdr.extend(h.to_be_bytes());
|
||||
ihdr.extend([1, 0, 0, 0, 0]);
|
||||
push(&mut out, &ihdr);
|
||||
push(&mut out, b"IDAT\x78\x9c");
|
||||
push(&mut out, b"IEND");
|
||||
out
|
||||
}
|
||||
|
||||
/// A header-only 23,170 × 23,170 1-bit gray PNG is within the 2 GiB RGBA allowance, so Place
|
||||
/// decodes it and must refuse it (no pixel rows). 23,171 × 23,170 is past the allowance and is
|
||||
/// kept unread, not refused. craft-image-codecs reports the cut-short file as LimitExceeded
|
||||
/// (its decompression-bomb guard); that is still "pixels can't be read", not "past the allowance".
|
||||
#[test]
|
||||
fn a_gray_header_within_the_allowance_is_unreadable() {
|
||||
let within = png_header_1bit(23_170, 23_170);
|
||||
let over = png_header_1bit(23_171, 23_170);
|
||||
assert!(crate::decode::decode_within(&within, crate::MAX_RASTER_ALLOC).is_err(), "header-only within the allowance");
|
||||
assert!(matches!(crate::decode::decode_within(&over, crate::MAX_RASTER_ALLOC), Ok(None)), "past the allowance is not decoded");
|
||||
assert!(ImageBlob::new("image/png", within).checked_with_pixels(false).is_none(), "Place refuses the file");
|
||||
let (_, pixels) = ImageBlob::new("image/png", over).checked_with_pixels(false).expect("past the allowance is kept unread");
|
||||
assert!(pixels.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user