craft-docx: documentProtection without w:enforcement is enforced, as in Word, and survives a save (#1750)

This commit is contained in:
midasmatterhorn authored and GitHub committed 2026-11-16 21:31:25 -05:00
1 parent 31af9e1613
commit 93598bc018
2 files changed
+47 -1

No files matched your search

+4 -1
View File
@@ -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"));
+43
View File
@@ -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<u8> {
let mut z = zip::ZipWriter::new(Cursor::new(Vec::new()));
let parts: [(&str, String); 3] = [
("word/document.xml", format!(r#"<w:document {W_NS}><w:body><w:p><w:r><w:t>Locked</w:t></w:r></w:p></w:body></w:document>"#)),
(
"word/_rels/document.xml.rels",
format!(
r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="{RT}settings" Target="settings.xml"/></Relationships>"#
),
),
("word/settings.xml", format!(r#"<w:settings {W_NS}>{protection}</w:settings>"#)),
];
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#"<w:documentProtection w:edit="readOnly" w:cryptAlgorithmSid="4" w:cryptSpinCount="100000" w:hash="AAAA" w:salt="BBBB"/>"#;
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#"<w:documentProtection w:edit="readOnly" w:enforcement="0"/>"#), None).unwrap();
assert!(off.settings.protection.is_none());
}