From 9e7fab103882cd1c3b8274389d1b858f11f1ad71 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 21:05:30 +0000 Subject: [PATCH 01/11] PPTX import: budget preserved theme XML, SmartArt drawing shapes and diagram definitions across the presentation --- crates/pptx/src/read.rs | 12 ++++++++++++ crates/pptx/src/read/shapes.rs | 34 ++++++++++++++++++++++++++++------ crates/pptx/src/read/theme.rs | 11 ++++++++++- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/crates/pptx/src/read.rs b/crates/pptx/src/read.rs index 288e497f8..958058be1 100644 --- a/crates/pptx/src/read.rs +++ b/crates/pptx/src/read.rs @@ -46,6 +46,12 @@ pub struct Imp<'a> { pub tag_bytes: usize, /// The tag budget ran out and the warning was given. pub tags_capped: bool, + /// Bytes of preserved theme XML kept by the masters read so far. + pub theme_extra_bytes: usize, + /// Shapes read from SmartArt drawings so far (see `shapes::MAX_SMARTART_SHAPES`). + pub smartart_shapes: usize, + /// Bytes of SmartArt layout, style and colour definitions copied into diagrams so far. + pub diagram_xml_bytes: usize, } impl Imp<'_> { @@ -446,6 +452,9 @@ pub fn import_theme(bytes: &[u8]) -> Result<(deckcraft_model::theme::Theme, Vec< tags_by_part: HashMap::new(), tag_bytes: 0, tags_capped: false, + theme_extra_bytes: 0, + smartart_shapes: 0, + diagram_xml_bytes: 0, }; let part = Part::new(&pkg, &theme_path, &doc); let theme = theme::read_theme(&mut imp, &part, &doc.root); @@ -487,6 +496,9 @@ pub fn import_checked(bytes: &[u8]) -> Result<(Presentation, Vec), PptxE tags_by_part: HashMap::new(), tag_bytes: 0, tags_capped: false, + theme_extra_bytes: 0, + smartart_shapes: 0, + diagram_xml_bytes: 0, }; // Slide ids first so hyperlinks anywhere can point at slides. let slide_ids = register_slides(&mut imp, &docs, &slide_entries, &mut lost_slides); diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index b3e3d48e6..142dcdb1b 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -558,9 +558,20 @@ fn diagram_data(imp: &mut Imp, target: &str) -> Option Some(id) } +/// Shapes all SmartArt drawings of one presentation may add; a frame read after that is left as a +/// placeholder. Each frame that references a drawing reads it again. +const MAX_SMARTART_SHAPES: usize = 250_000; + +/// SmartArt definition XML (layout, style, colours) all diagrams of one presentation may copy. +const MAX_DIAGRAM_XML_BYTES: usize = 32 << 20; + +fn count_shapes(shapes: &[Shape]) -> usize { + shapes.iter().map(|s| 1 + count_shapes(s.children())).sum() +} + /// SmartArt: read the pre-drawn shapes of its drawing part as a group. fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> Option { - if depth > 8 { + if depth > 8 || imp.smartart_shapes >= MAX_SMARTART_SHAPES { return None; } let rel_ids = gd.child("relIds")?; @@ -576,6 +587,8 @@ fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> if children.is_empty() { return None; } + // A drawing that frames reuse (or that nests another SmartArt) counts every time it is read. + imp.smartart_shapes = imp.smartart_shapes.saturating_add(count_shapes(&children)); let children = smartart_text_rects(imp, &mut sub, tree, children, depth + 1); let mut g = frame.clone(); let x = frame.xfrm.unwrap_or_default(); @@ -703,15 +716,22 @@ fn text_area(geom: &Geom, w: f64, h: f64) -> deckcraft_geom::Rect { /// The outline of a SmartArt graphic (for the Text Pane), with its layout, quick style and colour /// parts kept to be written back. -fn diagram_outline(imp: &Imp, part: &Part, rel_ids: &El, data: &El) -> Option { +fn diagram_outline(imp: &mut Imp, part: &Part, rel_ids: &El, data: &El) -> Option { let nodes = crate::dgm::outline(data); if nodes.is_empty() { return None; } + let (pkg, spent) = (imp.pkg, std::cell::Cell::new(imp.diagram_xml_bytes)); let raw = |attr: &str| -> Option { let rel = part.rels.get(rel_ids.attr(attr)?)?; - let bytes = imp.pkg.get(&rel.target)?; - (bytes.len() <= 4 << 20).then(|| String::from_utf8(bytes.to_vec()).ok()).flatten() + let bytes = pkg.get(&rel.target)?; + // Every diagram copies its definitions, so the copies share one presentation-wide budget. + if bytes.len() > 4 << 20 || spent.get().saturating_add(bytes.len()) > MAX_DIAGRAM_XML_BYTES { + return None; + } + let xml = String::from_utf8(bytes.to_vec()).ok()?; + spent.set(spent.get() + xml.len()); + Some(xml) }; let layout_xml = raw("r:lo"); let (layout_id, cat) = layout_xml @@ -732,6 +752,8 @@ fn diagram_outline(imp: &Imp, part: &Part, rel_ids: &El, data: &El) -> Option Option) -> FontSet { FontSet { latin: tf("latin"), ea: tf("ea"), cs: tf("cs"), scripts } } +/// Bytes of preserved theme XML (object defaults and extra colour schemes) all masters of one +/// presentation may keep. +const MAX_THEME_EXTRA_BYTES: usize = 16 << 20; + pub fn read_theme(imp: &mut Imp, part: &Part, root: &El) -> Theme { let mut t = Theme { name: root.attr("name").unwrap_or("Theme").to_string(), ..Default::default() }; let Some(te) = root.child("themeElements") else { return t }; @@ -89,8 +93,13 @@ pub fn read_theme(imp: &mut Imp, part: &Part, root: &El) -> Theme { .filter(|e| !super::has_rel_refs(e)) .map(|e| part.keep(e)) .collect(); - if !extra.is_empty() { + // Every master that references this theme keeps its own copy of the preserved XML, so the + // copies share one budget for the presentation. + if !extra.is_empty() && imp.theme_extra_bytes.saturating_add(extra.len()) <= MAX_THEME_EXTRA_BYTES { + imp.theme_extra_bytes += extra.len(); t.raw_extra = Some(extra); + } else if !extra.is_empty() { + log::warn!("pptx: theme object defaults not kept; the preserved-XML budget is spent"); } t } From 953ae8d060d7486cbd10632db2a4a3c54eeefe84 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Wed, 4 Nov 2026 23:44:51 +0000 Subject: [PATCH 02/11] Apply review fixes - crates/pptx/src/read/shapes.rs --- crates/pptx/src/read/shapes.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index b2f058819..c2e9293fc 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -584,12 +584,13 @@ fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> let dpart = Part::new(imp.pkg, &drawing_rel, &d); let tree = d.root.child("spTree")?; let mut sub = IdCtx { fresh: true, ..Default::default() }; + let before = imp.smartart_shapes; let children = sp_tree(imp, &dpart, &mut sub, tree, depth + 1); if children.is_empty() { return None; } - // A drawing that frames reuse (or that nests another SmartArt) counts every time it is read. - imp.smartart_shapes = imp.smartart_shapes.saturating_add(count_shapes(&children)); + // Counted once here, nested SmartArt included: a drawing that frames reuse counts every time it is read. + imp.smartart_shapes = before.saturating_add(count_shapes(&children)); let children = smartart_text_rects(imp, &mut sub, tree, children, depth + 1); let mut g = frame.clone(); let x = frame.xfrm.unwrap_or_default(); From 8ecb818f8156d2c83b27d7d1654263b10cd84287 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 10 Nov 2026 22:21:47 +0000 Subject: [PATCH 03/11] address review: layout id follows generated layout, charge text-rect shapes to smartart budget --- crates/pptx/src/read/shapes.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index c2e9293fc..860adeec6 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -679,10 +679,11 @@ fn text_on_rect(imp: &mut Imp, ctx: &mut IdCtx, s: &mut Shape, t: Xfrm) -> Optio return None; } } - if ctx.count >= MAX_SHAPES { + if ctx.count >= MAX_SHAPES || imp.smartart_shapes >= MAX_SMARTART_SHAPES { return None; } ctx.count += 1; + imp.smartart_shapes += 1; let text = s.text.take(); // No fill, outline or effects; the style keeps only the text colour and font. let style = s.style.clone().map(|mut st| { @@ -762,7 +763,8 @@ fn diagram_outline(imp: &mut Imp, part: &Part, rel_ids: &El, data: &El) -> Optio style, colors, imported: true, - layout_id: id, + // Without the layout part the writer generates one for `kind`; empty keeps data's loTypeId in step with it. + layout_id: if layout_xml.is_some() { id } else { String::new() }, layout_xml, style_xml, colors_xml, From 40052f4b09c60d987236b9a43e422dd5b4d4c15d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 02:07:46 +0000 Subject: [PATCH 04/11] address review: budget diagram definition parts copied by the opaque fallback, refuse a SmartArt drawing over the remaining shape budget --- crates/pptx/src/read/shapes.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index 860adeec6..9e0aa3e9b 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -480,6 +480,14 @@ fn frame(imp: &mut Imp, part: &Part, ctx: &mut IdCtx, e: &El, depth: usize) -> S return None; } let (kind, target) = (rel.ty.clone(), rel.target.clone()); + // A diagram's definition parts copied here count against the same presentation-wide budget. + if rel.kind.starts_with("diagram") && !imp.media_by_part.contains_key(&crate::opc::norm(&target)) { + let len = pkg.get(&target).map_or(0, |b| b.len()); + if len > MAX_DIAGRAM_PART_BYTES || imp.diagram_xml_bytes.saturating_add(len) > MAX_DIAGRAM_XML_BYTES { + return None; + } + imp.diagram_xml_bytes += len; + } let media = if rel.kind == "diagramData" { diagram_data(imp, &target) } else { imp.media_for(&target) }; Some((id, kind, media?)) }) @@ -566,6 +574,9 @@ const MAX_SMARTART_SHAPES: usize = 250_000; /// SmartArt definition XML (layout, style, colours) all diagrams of one presentation may copy. const MAX_DIAGRAM_XML_BYTES: usize = 32 << 20; +/// The largest single diagram definition part copied. +const MAX_DIAGRAM_PART_BYTES: usize = 4 << 20; + fn count_shapes(shapes: &[Shape]) -> usize { shapes.iter().map(|s| 1 + count_shapes(s.children())).sum() } @@ -590,7 +601,13 @@ fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> return None; } // Counted once here, nested SmartArt included: a drawing that frames reuse counts every time it is read. - imp.smartart_shapes = before.saturating_add(count_shapes(&children)); + let total = before.saturating_add(count_shapes(&children)); + if total > MAX_SMARTART_SHAPES { + // Over the presentation's remaining budget: a placeholder, and nothing charged, nested SmartArt included. + imp.smartart_shapes = before; + return None; + } + imp.smartart_shapes = total; let children = smartart_text_rects(imp, &mut sub, tree, children, depth + 1); let mut g = frame.clone(); let x = frame.xfrm.unwrap_or_default(); @@ -729,7 +746,7 @@ fn diagram_outline(imp: &mut Imp, part: &Part, rel_ids: &El, data: &El) -> Optio let rel = part.rels.get(rel_ids.attr(attr)?)?; let bytes = pkg.get(&rel.target)?; // Every diagram copies its definitions, so the copies share one presentation-wide budget. - if bytes.len() > 4 << 20 || spent.get().saturating_add(bytes.len()) > MAX_DIAGRAM_XML_BYTES { + if bytes.len() > MAX_DIAGRAM_PART_BYTES || spent.get().saturating_add(bytes.len()) > MAX_DIAGRAM_XML_BYTES { return None; } let xml = String::from_utf8(bytes.to_vec()).ok()?; From 96c6b5da4cf7b9d4bac4ce9421c30b42353b2296 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 10:20:58 +0000 Subject: [PATCH 05/11] address review: refund the diagram XML budget when a fallback or a parent drawing is dropped --- crates/pptx/src/read/shapes.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index 9e0aa3e9b..4e3b54398 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -471,6 +471,7 @@ fn frame(imp: &mut Imp, part: &Part, ctx: &mut IdCtx, e: &El, depth: usize) -> S }; // Keep the parts it points to so a save writes it back whole (#203). A part with relationships // of its own, or an external link, can't be carried: the save then falls back to the preview. + let xml_before = imp.diagram_xml_bytes; let parts = super::rel_refs(e) .into_iter() .map(|id| { @@ -491,8 +492,12 @@ fn frame(imp: &mut Imp, part: &Part, ctx: &mut IdCtx, e: &El, depth: usize) -> S let media = if rel.kind == "diagramData" { diagram_data(imp, &target) } else { imp.media_for(&target) }; Some((id, kind, media?)) }) - .collect::>>() - .unwrap_or_default(); + .collect::>>(); + // A fallback that drops its parts does not keep their bytes charged. + if parts.is_none() { + imp.diagram_xml_bytes = xml_before; + } + let parts = parts.unwrap_or_default(); s.kind = ShapeKind::Opaque { xml: part.keep(e), preview, label, parts }; s } @@ -596,6 +601,7 @@ fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> let tree = d.root.child("spTree")?; let mut sub = IdCtx { fresh: true, ..Default::default() }; let before = imp.smartart_shapes; + let xml_before = imp.diagram_xml_bytes; let children = sp_tree(imp, &dpart, &mut sub, tree, depth + 1); if children.is_empty() { return None; @@ -605,6 +611,7 @@ fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> if total > MAX_SMARTART_SHAPES { // Over the presentation's remaining budget: a placeholder, and nothing charged, nested SmartArt included. imp.smartart_shapes = before; + imp.diagram_xml_bytes = xml_before; return None; } imp.smartart_shapes = total; From 64c89064eae2f195c29e89e9166db5f78604453d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 15:11:54 +0000 Subject: [PATCH 06/11] address review: drop media copied by a rolled-back diagram fallback or parent drawing --- crates/pptx/src/read.rs | 9 +++++++++ crates/pptx/src/read/shapes.rs | 6 ++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/pptx/src/read.rs b/crates/pptx/src/read.rs index 9c8756f87..886201a30 100644 --- a/crates/pptx/src/read.rs +++ b/crates/pptx/src/read.rs @@ -76,6 +76,15 @@ impl Imp<'_> { self.media_by_part.insert(key, id); Some(id) } + /// Drop the media copied since the list was `len` long, with their part-cache entries: used when + /// the content that read them is discarded, so they are neither kept nor reused uncharged. + pub fn forget_media_from(&mut self, len: usize) { + if self.media.len() > len { + let gone: std::collections::HashSet = self.media[len..].iter().map(|m| m.id).collect(); + self.media.truncate(len); + self.media_by_part.retain(|_, id| !gone.contains(id)); + } + } /// Media item for an external link. pub fn media_link(&mut self, url: &str) -> MediaId { if let Some(m) = self.media.iter().find(|m| m.link.as_deref() == Some(url)) { diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index 4e3b54398..98ec3a155 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -471,7 +471,7 @@ fn frame(imp: &mut Imp, part: &Part, ctx: &mut IdCtx, e: &El, depth: usize) -> S }; // Keep the parts it points to so a save writes it back whole (#203). A part with relationships // of its own, or an external link, can't be carried: the save then falls back to the preview. - let xml_before = imp.diagram_xml_bytes; + let (xml_before, media_before) = (imp.diagram_xml_bytes, imp.media.len()); let parts = super::rel_refs(e) .into_iter() .map(|id| { @@ -496,6 +496,7 @@ fn frame(imp: &mut Imp, part: &Part, ctx: &mut IdCtx, e: &El, depth: usize) -> S // A fallback that drops its parts does not keep their bytes charged. if parts.is_none() { imp.diagram_xml_bytes = xml_before; + imp.forget_media_from(media_before); } let parts = parts.unwrap_or_default(); s.kind = ShapeKind::Opaque { xml: part.keep(e), preview, label, parts }; @@ -601,7 +602,7 @@ fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> let tree = d.root.child("spTree")?; let mut sub = IdCtx { fresh: true, ..Default::default() }; let before = imp.smartart_shapes; - let xml_before = imp.diagram_xml_bytes; + let (xml_before, media_before) = (imp.diagram_xml_bytes, imp.media.len()); let children = sp_tree(imp, &dpart, &mut sub, tree, depth + 1); if children.is_empty() { return None; @@ -612,6 +613,7 @@ fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> // Over the presentation's remaining budget: a placeholder, and nothing charged, nested SmartArt included. imp.smartart_shapes = before; imp.diagram_xml_bytes = xml_before; + imp.forget_media_from(media_before); return None; } imp.smartart_shapes = total; From 175fa54642ec6d36ee2514db09390b7b96f40a8d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:50:18 +0000 Subject: [PATCH 07/11] address review: roll back by journal, preflight fallback parts, restore tag and chart sidecar accounting on a rejected drawing --- crates/pptx/src/read.rs | 53 ++++++++++++++++++++---- crates/pptx/src/read/chart.rs | 1 + crates/pptx/src/read/shapes.rs | 76 +++++++++++++++++++++------------- 3 files changed, 95 insertions(+), 35 deletions(-) diff --git a/crates/pptx/src/read.rs b/crates/pptx/src/read.rs index 886201a30..189bd81de 100644 --- a/crates/pptx/src/read.rs +++ b/crates/pptx/src/read.rs @@ -24,11 +24,24 @@ use crate::PptxError; use crate::opc::{self, Package, Rels}; use crate::xml::{Doc, El, Node}; +/// A snapshot of the import state a discarded read rolls back to. +pub struct Mark { + diagram_xml_bytes: usize, + media: usize, + media_keys: usize, + tag_bytes: usize, + tags_capped: bool, + kept_sidecar_bytes: usize, + kept_chart_keys: usize, +} + /// Import state shared by all parts. pub struct Imp<'a> { pub pkg: &'a Package, pub media: Vec, pub media_by_part: HashMap, + /// Keys added to `media_by_part`, in order, so a rollback removes only its own. + pub media_keys: Vec, pub next: u32, pub slide_by_part: HashMap, pub custom_show_names: HashMap, @@ -40,6 +53,8 @@ pub struct Imp<'a> { pub kept_sidecar_bytes: usize, /// Cached kept sidecars by normalized chart part name. pub kept_charts: HashMap, + /// Keys added to `kept_charts`, in order, for rollback. + pub kept_chart_keys: Vec, /// Add-in tags parts, parsed once each, by normalized part name. pub tags_by_part: HashMap>, /// Bytes of tags copied into the model so far (see [`MAX_TAG_BYTES`]). @@ -73,16 +88,36 @@ impl Imp<'_> { let content_type = self.pkg.content_type(part).filter(|c| !c.is_empty()).unwrap_or_else(|| crate::mime_for(&name).to_string()); let id = MediaId(self.alloc()); self.media.push(MediaItem { id, name, content_type, data: Arc::new(data), link: None }); + self.media_keys.push(key.clone()); self.media_by_part.insert(key, id); Some(id) } - /// Drop the media copied since the list was `len` long, with their part-cache entries: used when - /// the content that read them is discarded, so they are neither kept nor reused uncharged. - pub fn forget_media_from(&mut self, len: usize) { - if self.media.len() > len { - let gone: std::collections::HashSet = self.media[len..].iter().map(|m| m.id).collect(); - self.media.truncate(len); - self.media_by_part.retain(|_, id| !gone.contains(id)); + /// What a discarded read must give back: the budgets it charged and the media and kept-chart + /// entries it added (see [`Imp::rollback`]). + pub fn mark(&self) -> Mark { + Mark { + diagram_xml_bytes: self.diagram_xml_bytes, + media: self.media.len(), + media_keys: self.media_keys.len(), + tag_bytes: self.tag_bytes, + tags_capped: self.tags_capped, + kept_sidecar_bytes: self.kept_sidecar_bytes, + kept_chart_keys: self.kept_chart_keys.len(), + } + } + /// Undo everything since `m`: only the entries added since are removed, so the cost follows + /// what the discarded read added, not the size of the caches. + pub fn rollback(&mut self, m: &Mark) { + self.diagram_xml_bytes = m.diagram_xml_bytes; + self.tag_bytes = m.tag_bytes; + self.tags_capped = m.tags_capped; + self.kept_sidecar_bytes = m.kept_sidecar_bytes; + self.media.truncate(m.media); + for k in self.media_keys.drain(m.media_keys..) { + self.media_by_part.remove(&k); + } + for k in self.kept_chart_keys.drain(m.kept_chart_keys..) { + self.kept_charts.remove(&k); } } /// Media item for an external link. @@ -454,6 +489,7 @@ pub fn import_theme(bytes: &[u8]) -> Result<(deckcraft_model::theme::Theme, Vec< pkg: &pkg, media: vec![], media_by_part: HashMap::new(), + media_keys: vec![], next: 0, slide_by_part: HashMap::new(), custom_show_names: HashMap::new(), @@ -461,6 +497,7 @@ pub fn import_theme(bytes: &[u8]) -> Result<(deckcraft_model::theme::Theme, Vec< vml_previews: HashMap::new(), kept_sidecar_bytes: 0, kept_charts: HashMap::new(), + kept_chart_keys: vec![], tags_by_part: HashMap::new(), tag_bytes: 0, tags_capped: false, @@ -499,6 +536,7 @@ pub fn import_checked(bytes: &[u8]) -> Result<(Presentation, Vec), PptxE pkg: &pkg, media: vec![], media_by_part: HashMap::new(), + media_keys: vec![], next: max_id.max(1), slide_by_part: HashMap::new(), custom_show_names: HashMap::new(), @@ -506,6 +544,7 @@ pub fn import_checked(bytes: &[u8]) -> Result<(Presentation, Vec), PptxE vml_previews: HashMap::new(), kept_sidecar_bytes: 0, kept_charts: HashMap::new(), + kept_chart_keys: vec![], tags_by_part: HashMap::new(), tag_bytes: 0, tags_capped: false, diff --git a/crates/pptx/src/read/chart.rs b/crates/pptx/src/read/chart.rs index 358f88ff5..059b79031 100644 --- a/crates/pptx/src/read/chart.rs +++ b/crates/pptx/src/read/chart.rs @@ -485,6 +485,7 @@ pub(crate) fn capture_kept(imp: &mut super::Imp, chart_part: &str) -> deckcraft_ kept.rels.push(ChartRel { id: r.id, rel_type: r.ty, target: r.target_raw, part: r.target, external: false }); } } + imp.kept_chart_keys.push(key.clone()); imp.kept_charts.insert(key, kept.clone()); kept } diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index 98ec3a155..3002e4e13 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -471,33 +471,53 @@ fn frame(imp: &mut Imp, part: &Part, ctx: &mut IdCtx, e: &El, depth: usize) -> S }; // Keep the parts it points to so a save writes it back whole (#203). A part with relationships // of its own, or an external link, can't be carried: the save then falls back to the preview. - let (xml_before, media_before) = (imp.diagram_xml_bytes, imp.media.len()); - let parts = super::rel_refs(e) - .into_iter() - .map(|id| { - let rel = part.rels.get(&id).filter(|r| !r.external)?; - let pkg = imp.pkg; - if *imp.has_rels.entry(crate::opc::norm(&rel.target)).or_insert_with(|| !pkg.rels(&rel.target).items.is_empty()) { - return None; + // Validate every relationship (and charge the budget) before copying anything, so a fallback + // that cannot be kept whole copies no part at all. + let mark = imp.mark(); + let mut planned = Vec::new(); + let mut spent = imp.diagram_xml_bytes; + let mut ok = true; + for id in super::rel_refs(e) { + let Some(rel) = part.rels.get(&id).filter(|r| !r.external) else { + ok = false; + break; + }; + let pkg = imp.pkg; + if *imp.has_rels.entry(crate::opc::norm(&rel.target)).or_insert_with(|| !pkg.rels(&rel.target).items.is_empty()) { + ok = false; + break; + } + // A diagram's definition parts copied here count against the same presentation-wide budget. + if rel.kind.starts_with("diagram") && !imp.media_by_part.contains_key(&crate::opc::norm(&rel.target)) { + let len = pkg.get(&rel.target).map_or(0, |b| b.len()); + if len > MAX_DIAGRAM_PART_BYTES || spent.saturating_add(len) > MAX_DIAGRAM_XML_BYTES { + ok = false; + break; } - let (kind, target) = (rel.ty.clone(), rel.target.clone()); - // A diagram's definition parts copied here count against the same presentation-wide budget. - if rel.kind.starts_with("diagram") && !imp.media_by_part.contains_key(&crate::opc::norm(&target)) { - let len = pkg.get(&target).map_or(0, |b| b.len()); - if len > MAX_DIAGRAM_PART_BYTES || imp.diagram_xml_bytes.saturating_add(len) > MAX_DIAGRAM_XML_BYTES { - return None; - } - imp.diagram_xml_bytes += len; - } - let media = if rel.kind == "diagramData" { diagram_data(imp, &target) } else { imp.media_for(&target) }; - Some((id, kind, media?)) - }) - .collect::>>(); - // A fallback that drops its parts does not keep their bytes charged. - if parts.is_none() { - imp.diagram_xml_bytes = xml_before; - imp.forget_media_from(media_before); + spent += len; + } + if pkg.get(&rel.target).is_none() { + ok = false; + break; + } + planned.push((id, rel.ty.clone(), rel.target.clone(), rel.kind == "diagramData")); } + let parts = if ok { + imp.diagram_xml_bytes = spent; + let copied: Option> = planned + .into_iter() + .map(|(id, kind, target, data)| { + let media = if data { diagram_data(imp, &target) } else { imp.media_for(&target) }; + Some((id, kind, media?)) + }) + .collect(); + if copied.is_none() { + imp.rollback(&mark); + } + copied + } else { + None + }; let parts = parts.unwrap_or_default(); s.kind = ShapeKind::Opaque { xml: part.keep(e), preview, label, parts }; s @@ -569,6 +589,7 @@ fn diagram_data(imp: &mut Imp, target: &str) -> Option data: std::sync::Arc::new(xml.into_bytes()), link: None, }); + imp.media_keys.push(norm_target.clone()); imp.media_by_part.insert(norm_target, id); Some(id) } @@ -602,7 +623,7 @@ fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> let tree = d.root.child("spTree")?; let mut sub = IdCtx { fresh: true, ..Default::default() }; let before = imp.smartart_shapes; - let (xml_before, media_before) = (imp.diagram_xml_bytes, imp.media.len()); + let mark = imp.mark(); let children = sp_tree(imp, &dpart, &mut sub, tree, depth + 1); if children.is_empty() { return None; @@ -612,8 +633,7 @@ fn smartart(imp: &mut Imp, part: &Part, gd: &El, frame: &Shape, depth: usize) -> if total > MAX_SMARTART_SHAPES { // Over the presentation's remaining budget: a placeholder, and nothing charged, nested SmartArt included. imp.smartart_shapes = before; - imp.diagram_xml_bytes = xml_before; - imp.forget_media_from(media_before); + imp.rollback(&mark); return None; } imp.smartart_shapes = total; From 3746f259a03bee4936b5b4a304edc1e9067b117d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:22:48 +0000 Subject: [PATCH 08/11] test: rollback restores budgets and removes only what the discarded read added --- crates/pptx/src/read.rs | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/crates/pptx/src/read.rs b/crates/pptx/src/read.rs index 189bd81de..c638376b8 100644 --- a/crates/pptx/src/read.rs +++ b/crates/pptx/src/read.rs @@ -1482,3 +1482,47 @@ fn header_footer(p: &Presentation) -> HeaderFooter { } hf } + +#[cfg(test)] +mod rollback_tests { + use super::*; + + /// A rolled-back read gives back its budgets and removes only the media and kept-chart entries it added. + #[test] + fn rollback_restores_budgets_and_removes_only_what_was_added() { + let bytes = crate::export(&deckcraft_model::Presentation::default()).expect("export"); + let pkg = Package::open(&bytes).expect("package"); + let mut imp = Imp { + pkg: &pkg, + media: vec![], + media_by_part: HashMap::new(), + media_keys: vec![], + next: 0, + slide_by_part: HashMap::new(), + custom_show_names: HashMap::new(), + table_style_ids: Vec::new(), + vml_previews: HashMap::new(), + kept_sidecar_bytes: 0, + kept_charts: HashMap::new(), + kept_chart_keys: vec![], + tags_by_part: HashMap::new(), + tag_bytes: 0, + tags_capped: false, + theme_extra_bytes: 0, + smartart_shapes: 0, + diagram_xml_bytes: 0, + has_rels: HashMap::new(), + }; + let kept_before = imp.media_for("ppt/presentation.xml").expect("media"); + let (m, items) = (imp.mark(), imp.media.len()); + assert!(imp.media_for("ppt/presProps.xml").is_some()); + imp.kept_chart_keys.push("ppt/charts/chart1.xml".into()); + imp.kept_charts.insert("ppt/charts/chart1.xml".into(), Default::default()); + (imp.diagram_xml_bytes, imp.tag_bytes, imp.tags_capped, imp.kept_sidecar_bytes) = (7, 5, true, 9); + imp.rollback(&m); + assert_eq!(imp.media.len(), items); + assert_eq!(imp.media_by_part.get("ppt/presentation.xml"), Some(&kept_before)); + assert!(!imp.media_by_part.contains_key("ppt/presprops.xml") && imp.kept_charts.is_empty() && imp.kept_chart_keys.is_empty()); + assert_eq!((imp.diagram_xml_bytes, imp.tag_bytes, imp.tags_capped, imp.kept_sidecar_bytes), (0, 0, false, 0)); + } +} From 6bbb593d82970eda2ed87fc9cf6bd4e154f607fe Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:22:23 -0500 Subject: [PATCH 09/11] Use shared Base64 encoding for command payloads --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + crates/engine/Cargo.toml | 1 + crates/engine/src/cmd.rs | 31 +++++++++++++++++-------------- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a34345398..6b4a4acd7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -853,6 +853,11 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-codepage" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725" + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -900,6 +905,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-mime" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725" +dependencies = [ + "chrono", + "craft-codepage", + "encoding_rs", +] + [[package]] name = "craft-pdf" version = "0.1.0" @@ -1110,6 +1125,7 @@ name = "deckcraft-engine" version = "0.19.0" dependencies = [ "chrono", + "craft-mime", "deckcraft-anim", "deckcraft-color", "deckcraft-fonts", diff --git a/Cargo.toml b/Cargo.toml index 73c7e84aa..bc6f15c9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ vello_cpu = "0.2" vello_common = { version = "0.2", default-features = false } image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] } # craft-media, craft-audio and craft-video share types and stay on one rev. +craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" } craft-media = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false } craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["opus-decode"] } # H.264, HEVC, VP9 and AV1 decoders. diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index cb1f4c29f..40569d1d6 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -9,6 +9,7 @@ description = "DeckCraft engine: sessions, undo history, selection, the command workspace = true [dependencies] +craft-mime = { workspace = true } deckcraft-geom = { workspace = true } deckcraft-color = { workspace = true } deckcraft-model = { workspace = true } diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 59160df14..628571edd 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -607,20 +607,7 @@ fn param_optional(alt: &str) -> Option { /// Base64 (standard alphabet) for binary payloads in JSON params and results. pub fn base64_encode(data: &[u8]) -> String { - const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - let mut out = String::with_capacity(data.len().div_ceil(3) * 4); - for chunk in data.chunks(3) { - let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)]; - let n = ((b[0] as u32) << 16) | ((b[1] as u32) << 8) | b[2] as u32; - for (i, shift) in [18, 12, 6, 0].iter().enumerate() { - if i <= chunk.len() { - out.push(A.get(((n >> shift) & 63) as usize).copied().unwrap_or(b'A') as char); - } else { - out.push('='); - } - } - } - out + craft_mime::base64::STANDARD.encode(data) } pub fn base64_decode(s: &str) -> Option> { @@ -662,6 +649,22 @@ pub fn base64_decode(s: &str) -> Option> { mod tests { use super::*; + #[test] + fn shared_encoder_preserves_rfc4648_and_all_byte_remainders() { + for (input, expected) in + [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] + { + assert_eq!(base64_encode(input.as_bytes()), expected); + } + let all: Vec = (0..=255).cycle().take(513).collect(); + for len in 0..=all.len() { + let encoded = base64_encode(&all[..len]); + assert_eq!(encoded.len(), len.div_ceil(3) * 4); + assert_eq!(encoded.bytes().rev().take_while(|b| *b == b'=').count(), (3 - len % 3) % 3); + assert_eq!(base64_decode(&encoded).as_deref(), Some(&all[..len])); + } + } + #[test] fn base64_rejects_a_single_character_tail() { for (encoded, decoded) in [ From d842a0cc2ae94dfabeb549110e8fd63aa769c3bf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 14:54:50 -0500 Subject: [PATCH 10/11] Delegate Base64 decoding while preserving command input guards --- Cargo.lock | 4 +- Cargo.toml | 2 +- crates/engine/src/cmd.rs | 91 ++++++++++++++++++++++++++++------------ 3 files changed, 68 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b4a4acd7..17a5f4b1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -856,7 +856,7 @@ dependencies = [ [[package]] name = "craft-codepage" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725" +source = "git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097dbcef01219cf25c7db#0325b2d73676d736e94097dbcef01219cf25c7db" [[package]] name = "craft-fonts-build" @@ -908,7 +908,7 @@ dependencies = [ [[package]] name = "craft-mime" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725" +source = "git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097dbcef01219cf25c7db#0325b2d73676d736e94097dbcef01219cf25c7db" dependencies = [ "chrono", "craft-codepage", diff --git a/Cargo.toml b/Cargo.toml index bc6f15c9c..548ad1165 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,7 @@ vello_cpu = "0.2" vello_common = { version = "0.2", default-features = false } image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] } # craft-media, craft-audio and craft-video share types and stay on one rev. -craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" } +craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "0325b2d73676d736e94097dbcef01219cf25c7db", version = "=0.1.0" } craft-media = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false } craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["opus-decode"] } # H.264, HEVC, VP9 and AV1 decoders. diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 628571edd..b97d42b4c 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -612,43 +612,82 @@ pub fn base64_encode(data: &[u8]) -> String { pub fn base64_decode(s: &str) -> Option> { let s = s.split_once("base64,").map(|(_, b)| b).unwrap_or(s); - let mut out = Vec::with_capacity(s.len() / 4 * 3); - let mut acc = 0u32; - let mut bits = 0; - let mut padded = false; + // Preserve the command transport's admission rules before the shared legacy decoder: + // padding ends the payload and one trailing symbol cannot encode a byte. + let (mut symbols_mod_four, mut padded) = (0u8, false); for c in s.bytes() { - let v = match c { - b'A'..=b'Z' => c - b'A', - b'a'..=b'z' => c - b'a' + 26, - b'0'..=b'9' => c - b'0' + 52, - b'+' | b'-' => 62, - b'/' | b'_' => 63, - b'=' => { - padded = true; - continue; + match c { + b'=' => padded = true, + b'\n' | b'\r' | b' ' => {} + b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'+' | b'-' | b'/' | b'_' if !padded => { + symbols_mod_four = (symbols_mod_four + 1) % 4; } - b'\n' | b'\r' | b' ' => continue, _ => return None, - } as u32; - // Padding ends the payload; no further data may be decoded. - if padded { - return None; - } - acc = (acc << 6) | v; - bits += 6; - if bits >= 8 { - bits -= 8; - out.push(((acc >> bits) & 0xff) as u8); } } - // A trailing lone Base64 character cannot encode another byte. - (bits != 6).then_some(out) + if symbols_mod_four == 1 { + return None; + } + craft_mime::base64_compat::decode(s).ok() } #[cfg(test)] mod tests { use super::*; + // Frozen pre-cutover implementation (Brandon Thomas, with Mex's padding/tail fixes). + fn previous_base64_decode(s: &str) -> Option> { + let s = s.split_once("base64,").map(|(_, b)| b).unwrap_or(s); + let mut out = Vec::with_capacity(s.len() / 4 * 3); + let mut acc = 0u32; + let mut bits = 0; + let mut padded = false; + for c in s.bytes() { + let v = match c { + b'A'..=b'Z' => c - b'A', + b'a'..=b'z' => c - b'a' + 26, + b'0'..=b'9' => c - b'0' + 52, + b'+' | b'-' => 62, + b'/' | b'_' => 63, + b'=' => { + padded = true; + continue; + } + b'\n' | b'\r' | b' ' => continue, + _ => return None, + } as u32; + // Padding ends the payload; no further data may be decoded. + if padded { + return None; + } + acc = (acc << 6) | v; + bits += 6; + if bits >= 8 { + bits -= 8; + out.push(((acc >> bits) & 0xff) as u8); + } + } + // A trailing lone Base64 character cannot encode another byte. + (bits != 6).then_some(out) + } + + #[test] + fn shared_decoder_preserves_command_transport_admission() { + for text in ["", "=", "Zg== \r\n==", "Zg==A", "A", "AAAAA", "AB", "+_8=", "Z=m9v", "Zm9v\t", "Zm9v!", "data:text/plain;base64,Zm9v"] { + assert_eq!(base64_decode(text), previous_base64_decode(text), "{text:?}"); + } + let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/-_= \r\n!\t"; + let mut seed = 0x1234_5678u32; + for n in 0..50_000 { + let mut text = String::new(); + for _ in 0..n % 65 { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + text.push(char::from(alphabet[(seed as usize) % alphabet.len()])); + } + assert_eq!(base64_decode(&text), previous_base64_decode(&text), "{text:?}"); + } + } + #[test] fn shared_encoder_preserves_rfc4648_and_all_byte_remainders() { for (input, expected) in From 97423d80fdc6945f8a542eca75caa99786250c75 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:46:59 +0100 Subject: [PATCH 11/11] test(engine): pin the standard Base64 alphabet in the encoder test The lenient decoder accepts both alphabets, so the round trip alone would not catch a switch to the URL-safe encoder. --- crates/engine/src/cmd.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 628571edd..f257d3282 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -656,6 +656,8 @@ mod tests { { assert_eq!(base64_encode(input.as_bytes()), expected); } + // The decoder accepts both alphabets, so pin the standard one explicitly. + assert_eq!(base64_encode(&[0xfb, 0xff]), "+/8="); let all: Vec = (0..=255).cycle().take(513).collect(); for len in 0..=all.len() { let encoded = base64_encode(&all[..len]);