diff --git a/crates/docx/src/lib.rs b/crates/docx/src/lib.rs index c3c3e8df3..68d41ab4d 100644 --- a/crates/docx/src/lib.rs +++ b/crates/docx/src/lib.rs @@ -3,8 +3,8 @@ //! [`read`] turns a `.docx` package into a [`craft_wordmodel::Document`]; [`write`] does the //! reverse. Both are written from the public specification. The reader is lenient (unknown //! markup is skipped, bad numbers fall back to defaults) and bounded (zip entry sizes, total -//! decompressed size, XML depth and element counts are capped), so hostile input yields an -//! error or a best-effort document, never a panic. +//! decompressed size, XML depth, element counts per part and a package-wide element budget are +//! capped), so hostile input yields an error or a best-effort document, never a panic. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] // The code came from an app whose lints allow this. #![allow(clippy::type_complexity)] diff --git a/crates/docx/src/package.rs b/crates/docx/src/package.rs index 3c7f45d94..6f90f56ba 100644 --- a/crates/docx/src/package.rs +++ b/crates/docx/src/package.rs @@ -1,5 +1,6 @@ //! OPC package access: bounded zip reading, relationships, part names. +use std::cell::Cell; use std::collections::{BTreeMap, HashMap}; use std::sync::Arc; @@ -216,6 +217,14 @@ fn opc_error(e: craft_opc::Error) -> DocxError { /// document keeps as it is (a picture, an embedded object) is handed over rather than copied. pub struct Package { opc: craft_opc::Package, + /// Elements this package's XML parts may still be parsed into. Every XML part read through + /// [`Package::xml`] charges it, so a file of many parts each inside the per-part cap builds + /// bounded tree memory in total — a chart part with no series builds a maximal tree for + /// almost no graphic work, and the trees the reader keeps accumulate. + budget: Cell, + /// What [`Package::reset_budget`] restores: the cap the package was opened with (the default, + /// or a smaller one from [`Package::open_limited_within`]). + budget_cap: usize, } impl Package { @@ -227,8 +236,16 @@ impl Package { /// `max_total` (counting entries that fail their checksum), is an error. A damaged entry is /// left out with a logged warning. pub fn open_limited(bytes: &[u8], max_part: u64, max_total: u64) -> Result { + Package::open_limited_within(bytes, max_part, max_total, xml::MAX_ELEMENTS_TOTAL) + } + + /// [`open_limited`] with an XML element budget of its own (nested documents share their + /// container's, tests use a small one). + pub fn open_limited_within(bytes: &[u8], max_part: u64, max_total: u64, max_elements: usize) -> Result { let limits = craft_opc::Limits::new(MAX_ENTRIES, max_part, max_total); - craft_opc::Package::open(bytes, &limits).map(|opc| Package { opc }).map_err(opc_error) + craft_opc::Package::open(bytes, &limits) + .map(|opc| Package { opc, budget: Cell::new(max_elements), budget_cap: max_elements }) + .map_err(opc_error) } /// The bytes of all its parts. @@ -244,7 +261,27 @@ impl Package { /// A package from parts already in memory (a Flat OPC file, a converted document), by part /// name without the leading `/`. pub fn from_parts(files: BTreeMap>) -> Package { - Package { opc: craft_opc::Package::from_parts(files) } + Package { opc: craft_opc::Package::from_parts(files), budget: Cell::new(xml::MAX_ELEMENTS_TOTAL), budget_cap: xml::MAX_ELEMENTS_TOTAL } + } + + /// Start a read's element budget over: the budget is per read, not per package's lifetime. + pub(crate) fn reset_budget(&self) { + self.budget.set(self.budget_cap); + } + + /// The XML elements left of the package's budget: nested documents open within it. + pub(crate) fn elements_left(&self) -> usize { + self.budget.get() + } + + /// Charge `count` elements a parsed part built against the budget. + pub(crate) fn charge(&self, count: usize) -> Result<(), DocxError> { + let left = self.budget.get(); + if count > left { + return Err(DocxError::Limit(format!("{count} XML elements, {left} left of the package's {} budget", self.budget_cap))); + } + self.budget.set(left - count); + Ok(()) } /// Part bytes by name (leading `/` ignored; case-insensitive). @@ -261,14 +298,28 @@ impl Package { /// [`Package::xml`] for the main document part: malformed XML is an error, not a truncated read. pub fn xml_strict(&self, name: &str) -> Result, DocxError> { match self.get(name) { - Some(b) => xml::parse_strict(b).map(Some), + Some(b) => { + // The parser stops at the remaining budget or the per-part cap, whichever is less: + // a part over what is left of the package's budget is refused before the whole + // per-part tree is built. + let (el, count) = xml::parse_strict_counted(b, self.budget.get().min(xml::MAX_ELEMENTS))?; + self.charge(count)?; + Ok(Some(el)) + } None => Ok(None), } } + /// A package part as a parsed XML tree, charged against the element budget: the parser + /// stops at what is left of the budget or the per-part cap, whichever is less, and the + /// count it built is charged after. A lenient read: malformed XML is a truncated one. pub fn xml(&self, name: &str) -> Result, DocxError> { match self.get(name) { - Some(b) => xml::parse(b).map(Some), + Some(b) => { + let (el, count) = xml::parse_counted(b, self.budget.get().min(xml::MAX_ELEMENTS))?; + self.charge(count)?; + Ok(Some(el)) + } None => Ok(None), } } @@ -448,6 +499,82 @@ mod tests { let junk = EmbeddedManifest::parse(b"R\tnope\trId1\tk\tt\tI\nP\tonly-two\n\t\t\n\xff\xfe"); assert!(junk.parts.is_empty()); } + + /// A file of many parts each inside the per-part cap is bounded in total: every XML part + /// read through the package charges a package-wide budget, and a part over it is skipped + /// (warned), never built. A chart part with no series builds a maximal tree for almost no + /// graphic work, so the trees a reader keeps would otherwise accumulate one per part. + #[test] + fn the_xml_element_budget_is_package_wide() { + let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; + let rels = r#""#; + let document = format!(r#""#); + let dense = |root: &str| format!(r#"<{root} {ns}>{}"#, "".repeat(900)); + let z = zip_of(&[ + ("word/document.xml", document.into_bytes()), + ("word/_rels/document.xml.rels", rels.as_bytes().to_vec()), + ("word/styles.xml", dense("w:styles").into_bytes()), + ("word/numbering.xml", dense("w:numbering").into_bytes()), + ]); + let pkg = Package::open_limited_within(&z, MAX_PART, MAX_TOTAL, 1_000).unwrap(); + // styles (900 elements) reads; numbering (900 more) is over what is left and is skipped + // with a warning, not built. + let read = || crate::read::read_opened(&pkg, None); + let (first, warned) = crate::test_log::warnings(read); + assert!(first.is_ok(), "{:?}", first.err()); + let refused: Vec<_> = warned.iter().filter(|w| w.contains("numbering") && w.contains("XML element")).collect(); + assert_eq!(refused.len(), 1, "{warned:?}"); + // The budget is per read: a second read starts it over and behaves the same. + let (second, warned) = crate::test_log::warnings(read); + assert!(second.is_ok(), "{:?}", second.err()); + assert_eq!(warned.iter().filter(|w| w.contains("numbering") && w.contains("XML element")).count(), 1, "{warned:?}"); + } + + /// The main document part itself is strict: over the budget it is an error, not a + /// best-effort read of a tree the file then paid for. + #[test] + fn a_main_part_over_the_element_budget_is_refused() { + let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; + let document = format!(r#"{}"#, "".repeat(20)); + let z = zip_of(&[("word/document.xml", document.into_bytes())]); + let pkg = Package::open_limited_within(&z, MAX_PART, MAX_TOTAL, 10).unwrap(); + assert!(matches!(crate::read::read_opened(&pkg, None), Err(DocxError::Limit(_)))); + } + + /// A part over what is left of the package's budget is refused while parsing, not after the + /// whole per-part tree is built: the parser stops at the remaining budget or the per-part + /// cap, whichever is less, so the file pays for at most what is left of the budget. + #[test] + fn a_part_over_the_remaining_budget_is_refused_while_parsing() { + let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; + let document = format!(r#"{}"#, "".repeat(200)); + let z = zip_of(&[("word/document.xml", document.into_bytes())]); + // 200 elements: inside the per-part cap, over the package's 100 budget. The parse + // refuses at the budget, so the error is the parse's, not the charge's, and names + // the effective limit for this parse (the remaining budget), not the whole budget. + let pkg = Package::open_limited_within(&z, MAX_PART, MAX_TOTAL, 100).unwrap(); + let e = crate::read::read_opened(&pkg, None).expect_err("refused"); + assert_eq!(e, DocxError::Limit("over the XML element budget for this parse: at most 100 elements".into())); + } + + /// A part parsed earlier shrinks what is left of the budget: the next part is refused while + /// parsing at the remaining, not at the whole budget, and the error names the effective limit + /// for this parse (the remaining), not the package's budget. + #[test] + fn an_earlier_part_reduces_the_remaining_budget_the_next_part_names_it() { + let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; + let styles = format!(r#"{}"#, "".repeat(60)); + let settings = format!(r#"{}"#, "".repeat(60)); + let z = zip_of(&[("word/styles.xml", styles.into_bytes()), ("word/settings.xml", settings.into_bytes())]); + // 100 elements of budget. The styles part parses (the main-part path: `xml_strict`) and + // charges its 61 elements, leaving 39. + let pkg = Package::open_limited_within(&z, MAX_PART, MAX_TOTAL, 100).unwrap(); + pkg.xml_strict("word/styles.xml").unwrap(); + // The settings part needs 61 elements, over the 39 left and under the per-part cap: the + // parse refuses at the remaining, so the error names 39, not the package's 100 budget. + let e = pkg.xml("word/settings.xml").expect_err("refused"); + assert_eq!(e, DocxError::Limit("over the XML element budget for this parse: at most 39 elements".into())); + } } #[cfg(test)] diff --git a/crates/docx/src/read.rs b/crates/docx/src/read.rs index 746225e97..18c9ea47a 100644 --- a/crates/docx/src/read.rs +++ b/crates/docx/src/read.rs @@ -154,9 +154,11 @@ pub(crate) fn read_opened(pkg: &Package, chunks: Option) -> Resul read_opened_in(pkg, ChunkState::new(chunks)).map(|(doc, ..)| doc) } -/// [`read_opened`] with what chunks may still bring in; also gives the chunks, chunk bytes and -/// graphic work left of that. -fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize, u64, usize), DocxError> { +/// [`read_opened`] with what chunks may still bring in; also gives the chunks, chunk bytes, +/// graphic work and XML elements left of that. +fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize, u64, usize, usize), DocxError> { + // The budget is per read, so a package read twice (tests, a Flat OPC conversion) starts over. + pkg.reset_budget(); let root_rels = pkg.rels(""); let main = root_rels.by_type(rt::OFFICE_DOC).filter(|r| !r.external).map(|r| r.target.clone()).unwrap_or_else(|| "word/document.xml".to_string()); let Some(root) = pkg.xml_strict(&main)? else { @@ -371,11 +373,11 @@ fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize, if r.shape_ids { keep_glued_ids(&mut r.doc, &r.glued); } - let (left, bytes, graphic) = (r.chunks.left, r.chunks.bytes, r.chunks.graphic); + let (left, bytes, graphic, elements) = (r.chunks.left, r.chunks.bytes, r.chunks.graphic, pkg.elements_left()); let mut doc = r.doc; doc.skipped_parts.extend(pkg.skipped().iter().cloned()); doc.ensure_nonempty(); - Ok((doc, left, bytes, graphic)) + Ok((doc, left, bytes, graphic, elements)) } /// Most move ranges open at once, and most move names tracked, per file. diff --git a/crates/docx/src/read/chunk.rs b/crates/docx/src/read/chunk.rs index 2dc1674a7..9bdde064d 100644 --- a/crates/docx/src/read/chunk.rs +++ b/crates/docx/src/read/chunk.rs @@ -110,11 +110,17 @@ impl Reader<'_> { log::warn!("docx: alternative format chunks nest deeper than {MAX_CHUNK_NESTING}; the inner ones are left out"); return None; } - let pkg = - Package::open_limited(bytes, MAX_PART, self.chunks.bytes).map_err(|e| log::warn!("docx: unreadable nested document chunk: {e}")).ok()?; + // The nested document shares the file's element budget: open_limited_within, not a + // fresh cap — nesting goes deep, so 5 packages each getting 16M is the OOM class the + // budget exists for. The container's own parse spent part of the chain, so the cap is + // the lesser of the chain and what the container has left. + let cap = self.chunks.elements.min(self.pkg.elements_left()); + let pkg = Package::open_limited_within(bytes, MAX_PART, self.chunks.bytes, cap) + .map_err(|e| log::warn!("docx: unreadable nested document chunk: {e}")) + .ok()?; self.chunks.bytes = self.chunks.bytes.saturating_sub(pkg.size()); let state = ChunkState { nesting: self.chunks.nesting + 1, depth: depth + 1, ..self.chunks }; - let (doc, left, bytes, graphic) = match super::read_opened_in(&pkg, state) { + let (doc, left, bytes, graphic, elements) = match super::read_opened_in(&pkg, state) { Ok(read) => read, Err(e) => { log::warn!("docx: unreadable nested document chunk: {e}"); @@ -125,6 +131,16 @@ impl Reader<'_> { self.chunks.left = left; self.chunks.bytes = bytes; self.chunks.graphic = graphic; + self.chunks.elements = elements; + // The nested document's XML usage comes out of the container's budget too: a later + // sibling chunk and the container's own remaining parts parse against what is left + // of it. The charge can't refuse: the cap is the lesser of the file's remaining + // elements and what the container has left, and the container's budget doesn't move + // while the nested read goes. + let spent = cap.saturating_sub(elements); + if let Err(e) = self.pkg.charge(spent) { + log::warn!("docx: nested document chunk outspent the container's element budget: {e}"); + } Some(doc) } } @@ -139,6 +155,9 @@ pub(crate) struct ChunkState<'a> { /// Graphic work left in the file's budget ([`super::MAX_GRAPHIC_WORK`]): nested documents' /// charts and diagrams count against it too. pub graphic: usize, + /// XML elements left in the file's budget: nested packages share their container's + /// ([`crate::xml::MAX_ELEMENTS_TOTAL`]), not one of their own. + pub elements: usize, /// How deep in nested WordprocessingML chunks this reader is. pub nesting: u8, /// The XML depth its stories start at (a nested document's: that of the chunk naming it). @@ -148,7 +167,15 @@ pub(crate) struct ChunkState<'a> { impl<'a> ChunkState<'a> { pub fn new(import: Option>) -> Self { - ChunkState { left: MAX_CHUNKS, bytes: MAX_TOTAL, graphic: super::MAX_GRAPHIC_WORK, nesting: 0, depth: 0, import } + ChunkState { + left: MAX_CHUNKS, + bytes: MAX_TOTAL, + graphic: super::MAX_GRAPHIC_WORK, + elements: crate::xml::MAX_ELEMENTS_TOTAL, + nesting: 0, + depth: 0, + import, + } } } @@ -198,8 +225,8 @@ mod tests { let outer = package(r#""#, ("rIdC", "aFChunk", "afchunk.docx"), ("word/afchunk.docx", &inner)); let pkg = Package::open(&outer).unwrap(); let read = |graphic| { - let (d, .., left) = super::super::read_opened_in(&pkg, ChunkState { graphic, ..ChunkState::new(None) }).unwrap(); - (drawn(&d), left) + let (d, .., work, _) = super::super::read_opened_in(&pkg, ChunkState { graphic, ..ChunkState::new(None) }).unwrap(); + (drawn(&d), work) }; let (shown, left) = read(super::super::MAX_GRAPHIC_WORK); assert!(shown); @@ -207,4 +234,130 @@ mod tests { // With the file's budget spent, the nested document doesn't get one of its own. assert_eq!(read(0), (false, 0)); } + + /// A package whose body is `body`, with the document relationships `rels` (id, type, target) + /// and the parts `(name, bytes)` they name. + fn package_parts(body: &str, rels: &[(&str, &str, &str)], parts: &[(&str, &[u8])]) -> Vec { + let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main""#; + let rels_xml = + |inner: &str| format!(r#"{inner}"#); + let rel = |(id, ty, target): &(&str, &str, &str)| { + format!(r#""#) + }; + let names: String = rels.iter().map(rel).collect::>().join(""); + let mut files: Vec<(&str, Vec)> = vec![ + ("_rels/.rels", rels_xml(&rel(&("rId1", "officeDocument", "word/document.xml"))).into_bytes()), + ("word/document.xml", format!(r#"{body}"#).into_bytes()), + ("word/_rels/document.xml.rels", rels_xml(&names).into_bytes()), + ]; + files.extend(parts.iter().map(|(name, bytes)| (*name, bytes.to_vec()))); + let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + for (name, bytes) in files { + zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); + zw.write_all(&bytes).unwrap(); + } + zw.finish().unwrap().into_inner() + } + + /// Whether the body holds a paragraph with `s`. + fn has(d: &Document, s: &str) -> bool { + d.body.iter().any(|b| b.as_para().map(|p| p.text.contains(s)).unwrap_or(false)) + } + + #[test] + fn a_nested_documents_elements_come_out_of_the_files_budget() { + // Three levels: the outer's altChunk names the inner, whose altChunk names the + // innermost. The chain spends down: the inner's own parse charges the file's budget, + // so the innermost opens with what the inner has left, not a fresh cap. + let innermost = package(r#"innermost"#, ("rIdZ", "chart", "charts/z.xml"), ("word/charts/z.xml", b"")); + let inner = package( + r#"inner"#, + ("rIdD", "aFChunk", "afchunk.docx"), + ("word/afchunk.docx", &innermost), + ); + let outer = package(r#""#, ("rIdC", "aFChunk", "afchunk.docx"), ("word/afchunk.docx", &inner)); + let pkg = Package::open(&outer).unwrap(); + let read = |elements| { + let (d, ..) = super::super::read_opened_in(&pkg, ChunkState { elements, ..ChunkState::new(None) }).unwrap(); + (has(&d, "inner"), has(&d, "innermost")) + }; + // With room, both levels flow in. + assert_eq!(read(crate::xml::MAX_ELEMENTS_TOTAL), (true, true)); + // The inner's own parse (its rels, then its main part) charged the file's budget: the + // innermost is left out instead of getting one of its own (before this, each nested + // package started with the full 16M). + assert_eq!(read(10), (true, false)); + } + + #[test] + fn a_nested_documents_elements_charge_the_container() { + // The nested document's XML usage comes out of the container's budget too: the cap + // the nested package was given, minus its returned remainder, is charged, so the + // container's remaining parts parse against what is left of the shared cap — not a + // budget that ignores the chunk. + let nested = package(r#"inner"#, ("rIdZ", "chart", "charts/z.xml"), ("word/charts/z.xml", b"")); + let body = r#"after"#; + let with = package(&format!(r#"{body}"#), ("rIdC", "aFChunk", "afchunk.docx"), ("word/afchunk.docx", &nested)); + let without = package(body, ("rIdZ", "chart", "charts/z.xml"), ("word/charts/z.xml", b"")); + let read = |outer: &[u8]| { + let pkg = Package::open(outer).unwrap(); + let (d, ..) = super::super::read_opened_in(&pkg, ChunkState { elements: 10, ..ChunkState::new(None) }).unwrap(); + (has(&d, "inner"), pkg.elements_left()) + }; + let (inner_in, charged) = read(&with); + assert!(inner_in); + let (_, base) = read(&without); + assert!(charged < base, "the nested document's usage is the container's: {charged} of {base}"); + } + + #[test] + fn a_grandchilds_reservation_charges_every_level() { + // Three levels: the innermost's reservation comes out of the inner's budget, and the + // inner's out of the outer's — the chain charges every container, so the outer's + // remainder is smaller with the deeper chain than with a shallow one. + let innermost = package(r#"leaf"#, ("rIdZ", "chart", "charts/z.xml"), ("word/charts/z.xml", b"")); + let inner = package( + r#"mid"#, + ("rIdD", "aFChunk", "afchunk.docx"), + ("word/afchunk.docx", &innermost), + ); + let outer = package(r#""#, ("rIdC", "aFChunk", "afchunk.docx"), ("word/afchunk.docx", &inner)); + let pkg = Package::open(&outer).unwrap(); + let (d, .., elements) = super::super::read_opened_in(&pkg, ChunkState::new(None)).unwrap(); + assert!(has(&d, "mid"), "the inner document didn't flow in"); + assert!(has(&d, "leaf"), "the innermost document didn't flow in"); + // The same container with a shallow chain: one reservation instead of two. + let shallow = package(r#""#, ("rIdC", "aFChunk", "afchunk.docx"), ("word/afchunk.docx", &innermost)); + let pkg = Package::open(&shallow).unwrap(); + let (.., shallow_elements) = super::super::read_opened_in(&pkg, ChunkState::new(None)).unwrap(); + assert!(elements < shallow_elements, "every level's reservation is charged: {elements} of {shallow_elements}"); + } + + #[test] + fn a_charged_budget_bounds_the_containers_part_and_the_sibling_after_it() { + // The container's budget pays for the parts it parses (the section's header, read + // during the chunk's own walk) and for the chunks after the first one: with a budget + // that fits the grandchild chunk and the header but not the sibling document's XML, + // the sibling is left out; skip the header's charge and it would flow in. + let hdr = r#"headheadhead"#; + let gc = package(r#"gc"#, ("rIdZ", "chart", "charts/z.xml"), ("word/charts/z.xml", b"")); + let sibling = + package(&r#"sibling"#.repeat(8), ("rIdZ", "chart", "charts/z.xml"), ("word/charts/z.xml", b"")); + let inner = package_parts( + r#"mid"#, + &[("rIdD", "aFChunk", "afchunk.docx"), ("rIdH", "header", "header1.xml"), ("rIdS", "aFChunk", "afchunk2.docx")], + &[("word/afchunk.docx", &gc), ("word/header1.xml", hdr.as_bytes()), ("word/afchunk2.docx", &sibling)], + ); + let outer = package(r#""#, ("rIdC", "aFChunk", "afchunk.docx"), ("word/afchunk.docx", &inner)); + let pkg = Package::open(&outer).unwrap(); + let read = |elements| { + let (d, ..) = super::super::read_opened_in(&pkg, ChunkState { elements, ..ChunkState::new(None) }).unwrap(); + (has(&d, "mid"), has(&d, "gc"), has(&d, "sibling")) + }; + // With room, everything flows in. + assert_eq!(read(crate::xml::MAX_ELEMENTS_TOTAL), (true, true, true)); + // The grandchild chunk and the header fit the file's budget; the sibling document's + // XML (8 paragraphs) doesn't, so it's left out instead of getting a fresh cap. + assert_eq!(read(40), (true, true, false)); + } } diff --git a/crates/docx/src/xml.rs b/crates/docx/src/xml.rs index fa50d217a..46da6911d 100644 --- a/crates/docx/src/xml.rs +++ b/crates/docx/src/xml.rs @@ -10,6 +10,10 @@ use crate::DocxError; pub const MAX_DEPTH: usize = 256; /// Maximum number of elements in one part. pub const MAX_ELEMENTS: usize = 8_000_000; +/// Maximum number of elements summed over every part a read parses: a file of many parts each +/// inside [`MAX_ELEMENTS`] would otherwise cost a tree per part, and the trees a reader keeps +/// (the main part's, charts' and diagrams') accumulate. +pub const MAX_ELEMENTS_TOTAL: usize = MAX_ELEMENTS * 2; /// Maximum number of attributes on one element (#3702). pub const MAX_ATTRS: usize = 1024; @@ -329,19 +333,31 @@ pub fn parse(bytes: &[u8]) -> Result { parse_ns(bytes, &[]) } +/// [`parse`], with the number of elements it built, stopping early at `max` elements: the caller +/// charges the count against a package-wide budget (see [`MAX_ELEMENTS_TOTAL`]), and a part over +/// what is left of it is refused before the whole per-part tree is built. +pub fn parse_counted(bytes: &[u8], max: usize) -> Result<(El, usize), DocxError> { + parse_with(bytes, &[], false, max) +} + +/// [`parse_counted`], strict, for a part whose loss would be silent data loss (the main +/// document): a mismatched end tag or an element left open at the end of the data is an error, +/// not a cue to stop reading. +pub fn parse_strict_counted(bytes: &[u8], max: usize) -> Result<(El, usize), DocxError> { + parse_with(bytes, &[], true, max) +} + /// [`parse`] with `extra` namespaces (prefix, URI) mapped to canonical prefixes first, so a /// related vocabulary (WordprocessingML 2003) reads with the prefixes the reader knows. pub fn parse_ns(bytes: &[u8], extra: &[(&'static str, &str)]) -> Result { - parse_with(bytes, extra, false) + parse_with(bytes, extra, false, MAX_ELEMENTS).map(|(e, _)| e) } -/// [`parse`] for a part whose loss would be silent data loss (the main document): a mismatched end -/// tag or an element left open at the end of the data is an error, not a cue to stop reading. -pub fn parse_strict(bytes: &[u8]) -> Result { - parse_with(bytes, &[], true) -} - -fn parse_with(bytes: &[u8], extra: &[(&'static str, &str)], strict: bool) -> Result { +/// The XML parser everything else goes through: transcodes UTF-16, walks the events and builds +/// the tree depth-first, stopping at `max` elements and counting the ones it built. A mismatched +/// end tag or an element left open at the end of the data is an error when `strict`, a cue to +/// stop reading when not. +fn parse_with(bytes: &[u8], extra: &[(&'static str, &str)], strict: bool, max: usize) -> Result<(El, usize), DocxError> { let namespaces: Vec<(&str, &str)> = extra.iter().copied().chain(NAMESPACES.iter().copied()).chain(OTHER_NAMESPACES.iter().copied()).chain(STRICT.iter().copied()).collect(); // The prefix of an element: its namespace's, with two exceptions for Strict documents @@ -375,7 +391,9 @@ fn parse_with(bytes: &[u8], extra: &[(&'static str, &str)], strict: bool) -> Res max_depth: MAX_DEPTH + 1, // Text stretches count as nodes too: two for each element. max_nodes: 2 * MAX_ELEMENTS, - max_elements: MAX_ELEMENTS, + // The parse stops here before the whole tree is built: a part over what is left of the + // package's element budget pays for at most that, not for the per-part cap. + max_elements: max, max_attrs: MAX_ATTRS, max_attrs_total: 2 * MAX_ELEMENTS, }; @@ -383,17 +401,27 @@ fn parse_with(bytes: &[u8], extra: &[(&'static str, &str)], strict: bool) -> Res if strict { options = options.strict(); } - let root = options.parse(bytes).map_err(|e| if e.is_limit() { DocxError::Limit(e.to_string()) } else { DocxError::Xml(e.to_string()) })?; - Ok(from_tree(root)) + let root = options.parse(bytes).map_err(|e| match &e { + craft_xml::Error::TooManyElements(_) => DocxError::Limit(format!("over the XML element budget for this parse: at most {max} elements")), + e if e.is_limit() => DocxError::Limit(e.to_string()), + e => DocxError::Xml(e.to_string()), + })?; + Ok(from_tree_counted(root)) } -/// The `craft-xml` tree as this module's (whose shape the reader was written against). +/// The `craft-xml` tree as this module's (whose shape the reader was written against), with the +/// number of elements the subtree holds (the caller charges it against a package-wide budget). // shortcut: one copy of each parsed part; read craft_xml::El directly when the reader is ported to it. -fn from_tree(e: craft_xml::El) -> El { +fn from_tree_counted(e: craft_xml::El) -> (El, usize) { let mut kids = Vec::with_capacity(e.children.len()); + let mut count = 1usize; for n in e.children { match n { - craft_xml::Node::El(c) => kids.push(Node::El(from_tree(c))), + craft_xml::Node::El(c) => { + let (kid, built) = from_tree_counted(c); + count += built; + kids.push(Node::El(kid)); + } craft_xml::Node::Text(t) | craft_xml::Node::CData(t) => match kids.last_mut() { Some(Node::Text(prev)) => prev.push_str(&t), _ => kids.push(Node::Text(t)), @@ -401,7 +429,7 @@ fn from_tree(e: craft_xml::El) -> El { craft_xml::Node::Pi { .. } | craft_xml::Node::Comment(_) => {} } } - El { name: e.name, attrs: e.attrs, kids } + (El { name: e.name, attrs: e.attrs, kids }, count) } /// This module's tree as `craft-xml`'s, for the shared readers (`craft-drawingml`, `craft-math`) @@ -515,12 +543,13 @@ mod tests { fn strict_parsing_rejects_a_dropped_entity_and_trailing_markup() { let bad = [&b"before &unknown; after"[..], b"", b""]; for xml in bad { - assert!(parse_strict(xml).is_err(), "{}", String::from_utf8_lossy(xml)); + assert!(parse_strict_counted(xml, MAX_ELEMENTS).is_err(), "{}", String::from_utf8_lossy(xml)); } assert!( - parse_strict( + parse_strict_counted( b"x & y -" +", + MAX_ELEMENTS ) .is_ok() ); diff --git a/crates/docx/tests/it/element_budget.rs b/crates/docx/tests/it/element_budget.rs new file mode 100644 index 000000000..6b220ae7c --- /dev/null +++ b/crates/docx/tests/it/element_budget.rs @@ -0,0 +1,44 @@ +//! The package-wide XML element budget: a file of many parts each inside the per-part +//! cap reads bounded, where every part used to build its own tree and the trees the +//! reader keeps accumulated. + +use crate::fixtures; +use craft_docx::read; + +const W_NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships""#; + +#[test] +fn a_file_of_element_budget_parts_still_reads() { + // Nine chart parts of 2M elements each: 18M over the package's 16M budget. The later + // parts are skipped with a warning instead of built, and the file still reads. Before + // the budget, every part's whole tree was parsed and the chart trees stayed cached for + // the whole read (~230 MB per 2M-element part, measured), so a file like this cost one + // tree per part and scales linearly with the part count. + let parts = 9usize; + let chunk = "".repeat(2_000_000); + let chart_xml = format!(r#"{chunk}"#); + let mut document = format!( + r#""# + ); + for i in 0..parts { + document.push_str(&format!( + r#""# + )); + } + document.push_str(""); + let mut rels = String::from(r#""#); + for i in 0..parts { + rels.push_str(&format!( + r#""# + )); + } + rels.push_str(""); + let mut entries: Vec<(&str, Vec)> = vec![("word/document.xml", document.into_bytes()), ("word/_rels/document.xml.rels", rels.into_bytes())]; + for i in 0..parts { + entries.push((Box::leak(format!("word/charts/chart{i}.xml").into_boxed_str()), chart_xml.clone().into_bytes())); + } + let refs: Vec<(&str, &[u8])> = entries.iter().map(|(n, b)| (*n, b.as_slice())).collect(); + let z = fixtures::zip(&refs); + let doc = read(&z).expect("a file over the element budget still reads, the extra parts skipped"); + assert_eq!(doc.body.len(), parts, "the document's own paragraphs read"); +} diff --git a/crates/docx/tests/it/main.rs b/crates/docx/tests/it/main.rs index 926cb5154..942d946ae 100644 --- a/crates/docx/tests/it/main.rs +++ b/crates/docx/tests/it/main.rs @@ -16,6 +16,7 @@ mod default_size; mod drawing_payload; mod east_asian_lang; mod east_asian_layout; +mod element_budget; mod emphasis_marks; mod fixtures; mod font_table;