From 76bd5dc1018966cce0f709dbfc61e294ea942437 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 2 Nov 2026 22:51:02 -0800 Subject: [PATCH] Import: budget PhotoCraft layer buffers before decoding pixels --- .../engine/src/interchange/pcraft_import.rs | 6 +++-- crates/engine/src/tests/import_photocraft.rs | 22 +++++++++++++++++++ crates/media/src/pcraft.rs | 7 ++++++ docs/photocraft-import.md | 4 +++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/interchange/pcraft_import.rs b/crates/engine/src/interchange/pcraft_import.rs index a7b24ff62..681409f1f 100644 --- a/crates/engine/src/interchange/pcraft_import.rs +++ b/crates/engine/src/interchange/pcraft_import.rs @@ -47,8 +47,10 @@ pub(crate) fn read(bytes: &[u8]) -> Result { if width == 0 || height == 0 || width > effectcraft_project::limits::MAX_SIZE || height > effectcraft_project::limits::MAX_SIZE { return Err(format!("PhotoCraft canvas {width} × {height} is outside the composition size limits")); } - if u64::from(width) * u64::from(height) > 16 << 20 { - return Err("PhotoCraft layer decoding exceeds 16777216 canvas pixels; import as Footage for the saved composite".into()); + if u64::from(width) * u64::from(height) > 12 << 20 { + return Err( + "PhotoCraft layer decoding exceeds the 1 GiB working-buffer budget (12582912 canvas pixels); import as Footage for the saved composite".into() + ); } if matches!(canvas.mode.as_str(), "Cmyk" | "Lab") { return Err(format!("PhotoCraft {} layer pixels are not supported; import as Footage for the saved composite", canvas.mode)); diff --git a/crates/engine/src/tests/import_photocraft.rs b/crates/engine/src/tests/import_photocraft.rs index 1298c346f..e9b7af55c 100644 --- a/crates/engine/src/tests/import_photocraft.rs +++ b/crates/engine/src/tests/import_photocraft.rs @@ -235,3 +235,25 @@ fn photocraft_large_files_are_rejected_before_reading() { std::fs::remove_file(path).unwrap(); } } + +#[test] +fn photocraft_layer_buffers_are_bounded_before_decoding_pixels() { + use effectcraft_project::{AlphaMode, Footage, SourceLayer}; + // An invalid source sample proves the budget is checked before the pixel reader. + let mut raster = layer("Large masked layer", true, 1.0, "Normal", json!({"kind":"raster","surface":surface("invalid-pixel-sample")})); + raster["mask"] = json!({"enabled":true,"density":1.0,"feather":4.0, + "surface":{"format":{"mode":"Grayscale","sample":"U8","alpha":false},"default":"ff","tiles":[]}}); + let path = fixture_bytes("mask-budget.pcraft", bundle_sized(json!([raster]), (4097, 3072))); + let f = Footage { + path: path.clone(), + width: 4097, + height: 3072, + has_video: true, + alpha: AlphaMode::Straight, + layer: Some(SourceLayer { index: 0, name: "Large masked layer".into(), layer_size: false, embedded: None, placed: false }), + ..Default::default() + }; + let error = effectcraft_media::MediaPool::new().frame_at(&f, effectcraft_time::Tick::ZERO).unwrap_err().to_string(); + assert!(error.contains("1 GiB working-buffer budget") && error.contains("Footage"), "{error}"); + std::fs::remove_file(path).unwrap(); +} diff --git a/crates/media/src/pcraft.rs b/crates/media/src/pcraft.rs index fb7587c31..220fcd381 100644 --- a/crates/media/src/pcraft.rs +++ b/crates/media/src/pcraft.rs @@ -9,6 +9,10 @@ use crate::{MediaError, Result}; /// Keep the decoded float frame and its conversion buffers within the still-image budget. const MAX_PIXELS: u64 = 16 << 20; +// Conservatively count source RGBA (16 B), raw mask samples (4 B), mask image +// (16 B), two blur images (32 B) and output (16 B), even where lifetimes do not +// overlap. Twelve Mi pixels fit within a 1 GiB working-buffer budget (84 B/pixel). +const MAX_LAYER_PIXELS: u64 = 12 << 20; pub(crate) const MAX_INPUT_BYTES: u64 = 512 << 20; pub(crate) fn sniff(path: &str, bytes: &[u8]) -> bool { @@ -113,6 +117,9 @@ pub(crate) fn decode(path: &str, bytes: &[u8], footage: &Footage, op: AlphaOp) - let outline = craft_doc::outline(bytes, &craft_doc::Limits::default()).map_err(|e| error(path, e))?; let canvas = outline.canvas.ok_or_else(|| error(path, "PhotoCraft document has no canvas"))?; check_size(path, canvas.width, canvas.height)?; + if u64::from(canvas.width) * u64::from(canvas.height) > MAX_LAYER_PIXELS { + return Err(error(path, "PhotoCraft layer exceeds the 1 GiB working-buffer budget; import as Footage for the saved composite")); + } let indexes = layer_path(&outline.layers, layer.index).ok_or_else(|| error(path, format!("no PhotoCraft layer {}", layer.index)))?; let pixels = craft_doc::pcraft_layer_rgba(bytes, &indexes, &craft_doc::Limits::default()) .map_err(|e| error(path, e))? diff --git a/docs/photocraft-import.md b/docs/photocraft-import.md index 8c4d709e3..47cdf5a8c 100644 --- a/docs/photocraft-import.md +++ b/docs/photocraft-import.md @@ -20,6 +20,8 @@ Use `"importAs":"composition"` for layers. Import uses the existing still-footag ## Limits -PhotoCraft input is limited to 512 MiB. Whole-file ZIP reads share that limit, checked from the archive signature before allocating the input, so renaming a bundle does not bypass it. Decoded composites and layer canvases are limited to 16,777,216 pixels. Composition import also respects EffectCraft's composition size limit, allows up to 4,096 layers and 100 levels of group nesting, and rejects unsupported CMYK or Lab layer data. Footage can still import a supported saved composite when the document's layers exceed those limits. +PhotoCraft input is limited to 512 MiB. Whole-file ZIP reads share that limit, checked from the archive signature before allocating the input, so renaming a bundle does not bypass it. Decoded composites are limited to 16,777,216 pixels and layer canvases to 12,582,912 pixels. Composition import also respects EffectCraft's composition size limit, allows up to 4,096 layers and 100 levels of group nesting, and rejects unsupported CMYK or Lab layer data. Footage can still import a supported saved composite when the document's layers exceed those limits. Unsupported layer kinds are skipped with a message naming the layer. A nonempty document with no supported pixel layers cannot import as a composition; the error recommends Footage. Invalid input reports an error before adding a composition. + +Layer decoding has a separate limit of 12,582,912 canvas pixels, checked before reading source samples. A conservative budget counts the source, raw mask, mask image, both feathering intermediates and output together, within 1 GiB of working buffers. Larger canvases report an error recommending Footage before decoding layer pixels. This includes masked and unmasked layers; saved composites retain the 16,777,216-pixel limit.