diff --git a/crates/docx/src/read/meta.rs b/crates/docx/src/read/meta.rs index 8cf4c99dd..b9debe1fa 100644 --- a/crates/docx/src/read/meta.rs +++ b/crates/docx/src/read/meta.rs @@ -345,7 +345,10 @@ pub(crate) const PROTECTION_HASH_ATTRS: [&str; 16] = [ /// kept; a hostile attribute value can't be longer than a hash needs. fn read_protection(k: &El, s: &mut craft_wordmodel::Settings) { let on = |v: &str| !matches!(v, "0" | "false" | "off"); - if !k.attr("w:enforcement").is_some_and(on) { + // Word enforces protection when `w:enforcement` is missing ([MS-OI29500] §2.1.402 (c)), the + // opposite of the standard's literal reading, so only an explicit off value unlocks the + // document; a file whose generator omits the attribute keeps its restriction and its hash. + if k.attr("w:enforcement").is_some_and(|v| !on(v)) { return; } let edit = k.attr("w:edit").filter(|e| matches!(*e, "readOnly" | "comments" | "trackedChanges" | "forms")); diff --git a/crates/docx/tests/protection.rs b/crates/docx/tests/protection.rs new file mode 100644 index 000000000..ff43f3b92 --- /dev/null +++ b/crates/docx/tests/protection.rs @@ -0,0 +1,43 @@ +//! `w:documentProtection` (ECMA-376 §17.15.1.29): Word enforces protection when +//! `w:enforcement` is missing ([MS-OI29500] §2.1.402 c), so the reader must keep the +//! restriction — and, on save, its hash — rather than unlocking the document. Synthetic +//! fixture written for this test (the test carried over from WordCraft's reader, which +//! craft-docx replaced). + +use std::io::{Cursor, Write}; + +const W_NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships""#; +const RT: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/"; + +fn fixture(protection: &str) -> Vec { + let mut z = zip::ZipWriter::new(Cursor::new(Vec::new())); + let parts: [(&str, String); 3] = [ + ("word/document.xml", format!(r#"Locked"#)), + ( + "word/_rels/document.xml.rels", + format!( + r#""# + ), + ), + ("word/settings.xml", format!(r#"{protection}"#)), + ]; + for (name, text) in parts { + z.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); + z.write_all(text.as_bytes()).unwrap(); + } + z.finish().unwrap().into_inner() +} + +#[test] +fn protection_without_enforcement_is_enforced_like_word() { + let no_enforcement = r#""#; + let read = craft_docx::read_with_password(&fixture(no_enforcement), None).unwrap(); + assert_eq!(read.settings.protection.as_deref(), Some("readOnly"), "Word enforces protection with no w:enforcement"); + assert!(read.settings.protect_hash.iter().any(|(k, _)| k == "w:hash"), "the password hash is kept"); + let again = craft_docx::read_with_password(&craft_docx::write(&read).unwrap(), None).unwrap(); + assert_eq!(again.settings.protection.as_deref(), Some("readOnly"), "saving keeps the protection"); + assert!(again.settings.protect_hash.iter().any(|(k, _)| k == "w:hash"), "saving keeps the hash"); + // Explicitly off stays off (ignored but persisted, as the standard says). + let off = craft_docx::read_with_password(&fixture(r#""#), None).unwrap(); + assert!(off.settings.protection.is_none()); +}