mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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 <[email protected]>
This commit is contained in:
1 parent
1fdb0378a3
commit
0fb4bbf1db
4 files changed
+41
-2
No files matched your search
@@ -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 |
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user