From 0fb4bbf1dbf25a56bd1e11e412747bc2a07559fe Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Sun, 11 Oct 2026 16:40:31 +0200 Subject: [PATCH] PSD: don't inflate an over-budget ZIP merged image at parse time (#2926) (#2967) validate_planes bounded its zlib loop by the uncapped declared size, so a PSD/PSB declaring more than MAX_DECODED_BYTES of merged image could make PsdFile::from_bytes decompress far past the decode budget before any error. decode_planes refuses such a layout with LimitExceeded anyway, so the stream is now kept verbatim without inflating it, like an unknown compression method. A layered file whose merged composite is too large still opens (the importer only warns when the composite can't be decoded). Co-authored-by: Claude Opus 5.5 --- crates/psd/README.md | 2 +- crates/psd/src/compression.rs | 25 +++++++++++++++++++++++++ crates/psd/src/file.rs | 13 +++++++++++++ crates/psd/tests/malformed.rs | 3 ++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/crates/psd/README.md b/crates/psd/README.md index 74870635b1..c3721edf26 100644 --- a/crates/psd/README.md +++ b/crates/psd/README.md @@ -98,7 +98,7 @@ testgen::{all_cases, merged_only, layered, small, sample_descriptor, pattern_pla | Layer masks | ✓ | ✓ | ✓ | 20 / 36-byte forms, mask parameters, real mask; unparseable masks stay raw | | Channel ids -1 / -2 / -3 | ✓ | ✓ | ✓ | per-channel rect via `channel_rect` | | Raw / RLE / ZIP / ZIP+prediction | ✓ | ✓ | – | prediction at 8/16/32 bits (32: byte-plane shuffle + delta); RLE counts are u16 in PSD, u32 in PSB | -| Merged image data | ✓ | ✓ | – | validated at parse time (sizes, RLE counts, zlib stream incl. checksum) | +| Merged image data | ✓ | ✓ | – | validated at parse time (sizes, RLE counts, zlib stream incl. checksum); a zlib stream declaring more than `MAX_DECODED_BYTES` is kept verbatim without inflating it, and decoding it reports the limit | | Tagged blocks (raw passthrough) | ✓ | ✓ | – | keeps `8BIM` / `8B64` and exact padding | | PSB 8-byte-length keys | ✓ | ✓ | – | LMsk Lr16 Lr32 Layr Mt16 Mt32 Mtrn Alph FMsk lnk2 FEid FXid PxSD | | Lr16 / Lr32 / Layr layer info | ✓ | ✓ | ✓ | lifted into `layer_info`; re-inserted at its original position | diff --git a/crates/psd/src/compression.rs b/crates/psd/src/compression.rs index 7a50b3ceca..d47408d28c 100644 --- a/crates/psd/src/compression.rs +++ b/crates/psd/src/compression.rs @@ -244,6 +244,13 @@ pub fn encode_planes(compression: Compression, decoded: &[u8], layout: &PlaneLay } /// Validates encoded sizes and, for ZIP, the complete stream and checksum. +/// +/// A ZIP stream whose declared size is over [`MAX_DECODED_BYTES`] is not +/// decompressed at all (#2926): [`decode_planes`] refuses that layout with +/// `LimitExceeded` anyway, so inflating it here would only be work on an +/// untrusted file before the same error. The bytes are kept verbatim, like an +/// unknown compression method, so a layered file whose merged composite is +/// too large still opens. pub(crate) fn validate_planes(compression: Compression, data: &[u8], layout: &PlaneLayout) -> Result<()> { let total = layout.total_bytes()?; match compression { @@ -266,6 +273,7 @@ pub(crate) fn validate_planes(compression: Compression, data: &[u8], layout: &Pl } Ok(()) } + Compression::Zip | Compression::ZipPrediction if total > MAX_DECODED_BYTES => Ok(()), Compression::Zip | Compression::ZipPrediction => inflate_checked(data, total, false).map(|_| ()), Compression::Unknown(_) => Ok(()), } @@ -708,6 +716,23 @@ mod tests { } } + /// #2926: a ZIP stream declaring more than `MAX_DECODED_BYTES` is not inflated at parse + /// time. Garbage zlib data proves it: decompressing it would fail, skipping it passes, and + /// decoding the layout still reports the size limit. + #[test] + fn zip_over_budget_is_not_decompressed_at_parse() { + let l = layout(4, 300_000, 300_000, 8, Version::Psb); + assert!(l.total_bytes().unwrap() > MAX_DECODED_BYTES); + let garbage = [0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03]; + for c in [Compression::Zip, Compression::ZipPrediction] { + validate_planes(c, &garbage, &l).unwrap(); + assert!(matches!(decode_planes(c, &garbage, &l), Err(PsdError::LimitExceeded(_))), "{c:?}"); + } + // Within the budget the stream is still checked. + let small = layout(1, 4, 4, 8, Version::Psd); + assert!(validate_planes(Compression::Zip, &garbage, &small).is_err()); + } + #[test] fn raw_short_fails() { let l = layout(2, 3, 3, 8, Version::Psd); diff --git a/crates/psd/src/file.rs b/crates/psd/src/file.rs index e493eddc1a..3c621bb25a 100644 --- a/crates/psd/src/file.rs +++ b/crates/psd/src/file.rs @@ -338,6 +338,19 @@ mod write_tests { } } + /// #2926: a PSB whose ZIP merged image declares more than `MAX_DECODED_BYTES` opens without + /// inflating that stream (its layers are still usable); decoding the composite reports the limit. + #[test] + fn over_budget_zip_merged_image_opens_without_inflating() { + let mut bytes = testgen::small(Version::Psb, Compression::Zip).to_bytes().unwrap(); + // Header: height at 14, width at 18 (big-endian u32). + bytes[14..18].copy_from_slice(&300_000u32.to_be_bytes()); + bytes[18..22].copy_from_slice(&300_000u32.to_be_bytes()); + let file = PsdFile::from_bytes(&bytes).unwrap(); + assert!(!file.layers().is_empty()); + assert!(matches!(file.decode_merged(), Err(PsdError::LimitExceeded(_)))); + } + #[test] fn global_layer_info_serialization_propagates_layer_limit_errors() { let mut file = testgen::small(Version::Psd, Compression::Raw); diff --git a/crates/psd/tests/malformed.rs b/crates/psd/tests/malformed.rs index 85968e72cb..cd8a0d7dab 100644 --- a/crates/psd/tests/malformed.rs +++ b/crates/psd/tests/malformed.rs @@ -114,10 +114,11 @@ fn huge_declared_dimensions_small_data() { f.header.height = 300_000; let b = f.to_bytes().unwrap(); assert!(PsdFile::from_bytes(&b).is_err()); + // ZIP over `MAX_DECODED_BYTES` is not inflated at parse time (#2926); decoding refuses it. f.image_data.compression = Compression::Zip; f.image_data.data = compression::zip_compress(&[0; 100]); let b = f.to_bytes().unwrap(); - assert!(PsdFile::from_bytes(&b).is_err()); + assert!(matches!(PsdFile::from_bytes(&b).and_then(|f| f.decode_merged()), Err(PsdError::LimitExceeded(_)))); } #[test]