From db1030835a4ad61bf28c5606d42464875a30e838 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Fri, 20 Nov 2026 17:16:45 -0800 Subject: [PATCH 1/2] docx: exercise Flat OPC element limits with small inputs --- crates/docx/src/flat.rs | 29 ++++++++++++++++++++++++++++- crates/docx/tests/it/word_xml.rs | 2 -- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/crates/docx/src/flat.rs b/crates/docx/src/flat.rs index b88211805f..10e5a5fd30 100644 --- a/crates/docx/src/flat.rs +++ b/crates/docx/src/flat.rs @@ -116,6 +116,10 @@ fn room(total: u64) -> u64 { /// Unpack a Flat OPC file into its parts, with a `[Content_Types].xml` made from the parts' /// content types (unless the file carries one). pub(crate) fn unpack(bytes: &[u8]) -> Result { + unpack_within(bytes, MAX_PART_ELEMENTS) +} + +fn unpack_within(bytes: &[u8], part_elements: usize) -> Result { let text = utf8_text(bytes); let src: &[u8] = text.as_ref(); let mut r = NsReader::from_reader(src); @@ -150,7 +154,7 @@ pub(crate) fn unpack(bytes: &[u8]) -> Result { } } elements += 1; - if elements > MAX_PART_ELEMENTS { + if elements > part_elements { return Err(DocxError::Limit("too many XML elements".into())); } let local = e.local_name().as_ref().to_vec(); @@ -552,6 +556,29 @@ mod tests { assert_eq!(out.matches("xmlns:p").count(), 256); } + #[test] + fn part_element_budget_counts_parts_that_are_not_read() { + assert_eq!(MAX_PART_ELEMENTS, 8_000_002); + let cap = 100; + let inline = |children| format!("{}", "".repeat(children)); + let part = |name: &str, xml: &str| { + format!("{xml}") + }; + let package = |parts: &str| format!("{parts}"); + // The part, xmlData and r wrappers count too: 97 children make exactly 100. + let xml = inline(97); + let first = part("/word/unused.xml", &xml); + let at_cap = unpack_within(package(&first).as_bytes(), cap).unwrap(); + assert_eq!(at_cap.get("word/unused.xml"), Some(xml.as_bytes())); + let over = part("/word/unused.xml", &inline(98)); + assert!(matches!(unpack_within(package(&over).as_bytes(), cap), Err(DocxError::Limit(_)))); + // Each package child has its own allowance, even though neither part is read. + let two = format!("{first}{}", part("/word/also-unused.xml", &xml)); + let reset = unpack_within(package(&two).as_bytes(), cap).unwrap(); + assert_eq!(reset.get("word/unused.xml"), Some(xml.as_bytes())); + assert_eq!(reset.get("word/also-unused.xml"), Some(xml.as_bytes())); + } + #[test] fn nesting_is_capped_where_the_xml_tree_reader_stops() { let inline = |levels: usize| format!("{}{}", "".repeat(levels), "".repeat(levels)); diff --git a/crates/docx/tests/it/word_xml.rs b/crates/docx/tests/it/word_xml.rs index 31c022aa0e..b3ba75395a 100644 --- a/crates/docx/tests/it/word_xml.rs +++ b/crates/docx/tests/it/word_xml.rs @@ -267,8 +267,6 @@ fn flat_opc_limits_count_what_is_not_kept() { // One part name over and over, or other elements, past the part count. assert!(limited(&r#"AA=="#.repeat(20_000))); assert!(limited(&"".repeat(20_000))); - // A part nothing reads, one element over what the XML tree reader takes in a part (8 000 000). - assert!(limited(&format!(r#"{}"#, "".repeat(8_000_000)))); } /// A Word 2003 XML Document saved with Windows line ends: XML reads CR LF as one line feed (XML From b479547629d0eff68ed9e7ab39ed9c27bbfa9729 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Fri, 20 Nov 2026 17:19:03 -0800 Subject: [PATCH 2/2] style(docx): format Flat OPC cap fixture --- crates/docx/src/flat.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/docx/src/flat.rs b/crates/docx/src/flat.rs index 10e5a5fd30..5fd20ecb23 100644 --- a/crates/docx/src/flat.rs +++ b/crates/docx/src/flat.rs @@ -561,9 +561,7 @@ mod tests { assert_eq!(MAX_PART_ELEMENTS, 8_000_002); let cap = 100; let inline = |children| format!("{}", "".repeat(children)); - let part = |name: &str, xml: &str| { - format!("{xml}") - }; + let part = |name: &str, xml: &str| format!("{xml}"); let package = |parts: &str| format!("{parts}"); // The part, xmlData and r wrappers count too: 97 children make exactly 100. let xml = inline(97);