PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Integration: a typed Password-field value stays in the open document after Save (#3943)
aionggb04257d5eb73
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 9f169171a..4f8309a0d 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -4456,8 +4456,11 @@ impl Session {// An encrypted file needs the password it is protected with now (the owner// password when known, so saving never downgrades this session's rights).let keys = editor.keys.clone();+ // The saved file carries no Password-field values (#3943); the open document keeps them.+ let typed = pdfcraft_forms::password_values(&editor.cos);editor.cos =pdfcraft_cos::Document::open_with_password(bytes.clone(), keys.reopen.as_deref()).map_err(|e| EditError::Reopen(e.to_string()))?;+ pdfcraft_forms::restore_password_values(&mut editor.cos, &typed).map_err(|e| EditError::Reopen(e.to_string()))?;editor.saved = bytes;editor.saved_keys = keys;editor.unsaved_file = false;diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rsindex c90ab8fcd..63eff4a5f 100644--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@ -2178,6 +2178,22 @@ fn password_field_values_are_kept_in_memory_but_written_to_no_file() {assert!(t.get(reopened).unwrap().form.iter().find(|f| f.name == "name").unwrap().value.iter().all(|v| v.is_empty()));}+/// #3943 review: recording a save rebases editing on the saved file, which carries no Password+/// value; the typed value stays in the open document all the same, and a later save still omits it.+#[test]+fn a_typed_password_value_survives_save_and_mark_saved() {+ let fixture = String::from_utf8(form_fixture()).unwrap().replace("/T (name)", "/T (name) /Ff 8192");+ let mut s = Session::new().with_clock(|| 1_700_000_000);+ let id = s.open("form.pdf", None, Arc::new(fixture.into_bytes()), None).unwrap();+ s.apply(id, Edit::SetFieldValue { name: "name".into(), value: FieldValue::Text("hunter2".into()) }).unwrap();+ let saved = s.save_bytes(id).unwrap();+ s.mark_saved(id, saved, Some("saved.pdf".into())).unwrap();+ let value = |s: &Session| s.get(id).unwrap().form.iter().find(|f| f.name == "name").unwrap().value.clone();+ assert_eq!(value(&s), ["hunter2"], "the open document keeps the value after Save");+ assert!(!s.get(id).unwrap().dirty, "restoring it is not an unsaved change");+ assert!(!appears_in_pdf(&s.save_bytes(id).unwrap(), "hunter2"), "the next save still omits it");+}+/// #3926 / #3943 review: signing writes the signed file from the same copy a save does, so a/// typed Password-field value is not signed into the file either.#[test]diff --git a/crates/forms/src/lib.rs b/crates/forms/src/lib.rsindex 17d67e473..75c3dbee0 100644--- a/crates/forms/src/lib.rs+++ b/crates/forms/src/lib.rs@@ -1345,6 +1345,24 @@ pub fn has_password_values(doc: &Document) -> bool {!password_fields(doc).is_empty()}+/// The typed values of the text fields with the Password flag, as (name, text) pairs, for+/// [`restore_password_values`] after the document was rebased on a file written without them.+pub fn password_values(doc: &Document) -> Vec<(String, String)> {+ password_fields(doc).into_iter().filter_map(|f| f.value.first().map(|v| (f.name.clone(), v.clone()))).collect()+}++/// Put values from [`password_values`] back into the open document, in memory only. A field+/// that is gone or read-only keeps what it has.+pub fn restore_password_values(doc: &mut Document, values: &[(String, String)]) -> Result<(), FormError> {+ for (name, v) in values {+ match set_value(doc, name, &FieldValue::Text(v.clone())) {+ Ok(()) | Err(FormError::NoSuchField(_) | FormError::ReadOnly(_)) => {}+ Err(e) => return Err(e),+ }+ }+ Ok(())+}+/// `r` and its field ancestors (`/Parent`), nearest first, at most 32 deep and without repeats.fn field_chain(doc: &Document, r: ObjRef) -> Vec<ObjRef> {let mut chain = vec![r];