From 687ded0284ce3d22a1aa0de4b6d3cf849bc4b406 Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:11:26 +0100 Subject: [PATCH 1/2] fix(automation): reject malformed RPC save parameters Reject non-object params and wrong-type path/format values before they can become an implicit save to the stored layered file. Preserve missing/null behavior. Fixes #15670. --- crates/automation/src/rpc.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs index 404891f61e..0a616e07b2 100644 --- a/crates/automation/src/rpc.rs +++ b/crates/automation/src/rpc.rs @@ -167,6 +167,15 @@ impl Headless { } "doc.new" => self.command_run("file.new", p), "doc.save" => { + // A malformed Save As request must not become an implicit save to the stored path. + if !p.is_object() { + return Err(bad("doc.save params must be an object")); + } + for key in ["path", "format"] { + if p.get(key).is_some_and(|value| !value.is_null() && !value.is_string()) { + return Err(bad(format!("`{key}` must be a string"))); + } + } let tga_bits = photocraft_engine::file_cmds::tga_bits_param(&p, "doc.save").map_err(|e| bad(e.to_string()))?; let pdf_spot_plates = photocraft_engine::file_cmds::pdf_spot_plates_param(&p, "doc.save").map_err(|e| bad(e.to_string()))?; let mut opts = photocraft_io::ExportOptions { From 8c106a42c7836bf1f8b10509ce6a8568d6c38ea2 Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:13:19 +0100 Subject: [PATCH 2/2] test(automation): preserve saved files after malformed RPC saves Check path/format types and non-object params against an edited .pcraft document; preserve stored bytes and document state on refusal, with valid implicit and explicit save controls. --- crates/automation/src/rpc/tests.rs | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/crates/automation/src/rpc/tests.rs b/crates/automation/src/rpc/tests.rs index 51f053b595..76af659221 100644 --- a/crates/automation/src/rpc/tests.rs +++ b/crates/automation/src/rpc/tests.rs @@ -1186,3 +1186,49 @@ fn doc_place_forwards_high_memory_approval() { drop(t); std::fs::remove_dir_all(root).unwrap(); } + +#[test] +fn doc_save_rejects_malformed_requests_without_writing_the_stored_path() { + let dir = std::env::temp_dir().join(format!("pc-rpc-save-path-{}", std::process::id())); + std::fs::create_dir(&dir).unwrap(); + let workspace = crate::AuthorizedWorkspace::new(Some(&dir), Some(&dir)).unwrap(); + let mut h = Headless::with_workspace(workspace); + h.handle("doc.new", json!({"width": 8, "height": 8})).unwrap(); + h.handle("doc.save", json!({"path": "saved.pcraft"})).unwrap(); + let path = dir.join("saved.pcraft"); + let original = std::fs::read(&path).unwrap(); + h.handle("engine.execute", json!({"command": "layer.new.layer", "params": {"name": "Unsaved"}})).unwrap(); + let unsaved = h.inspect(None).unwrap(); + + for field in ["path", "format"] { + for value in [json!(123), json!(false), json!([]), json!({})] { + let mut params = json!({}); + params[field] = value.clone(); + let error = h.handle("doc.save", params).unwrap_err().to_string(); + assert!(error.contains(field), "{field} {value}: {error}"); + assert_eq!(std::fs::read(&path).unwrap(), original, "invalid {field} {value} wrote the stored file"); + assert_eq!(h.inspect(None).unwrap(), unsaved, "invalid {field} {value} changed the document"); + } + } + for params in [json!(123), json!(false), json!([]), json!("saved.pcraft")] { + let error = h.handle("doc.save", params.clone()).unwrap_err().to_string(); + assert!(error.contains("object"), "{params}: {error}"); + assert_eq!(std::fs::read(&path).unwrap(), original, "invalid params {params} wrote the stored file"); + assert_eq!(h.inspect(None).unwrap(), unsaved, "invalid params {params} changed the document"); + } + + let mut previous = original; + for params in [json!({}), json!({"path": null}), json!({"format": null}), Value::Null] { + h.handle("engine.execute", json!({"command": "layer.new.layer"})).unwrap(); + let saved = h.handle("doc.save", params).unwrap(); + assert_eq!(saved["path"], "saved.pcraft"); + let bytes = std::fs::read(&path).unwrap(); + assert_ne!(bytes, previous, "an implicit save did not write the edited document"); + previous = bytes; + } + let saved = h.handle("doc.save", json!({"path": "copy.pcraft", "format": "pcraft"})).unwrap(); + assert_eq!(saved["path"], "copy.pcraft"); + assert!(dir.join("copy.pcraft").is_file()); + drop(h); + std::fs::remove_dir_all(dir).unwrap(); +}