Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Images: refuse a raster that would decode past 1 GiB of RGBA (fixes #1586)

VoiceLessQ5eddc821ecff
diff --git a/ROADMAP.md b/ROADMAP.md
index c02176bab..16fb72145 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -380,6 +380,7 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.m
Newest first.
+- **2026-10-28** · A placed image whose pixels would decode past 1 GiB of RGBA (256 Mpx) is refused from its header, before decoding (#1586). A 288 KB 23,000 px gray PNG used to hang the first draw for 9 s and take the app to 4.5 GB; it now draws as an empty frame, and PDF export skips it with a warning. A 300 ppi A0 scan still fits. No readiness percentage change.
- **2026-10-27** · An iconic panel's flyout stays open while you work in the canvas or a text field: Auto-Collapse Iconic Panels is off by default, and preferences saved with it on start with it off. Preferences › Interface › Panels holds the setting next to the column bar's menu item. No readiness percentage change.
- **2026-10-24** · Column rules run from the top line's ascent to the bottom line's descent, as InDesign draws them, instead of from the top to the bottom of the columns, on screen, in PDF and in PowerPoint export. The top and bottom insets shorten the rule from those lines. Existing documents draw their column rules at the new length. No readiness percentage change.
- **2026-10-20** · Edit › Keyboard Shortcuts is craft-keymap's shared dialog: commands grouped by menu with Show and Filter, recording that asks before taking a key from another command, undo and redo, named shortcut sets (edits of Default become Custom; an imported set becomes a set of its own), an on-screen US keyboard, InDesign Show Set files reviewed before they apply, JSON export and an HTML summary. ui.json keeps the `shortcuts` map earlier versions read, for the active set. No readiness percentage change.
diff --git a/crates/images/src/lib.rs b/crates/images/src/lib.rs
index 817aa7829..117017502 100644
--- a/crates/images/src/lib.rs
+++ b/crates/images/src/lib.rs
@@ -324,10 +324,17 @@ pub fn pixel_size(bytes: &[u8]) -> Option<(u32, u32)> {
.or_else(|| tiff_decoder(bytes)?.dimensions().ok())
}
+/// The largest decoded raster, in RGBA bytes: 256 Mpx, a 300 ppi scan of an A0 sheet fits.
+const MAX_RGBA_BYTES: u64 = 1 << 30;
+
/// Straight (non-premultiplied) RGBA8 of a raster in display sRGB: an embedded ICC profile is
/// applied, and CMYK goes through its own profile or the working CMYK space ([`colour`]).
pub fn decode_rgba(bytes: &[u8]) -> Option<image::RgbaImage> {
use colour::{DisplayColour, Pixels, to_display_rgba};
+ // `image` limits only its own output, which can be a quarter of the RGBA copy or less.
+ if pixel_size(bytes).is_some_and(|(w, h)| u64::from(w).saturating_mul(u64::from(h)).saturating_mul(4) > MAX_RGBA_BYTES) {
+ return None;
+ }
if is_psd(bytes) {
return psd::decode(bytes);
}
diff --git a/crates/images/src/tests.rs b/crates/images/src/tests.rs
index ba6e032eb..54a173cd1 100644
--- a/crates/images/src/tests.rs
+++ b/crates/images/src/tests.rs
@@ -401,3 +401,41 @@ fn bmp_decodes() {
assert_eq!(pixel_size(&bmp), Some((3, 2)));
assert_eq!(decode_rgba(&bmp).unwrap().get_pixel(1, 1).0, [0, 0, 255, 255]);
}
+
+/// A 1-bit gray PNG of `side` x `side` blank pixels, written row by row so the test stays small.
+fn blank_png(side: u32) -> Vec<u8> {
+ use std::io::Write;
+ let chunk = |out: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]| {
+ out.extend((data.len() as u32).to_be_bytes());
+ let mut crc = flate2::Crc::new();
+ crc.update(kind);
+ crc.update(data);
+ out.extend(kind);
+ out.extend(data);
+ out.extend(crc.sum().to_be_bytes());
+ };
+ let row = vec![0u8; 1 + side.div_ceil(8) as usize];
+ let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast());
+ for _ in 0..side {
+ z.write_all(&row).unwrap();
+ }
+ let mut ihdr = [side.to_be_bytes(), side.to_be_bytes()].concat();
+ ihdr.extend([1, 0, 0, 0, 0]);
+ let mut png = b"\x89PNG\r\n\x1a\n".to_vec();
+ chunk(&mut png, b"IHDR", &ihdr);
+ chunk(&mut png, b"IDAT", &z.finish().unwrap());
+ chunk(&mut png, b"IEND", &[]);
+ png
+}
+
+/// A small file that expands past 1 GiB of RGBA is refused from its header (#1586).
+#[test]
+fn rasters_past_the_rgba_budget_are_refused_before_decoding() {
+ let png = blank_png(16_385);
+ assert!(png.len() < 1 << 20, "{} bytes", png.len());
+ assert_eq!(pixel_size(&png), Some((16_385, 16_385)));
+ assert_eq!(natural_size(&png), Some((16_385.0, 16_385.0)));
+ assert!(decode_rgba(&png).is_none());
+ let small = decode_rgba(&blank_png(9)).expect("decodes");
+ assert_eq!(small.get_pixel(8, 8).0, [0, 0, 0, 255]);
+}