From a756832dd5606ec092521c79f7ded1d028b73314 Mon Sep 17 00:00:00 2001 From: Eric Kaden <608341+EricKaden@users.noreply.github.com> Date: Sat, 24 Oct 2026 14:00:13 -0400 Subject: [PATCH] M12: a signed file that can't be written leaves the preSign step pending, or takes it back --- crates/automation/src/signing.rs | 9 ++++++++- crates/automation/tests/it/automation.rs | 17 +++++++++++++++++ crates/ui-egui/src/sign_ui.rs | 6 +++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/crates/automation/src/signing.rs b/crates/automation/src/signing.rs index e7f6b3efee..1a6440fd54 100644 --- a/crates/automation/src/signing.rs +++ b/crates/automation/src/signing.rs @@ -307,7 +307,14 @@ impl Automation { return Err(e); } }; - write_atomic(&out, &signed)?; + if let Err(e) = write_atomic(&out, &signed) { + // Not saved, so not signed: what the preSign scripts did is taken back too. + if before { + let _ = self.session.undo(doc_id); + let _ = self.session.take_js_output(doc_id); + } + return Err(e); + } let path = out.to_string_lossy().into_owned(); self.session.mark_signed(doc_id, signed.clone(), Some(path.clone())).map_err(failed)?; // Then its postSign scripts; what they change is a change after signing, not saved yet. diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rs index 9d03c93e8e..c50d3d53c2 100644 --- a/crates/automation/tests/it/automation.rs +++ b/crates/automation/tests/it/automation.rs @@ -5804,6 +5804,23 @@ fn signing_an_xfa_box_runs_its_signing_scripts_through_tools() { "{unasked}" ); let doc = ok(&mut a, "doc_open", json!({ "path": "form.pdf" }))["doc"].as_u64().unwrap(); + // A signed file that can't be written (`out` is a folder): not signed, and what preSign did + // is taken back. + std::fs::create_dir_all(dir.join("taken")).unwrap(); + let failed = a.call( + "sign_document", + &json!({ "doc": doc, "id": "ada.p12", "password": "secret12", "field": name("sign"), "out": "taken", "run_form_scripts": true }), + ); + assert!(failed.is_err(), "{failed:?}"); + let now = ok(&mut a, "form_fields", json!({ "doc": doc })); + assert!( + now["fields"] + .as_array() + .unwrap() + .iter() + .any(|f| f["name"].as_str().is_some_and(|n| n == "when" || n.ends_with(".when[0]")) && f["value"].is_null()), + "{now}" + ); let r = ok( &mut a, "sign_document", diff --git a/crates/ui-egui/src/sign_ui.rs b/crates/ui-egui/src/sign_ui.rs index 9e3eb395ec..3414f4ce9b 100644 --- a/crates/ui-egui/src/sign_ui.rs +++ b/crates/ui-egui/src/sign_ui.rs @@ -520,7 +520,11 @@ impl PdfCraftApp { return Err(e.to_string()); } }; - crate::editing::write_atomically(&path.to_string_lossy(), signed.as_slice()).map_err(|e| format!("Could not save: {e}"))?; + if let Err(e) = crate::editing::write_atomically(&path.to_string_lossy(), signed.as_slice()) { + // Not saved, so not signed: the preSign step stays pending as well. + app.presign = pending; + return Err(format!("Could not save: {e}")); + } // Our own write is not a change on disk to reload (#431). #[cfg(not(target_arch = "wasm32"))] app.watch.known_now(doc_id, &path.to_string_lossy());