PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Validate selection modification limits before allocating the canvas mask. (#4591)
Vikram Bhaskaran7c916d9ecdd8
diff --git a/crates/engine/src/selection_cmds.rs b/crates/engine/src/selection_cmds.rsindex 8efb8d7b9..74de286f0 100644--- a/crates/engine/src/selection_cmds.rs+++ b/crates/engine/src/selection_cmds.rs@@ -461,7 +461,12 @@ fn modify(s: &mut Session, p: &Value, op: &str) -> Result<Value> {// continue past the canvas. Border has no such option: its band always follows the canvas edge,// or a Select All would border to nothing.let at_bounds = op == "border" || b(p, "applyAtCanvasBounds", false);- let (area, m) = current_mask(&s.active().ok_or(EngineError::NoDocument)?.doc);+ let doc = &s.active().ok_or(EngineError::NoDocument)?.doc;+ let area = doc.bounds();+ if area.size().area() > MAX_MODIFY_PIXELS as u64 {+ return Err(EngineError::BadParams { cmd: format!("select.modify.{op}"), msg: "the selection is too large to modify".into() });+ }+ let (_, m) = current_mask(doc);let (w, h) = (area.width() as usize, area.height() as usize);// Feather reaches 3σ = 3 r (σ = r, as in Photoshop); the others reach r. A margin past that// keeps the canvas edge out of reach. A selection clear of the canvas edge needs none: both@@ -765,6 +770,28 @@ mod tests {assert!(err.to_string().contains("detectFaces"), "{err}");}+ #[test]+ fn modify_rejects_oversized_canvases_before_reading_the_mask() {+ let mut s = Session::new();+ let mut doc = photocraft_doc::Document::new(+ "large",+ photocraft_geom::Size::new(300_000, 300_000),+ photocraft_color::ColorMode::Rgb,+ photocraft_color::SampleType::U8,+ );+ let mut selection = photocraft_raster::Surface::new(photocraft_color::PixelFormat::GRAY8);+ selection.write_pixel(0, 0, &[1.0]);+ doc.selection = Some(selection);+ s.add_document(doc, None);+ let before = s.active().unwrap().doc.clone();+ for op in ["expand", "contract", "border", "smooth", "feather"] {+ let error = s.execute(&format!("select.modify.{op}"), json!({"radius": 1})).unwrap_err();+ assert!(error.to_string().contains("too large to modify"));+ assert_eq!(*s.active().unwrap().doc, *before);+ assert!(!s.active().unwrap().history.can_undo());+ }+ }+#[test]fn color_range_replace_is_bounded_by_existing_selection() {for params in [json!({"color": "#ff0000", "fuzziness": 0}), json!({"select": "reds"})] {