From 740b1e3f7e66f88cf2d185bc1098d01ea322c104 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 13:57:26 -0800 Subject: [PATCH] Tests: fuzz PSD slices in versions 6, 7 and 8 import_fuzz builds PSD files from scratch whose image resource 1050 holds slices in version 6 (a version 16 descriptor after the first record) and in the version 16 descriptors of versions 7 and 8. Each sample has one user slice, one slice that follows a layer and one generated automatically; the first two come in as user slices. - the_sliced_psd_samples_bring_in_their_user_slices: each sample opens with its two user slices and no warning, document.psdInfo counts 2, and an embedded place brings 2. - sliced_psd_samples_with_huge_counts_or_cut_short_come_in_without_their_slices: slice counts of 10,001, 0x7fffffff and u32::MAX, data one byte short, and data cut inside the count bring the art in without its slices and with a warning. - mutated_psd_slices_never_panic: mutated slice data, kept whole, cut short with the resource length rebuilt, or with the file cut in the resource block, never makes document.open, document.psdInfo or an embedded file.place report an internal error. tests_psdslices::damaged_slices_never_panic now fails when a command reports an internal error instead of discarding the results. --- .../engine/src/cmd/fileio/tests_psdslices.rs | 9 +- crates/engine/tests/import_fuzz.rs | 267 +++++++++++++++++- docs/target-app-parity.md | 1 + 3 files changed, 274 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/fileio/tests_psdslices.rs b/crates/engine/src/cmd/fileio/tests_psdslices.rs index 2d8d0c951f..85fa6ae7e9 100644 --- a/crates/engine/src/cmd/fileio/tests_psdslices.rs +++ b/crates/engine/src/cmd/fileio/tests_psdslices.rs @@ -501,8 +501,13 @@ fn damaged_slices_never_panic() { let mut f = base.clone(); f[at] = v; let mut s = Session::new(); - let _ = s.execute("document.open", &json!({"name": "poster.psd", "dataBase64": b64(&f)})); - let _ = s.execute("document.psdInfo", &json!({"dataBase64": b64(&f)})); + // The engine's guard reports a panic as an internal error. + for r in [ + s.execute("document.open", &json!({"name": "poster.psd", "dataBase64": b64(&f)})), + s.execute("document.psdInfo", &json!({"dataBase64": b64(&f)})), + ] { + assert!(!matches!(r, Err(crate::EngineError::Internal { .. })), "byte {at} set to {v}: {r:?}"); + } } } } diff --git a/crates/engine/tests/import_fuzz.rs b/crates/engine/tests/import_fuzz.rs index 8de519fa4e..5614aef56a 100644 --- a/crates/engine/tests/import_fuzz.rs +++ b/crates/engine/tests/import_fuzz.rs @@ -1,5 +1,5 @@ //! Untrusted files never crash the app: garbage, truncated, mutated and hostile SVG, PDF, DXF and HP-GL input, -//! mutated raster images (Photoshop documents and JPEG XL images among them) placed with File → Place, and swatch (`.vcswatches`, `.gpl`, `.ase`), graphic style (`.vcstyles`), flattener preset +//! mutated raster images (Photoshop documents and JPEG XL images among them) placed with File → Place, Photoshop documents' slices (damaged or cut short) opened and placed, and swatch (`.vcswatches`, `.gpl`, `.ase`), graphic style (`.vcstyles`), flattener preset //! (`.vcflattener`) and export preset (`.vcexportpresets`) libraries, shortcut sets and action sets other apps save as text (`.kys`, `.aia`), and native files (compressed, damaged, saved for older versions), //! must load as an error or as a document that then renders and exports, without a panic; nor may //! bitmaps, PDF and text pasted from other apps, nor EMF and WMF pictures (damaged files, records @@ -1939,6 +1939,271 @@ proptest! { } } +// ---------- PSD slices ---------- + +/// A Unicode string as a PSD file stores it: a count of UTF-16 units, then the units. +fn psd_unicode(s: &str) -> Vec { + let units: Vec = s.encode_utf16().collect(); + let mut b = (units.len() as u32).to_be_bytes().to_vec(); + b.extend(units.iter().flat_map(|u| u.to_be_bytes())); + b +} + +/// A descriptor key or class ID: length 0 and its four characters, or its length and its characters. +fn psd_key(k: &str) -> Vec { + let mut b = if k.len() == 4 { 0u32 } else { k.len() as u32 }.to_be_bytes().to_vec(); + b.extend(k.as_bytes()); + b +} + +/// A descriptor's class (an empty name and the ID `null`), then the count of its items. +fn psd_class(count: usize) -> Vec { + let mut b = psd_unicode(""); + b.extend(psd_key("null")); + b.extend((count as u32).to_be_bytes()); + b +} + +/// A descriptor of `items`, each a key and a typed value. +fn psd_descriptor(items: &[(&str, Vec)]) -> Vec { + let mut b = psd_class(items.len()); + for (k, v) in items { + b.extend(psd_key(k)); + b.extend(v); + } + b +} + +/// A typed descriptor value: its four-character type, then `body`. +fn psd_value(kind: &[u8; 4], body: &[u8]) -> Vec { + [kind.as_slice(), body].concat() +} + +fn psd_long(v: i32) -> Vec { + psd_value(b"long", &v.to_be_bytes()) +} + +fn psd_text(t: &str) -> Vec { + psd_value(b"TEXT", &psd_unicode(t)) +} + +fn psd_enum(kind: &str, v: &str) -> Vec { + psd_value(b"enum", &[psd_key(kind), psd_key(v)].concat()) +} + +/// A bounds object: left, top, right and bottom (px), stored top, left, bottom, right. +fn psd_bounds([l, t, r, b]: [i32; 4]) -> Vec { + psd_value(b"Objc", &psd_descriptor(&[("Top ", psd_long(t)), ("Left", psd_long(l)), ("Btom", psd_long(b)), ("Rght", psd_long(r))])) +} + +/// The slices of the samples: origin (0: generated automatically, 1: follows a layer, 2: made by +/// the user), name, and left, top, right and bottom on the 12 × 10 px canvas. Two come in; the +/// automatic one between them is left out. +const PSD_SLICES: [(u32, &str, [i32; 4]); 3] = [(2, "a", [1, 2, 5, 6]), (0, "auto", [0, 0, 12, 1]), (1, "b", [6, 3, 11, 9])]; + +/// The data of an image resource 1050 holding [`PSD_SLICES`]. +struct SliceSample { + version: u32, + data: Vec, + /// Where the slice count starts in `data`. + count_at: usize, +} + +/// Version 6: the bounds of all the slices, the group's name, the count and one record per slice. +/// The first record is followed by a Photoshop 7 descriptor (version 16) with a nested object, which +/// the reader reads past to the second record. +fn psd_slices_v6() -> SliceSample { + let mut b = 6u32.to_be_bytes().to_vec(); + b.extend([0i32, 0, 10, 12].iter().flat_map(|v| v.to_be_bytes())); + b.extend(psd_unicode("group")); + let count_at = b.len(); + b.extend((PSD_SLICES.len() as u32).to_be_bytes()); + for (i, (origin, name, rect)) in PSD_SLICES.iter().enumerate() { + // The ID, the group ID and the origin; a slice that follows a layer gives the layer's ID. + b.extend((i as u32 + 1).to_be_bytes()); + b.extend(0u32.to_be_bytes()); + b.extend(origin.to_be_bytes()); + if *origin == 1 { + b.extend(4u32.to_be_bytes()); + } + b.extend(psd_unicode(name)); + // An Image slice: its rectangle, link, target, message, alternative text, plain cell text, + // alignment and background color (alpha, red, green, blue). + b.extend(1u32.to_be_bytes()); + b.extend(rect.iter().flat_map(|v| v.to_be_bytes())); + for t in ["https://example.com/", "_blank", "", "alt"] { + b.extend(psd_unicode(t)); + } + b.push(0); + b.extend(psd_unicode("")); + b.extend([0u8; 8]); + b.extend([255, 10, 20, 30]); + if i == 0 { + b.extend(16u32.to_be_bytes()); + let color = psd_value(b"Objc", &psd_descriptor(&[("Rd ", psd_value(b"doub", &128f64.to_be_bytes()))])); + b.extend(psd_descriptor(&[("topOutset", psd_long(0)), ("bgColor", color)])); + } + } + SliceSample { version: 6, data: b, count_at } +} + +/// Versions 7 and 8: a version 16 descriptor with the slices' bounds, base name and `slices` list, +/// each slice an object with a nested bounds and background color object. +fn psd_slices_descriptor(version: u32) -> SliceSample { + let mut b = version.to_be_bytes().to_vec(); + b.extend(16u32.to_be_bytes()); + b.extend(psd_class(3)); + b.extend(psd_key("bounds")); + b.extend(psd_bounds([0, 0, 12, 10])); + b.extend(psd_key("baseName")); + b.extend(psd_text("sheet")); + b.extend(psd_key("slices")); + b.extend(b"VlLs"); + let count_at = b.len(); + b.extend((PSD_SLICES.len() as u32).to_be_bytes()); + for (i, (origin, name, rect)) in PSD_SLICES.iter().enumerate() { + let origin = match origin { + 0 => "autoGenerated", + 1 => "layerGenerated", + _ => "userGenerated", + }; + let doub = |v: f64| psd_value(b"doub", &v.to_be_bytes()); + let color = psd_value(b"Objc", &psd_descriptor(&[("Rd ", doub(10.0)), ("Grn ", doub(20.0)), ("Bl ", doub(30.0))])); + b.extend(psd_value( + b"Objc", + &psd_descriptor(&[ + ("sliceID", psd_long(i as i32 + 1)), + ("groupID", psd_long(0)), + ("origin", psd_enum("ESliceOrigin", origin)), + ("Type", psd_enum("ESliceType", "Img ")), + ("bounds", psd_bounds(*rect)), + ("Nm ", psd_text(name)), + ("url", psd_text("https://example.com/")), + ("bgColorType", psd_enum("ESliceBGColorType", "Clr ")), + ("bgColor", color), + ]), + )); + } + SliceSample { version, data: b, count_at } +} + +/// Resource 1050 in versions 6, 7 and 8. +fn psd_slice_samples() -> Vec { + vec![psd_slices_v6(), psd_slices_descriptor(7), psd_slices_descriptor(8)] +} + +/// A flat 12 × 10 px RGB PSD file whose one image resource is 1050 holding `data`, and where +/// `data` lies in it. +fn sliced_psd(data: &[u8]) -> (Vec, std::ops::Range) { + // The signature, version 1, reserved bytes, 3 channels, 10 rows, 12 columns, 8 bits, RGB, and + // no color mode data. + let mut f = b"8BPS\0\x01\0\0\0\0\0\0\0\x03".to_vec(); + f.extend(10u32.to_be_bytes()); + f.extend(12u32.to_be_bytes()); + f.extend([0, 8, 0, 3]); + f.extend([0; 4]); + let mut block = b"8BIM".to_vec(); + block.extend(1050u16.to_be_bytes()); + block.extend([0, 0]); + block.extend((data.len() as u32).to_be_bytes()); + let start = f.len() + 4 + block.len(); + block.extend(data); + block.resize(block.len().next_multiple_of(2), 0); + f.extend((block.len() as u32).to_be_bytes()); + f.extend(block); + // No layer and mask information, then the merged image, raw. + f.extend([0; 4]); + f.extend([0, 0]); + f.extend((0..360u32).map(|v| (v * 7) as u8)); + (f, start..start + data.len()) +} + +/// PSD file `f` opened (the names of the document's slices, and the open's warnings), counted by +/// `document.psdInfo`, and placed embedded in a new document (the count of the slices it brings in). +fn sliced_psd_imports(f: &[u8]) -> (Vec, Value, Value, usize) { + let mut s = vectorcraft_engine::Session::new(); + let p = json!({"name": "sheet.psd", "dataBase64": vectorcraft_format::base64_encode(f)}); + let r = s.execute("document.open", &p).unwrap(); + let names = s.doc().unwrap().doc.slices.iter().map(|x| x.options.name.clone()).collect(); + let info = s.execute("document.psdInfo", &p).unwrap(); + s.execute("file.new", &json!({})).unwrap(); + let placed = s.execute("file.place", &json!({"name": "sheet.psd", "dataBase64": vectorcraft_format::base64_encode(f), "link": false})).unwrap(); + (names, r["warnings"].clone(), info["slices"].clone(), placed["slices"].as_array().map_or(0, Vec::len)) +} + +/// Each sample reaches the slice reader: its two user slices open and place, without a warning, +/// and `document.psdInfo` counts them; the automatic one is left out. +#[test] +fn the_sliced_psd_samples_bring_in_their_user_slices() { + for sample in psd_slice_samples() { + let (names, warnings, info, placed) = sliced_psd_imports(&sliced_psd(&sample.data).0); + assert_eq!(names, ["a", "b"], "version {}: {warnings}", sample.version); + assert_eq!(warnings.as_array().map_or(0, Vec::len), 0, "version {}: {warnings}", sample.version); + assert_eq!((info, placed), (json!(2), 2), "version {}", sample.version); + } +} + +/// A slice count past the 10,000 read, or one larger than the slices stored, and resource data cut +/// short, open and place the file without its slices, with a warning, and `document.psdInfo` +/// counts none. +#[test] +fn sliced_psd_samples_with_huge_counts_or_cut_short_come_in_without_their_slices() { + for sample in psd_slice_samples() { + let mut damaged = vec![]; + for count in [10_001u32, 0x7fff_ffff, u32::MAX] { + let mut d = sample.data.clone(); + d[sample.count_at..sample.count_at + 4].copy_from_slice(&count.to_be_bytes()); + damaged.push((format!("count {count}"), d)); + } + damaged.push(("one byte short".into(), sample.data[..sample.data.len() - 1].to_vec())); + damaged.push(("cut in the count".into(), sample.data[..sample.count_at + 2].to_vec())); + for (what, d) in damaged { + let (names, warnings, info, placed) = sliced_psd_imports(&sliced_psd(&d).0); + let warned = warnings.as_array().is_some_and(|w| w.iter().any(|w| w.as_str().is_some_and(|w| w.contains("the slices can't be read")))); + assert!(warned, "version {}, {what}: {warnings}", sample.version); + assert_eq!((names.len(), info, placed), (0, json!(0), 0), "version {}, {what}", sample.version); + } + } +} + +proptest! { + #![proptest_config(config())] + + /// Mutated copies of the slice samples never crash opening the file, `document.psdInfo` or + /// placing it embedded: the resource's data mutated, then kept whole, cut short with the + /// resource's length saying so, or with the file cut short in the resource's block. + #[test] + fn mutated_psd_slices_never_panic(which in 0usize..3, mode in 0u8..3, cut in any::(), edits in prop::collection::vec((any::(), any::()), 0..12)) { + let SliceSample { version, mut data, .. } = psd_slice_samples().swap_remove(which); + let len = data.len(); + for (at, b) in edits { + data[at % len] = b; + } + let bytes = match mode { + 0 => sliced_psd(&data).0, + 1 => { + data.truncate(cut % (len + 1)); + sliced_psd(&data).0 + } + _ => { + let (mut f, at) = sliced_psd(&data); + f.truncate(at.start - 12 + cut % (len + 13)); + f + } + }; + let r = catch_quiet(|| { + let mut s = vectorcraft_engine::Session::new(); + let p = json!({"name": "sheet.psd", "dataBase64": vectorcraft_format::base64_encode(&bytes)}); + let _ = no_panic(s.execute("document.open", &p)); + let _ = no_panic(s.execute("document.psdInfo", &p)); + let _ = no_panic(s.execute("file.new", &json!({}))); + let p = json!({"name": "sheet.psd", "dataBase64": vectorcraft_format::base64_encode(&bytes), "link": false}); + let _ = no_panic(s.execute("file.place", &p)); + }); + prop_assert!(r.is_ok(), "version {version}, mode {mode}: panicked: {:?}", r.err()); + } +} + /// 1-bit TIFFs compressed with CCITT Group 3: modified Huffman (Compression 2) in three strips, T.4 /// (Compression 3) with two-dimensional rows, fill bits, six EOLs at the end and each byte's lowest /// bit first, and T.4 two-dimensional rows in 16 × 16 tiles. diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 21b0074bbd..e49c09ec20 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1899,6 +1899,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - File › Open of a PSD or PSB file with two or more layers, and File › Place of one without Link, show PSD Import Options: Convert Layers to Objects with Import Hidden Layers, or Flatten Layers to a Single Image, the options [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded. A file placed with Link shows its flat image and isn't asked about. `file.place` takes `layers` and `hiddenLayers`, and `document.psdInfo` counts a file's layers without opening it. VectorCraft's own rules: placed layers come in as one group named after the file, a layer as an image and a layer group as a group, at the place the merged image would take and clipped to the canvas when layers reach past it; the dialog asks from two layers up and starts from the choices made last. Layer comps aren't read (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). - A PSD or PSB file's clipping path, the saved path that image resource 2999 names in the [PSD file format specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/), clips its merged image on File › Place and on File › Open of the merged image, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file imported as one flattened image. The image comes in a clip group whose clipping path has no paint and uses the even-odd rule, and the path can be edited or released. `links.update` and `links.relink` keep the clip group, and files that name no clipping path come in as before (`crates/engine/src/cmd/fileio/tests_psdread.rs`). - Import Slices: File › Open of a PSD or PSB file with slices, and File › Place of one without Link, bring its slices in as user slices, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded; PSD Import Options offers Import Slices for such a file. `document.open` and `file.place` take `importSlices`, and `document.psdInfo` counts the slices. The slices are read from image resource 1050 in version 6 and in the descriptor of versions 7 and 8, as the [Photoshop File Formats Specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/) describes it. VectorCraft's own rules: Import Slices applies to Convert Layers to Objects and to Flatten Layers to a Single Image alike, and makes the dialog ask for a file with fewer than two layers; placed slices move and scale with the art in the same undo step; each slice keeps its name, type, link, target, message, alternative text, cell text and background color; slices the file generated automatically, and slices wholly outside the canvas, are left out; slices that can't be read are left out with a warning while the art comes in; with Show Import Options off, a PSD file is placed with the PSD Import Options chosen last, and on, each PSD file of the place cursor asks for them (`crates/engine/src/cmd/fileio/tests_psdslices.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). + The import fuzz tests open, count and place PSD files whose resource 1050 is in version 6 (with a version 16 descriptor after a record) and in the descriptors of versions 7 and 8, with their data mutated, cut short with the resource's length saying so, and cut short in the file; a slice count past 10,000 or past the slices stored, and data one byte short, bring the art in without its slices and with a warning. `tests_psdslices::damaged_slices_never_panic` now fails when a command reports an internal error (`import_fuzz::the_sliced_psd_samples_bring_in_their_user_slices`, `import_fuzz::sliced_psd_samples_with_huge_counts_or_cut_short_come_in_without_their_slices`, `import_fuzz::mutated_psd_slices_never_panic`). Scores and estimates unchanged. - PCX files (`.pcx`) open and place, from the ZSoft PCX Technical Reference Manual: 1 bit in the header palette's first two colors (black and white when the version has no palette or both colors are the same), 1 bit in two to four planes and 4 bits with up to 16 colors from the header's palette (the EGA colors for versions 0 and 3), 2 bits in the CGA colors that the header's CGA palette settings pick (background, color burst, palette and intensity), 8 bits with the 256-color palette after the image data (gray levels without one) and 24-bit color in three planes, run-length encoded. The header's HDpi and VDpi size the image. A file of another depth or plane count, one cut short, and one whose pixels as RGBA pass the pixel allowance are refused with a message before its pixels are decoded. Open and Place recognize a PCX file by its extension, and `clipboard.importImage` and Glass's Load Texture… by its header; a PCX file copied in a file manager pastes as a bitmap (`crates/engine/src/cmd/fileio/tests_pcxread.rs`). - Pixar picture files (`.pxr`) open, place and paste, from "The Format of Stored Pictures" in the Pixar Image Computer manuals: any set of the R, G, B and A channels in 8 or 12 bits, dumped or run-length encoded with all four packet kinds and block ends, in tiles of any size (pixels of border tiles past the picture are dropped). R alone is gray, a missing color channel is 0 and a missing A opaque; 12-bit samples are clamped to 0.0 to 1.0; color matted to black becomes straight color; a null tile is transparent when the picture has A and black otherwise. The offsets aren't read, and the image takes the default resolution. Version 2 headers read like version 0. A file cut short, one with a tile whose writing wasn't finished, one with header values or packets the format doesn't define, and one whose pixels as RGBA pass the pixel allowance are refused with a message. Every reader recognizes a Pixar file by its magic number. Tested on synthetic files only (`crates/engine/src/cmd/fileio/tests_pxrread.rs`). - JPEG 2000 JP2 and raw codestream images open and place from `.jpf`, `.jpx`, `.jp2`, `.j2k`, `.j2c` and `.jpc`. Each axis uses the valid display resolution (`resd`), else the valid capture resolution (`resc`), whichever box comes first ([ITU-T T.800](https://www.itu.int/rec/T-REC-T.800/en), I.5.3.7.2); an axis with none uses 72 ppi. Multiple codestreams, JPX composition or layers, and unsupported color spaces are refused with a message. The decoder checks image size before allocating (`crates/engine/src/cmd/fileio/tests_jp2read.rs`).