From edbea6036f88fe176be7a5b83f9b6670a9483b74 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 12:28:30 +0000 Subject: [PATCH 1/9] fix: bound recent-file preview decoding and PDF crop allocation --- crates/engine/src/cmd/file/open.rs | 10 ++++++++++ crates/render/src/pdf_raster.rs | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/crates/engine/src/cmd/file/open.rs b/crates/engine/src/cmd/file/open.rs index 4e90f30fb..86de7c369 100644 --- a/crates/engine/src/cmd/file/open.rs +++ b/crates/engine/src/cmd/file/open.rs @@ -16,11 +16,21 @@ pub struct RecentPreview { /// The picture a `.designcraft` file saved of its first page; None when the file is gone, isn't /// ours, or was saved before files had one (it gets one when saved again). +/// The most pixels a recent file's preview may have to be shown (its PNG is bounded by +/// `designcraft_format::MAX_PREVIEW_BYTES`, but a small PNG can claim a huge picture). +const MAX_PREVIEW_PIXELS: u64 = 1 << 24; + pub fn recent_preview(path: &str) -> Option { #[cfg(not(target_arch = "wasm32"))] { // Only the zip directory and the preview entry are read, not the whole document. let png = designcraft_format::read_preview(std::io::BufReader::new(crate::io::open(path).ok()?))?; + // A preview is a small picture: one whose header claims more pixels than that isn't + // decoded (a decode is bounded, but not at thumbnail size). + let (w, h) = designcraft_images::pixel_size(&png)?; + if u64::from(w).saturating_mul(u64::from(h)) > MAX_PREVIEW_PIXELS { + return None; + } let img = designcraft_images::decode_rgba(&png)?; let (width, height) = img.dimensions(); Some(RecentPreview { width, height, rgba: img.into_raw() }) diff --git a/crates/render/src/pdf_raster.rs b/crates/render/src/pdf_raster.rs index 76160be96..e660e1657 100644 --- a/crates/render/src/pdf_raster.rs +++ b/crates/render/src/pdf_raster.rs @@ -251,6 +251,11 @@ pub fn crop_pixmap(pm: &Pixmap, frac: [f64; 4]) -> Option { return None; } let (w, h) = (w as u16, h as u16); + // A box reaching far outside the page would mostly be transparent padding: it gets no more + // pixels than a page raster may have. + if u64::from(w) * u64::from(h) > crate::MAX_PAGE_PIXELS { + return None; + } let mut out = Pixmap::new(w, h); let src = pm.data(); let dst = out.data_mut(); @@ -272,3 +277,17 @@ pub fn crop_pixmap(pm: &Pixmap, frac: [f64; 4]) -> Option { } Some(out) } + +#[cfg(test)] +mod crop_tests { + use super::*; + + /// A crop box reaching far outside the page gets no bigger a pixmap than a page may have. + #[test] + fn crop_outside_the_page_is_bounded() { + let pm = Pixmap::new(10, 10); + assert!(crop_pixmap(&pm, [-0.5, -0.5, 1.5, 1.5]).is_some_and(|c| (c.width(), c.height()) == (20, 20))); + // 60,000 × 60,000 pixels: within u16 each way, far past the page raster budget. + assert!(crop_pixmap(&pm, [-3000.0, -3000.0, 3000.0, 3000.0]).is_none()); + } +} From 3f6e234e14e993478a857ac2f8b41cab745a57d0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 15:33:40 +0000 Subject: [PATCH 2/9] fix(compose): bound hyphenation dictionary inflation, find the last plain glyph as the breaker goes --- crates/compose/src/breaker.rs | 9 ++++++++- crates/compose/src/hyphen/dict.rs | 20 ++++++++++++++++++-- crates/compose/src/tests.rs | 12 ++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/compose/src/breaker.rs b/crates/compose/src/breaker.rs index 6f62542d7..46fb3bdff 100644 --- a/crates/compose/src/breaker.rs +++ b/crates/compose/src/breaker.rs @@ -395,7 +395,14 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S for i in (0..n).rev() { hangs[i] = glyphs[i].is_space() && glyphs.get(i + 1).is_none_or(|nx| hangs[i + 1] || is_forced(nx.ch)); } + // The last glyph that isn't a soft hyphen, kept as the loop goes (a run of soft hyphens + // searched back over all of them for every one). + let mut last_plain: Option<&Glyph> = None; for (i, g) in glyphs.iter().enumerate() { + // Set before the soft-hyphen case below reads it; a soft hyphen leaves it as it was. + if g.ch != SOFT_HYPHEN { + last_plain = Some(g); + } let prev_hang = if i > 0 { right_hang(&glyphs[i - 1], sp) } else { 0.0 }; if hangs[i] { continue; @@ -444,7 +451,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S continue; } if g.ch == SOFT_HYPHEN { - let hw = glyphs[..i].iter().rev().find(|g| g.ch != SOFT_HYPHEN).map_or(g.size * 0.33, |p| hyphen_width(p, &mut hy_cache)); + let hw = last_plain.map_or(g.size * 0.33, |p| hyphen_width(p, &mut hy_cache)); let mut p = Item::penalty(hw, sp.hyphen_penalty, true); p.auto = true; p.zone_from = last_space; diff --git a/crates/compose/src/hyphen/dict.rs b/crates/compose/src/hyphen/dict.rs index 83a54d443..1b45c7edb 100644 --- a/crates/compose/src/hyphen/dict.rs +++ b/crates/compose/src/hyphen/dict.rs @@ -15,6 +15,9 @@ pub struct Dictionary { const MAGIC: &[u8] = b"DCHYPH1\n"; +/// Most bytes a serialized dictionary may inflate to (the bundled English one is far smaller). +pub const MAX_INFLATED_BYTES: usize = 64 << 20; + impl Dictionary { /// Build from `(lowercase word, break mask)` pairs (sorted and de-duplicated here; the first /// entry of a duplicate wins). @@ -76,9 +79,11 @@ impl Dictionary { miniz_oxide::deflate::compress_to_vec(&raw, 10) } - /// Deserialize data written by [`Dictionary::to_bytes`]. + /// Deserialize data written by [`Dictionary::to_bytes`]. At most [`MAX_INFLATED_BYTES`] are + /// inflated: a few compressed bytes can stand for gigabytes. pub fn from_bytes(data: &[u8]) -> Result { - let raw = miniz_oxide::inflate::decompress_to_vec(data).map_err(|e| format!("hyphenation dictionary: inflate failed: {e:?}"))?; + let raw = miniz_oxide::inflate::decompress_to_vec_with_limit(data, MAX_INFLATED_BYTES) + .map_err(|e| format!("hyphenation dictionary: inflate failed (or over {} MB): {e:?}", MAX_INFLATED_BYTES >> 20))?; let body = raw.strip_prefix(MAGIC).ok_or("hyphenation dictionary: bad header")?; let mut d = Dictionary { words: String::with_capacity(body.len()), offs: vec![0], masks: Vec::new() }; let mut cur: Vec = Vec::new(); @@ -147,4 +152,15 @@ mod tests { assert_eq!(back.get("naïveté").map(|m| hyphenated("naïveté", m)), Some("naïve-té".to_string())); assert!(back.get("dog").is_none()); } + + /// Data that inflates past the limit is refused instead of being inflated to the end. + #[test] + fn an_inflation_bomb_is_refused() { + let mut raw = MAGIC.to_vec(); + raw.resize(MAX_INFLATED_BYTES + 1, b'a'); + let bomb = miniz_oxide::deflate::compress_to_vec(&raw, 10); + assert!(bomb.len() < 1 << 20, "{}", bomb.len()); + let e = Dictionary::from_bytes(&bomb).err().unwrap_or_default(); + assert!(e.contains("inflate failed"), "{e}"); + } } diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 7b5e1024f..dfae2c2d4 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -46,6 +46,18 @@ fn all_lines(cs: &ComposedStory) -> Vec<&Line> { cs.frames.iter().flat_map(|f| f.lines.iter()).collect() } +/// A long run of soft hyphens composes: each one's hyphen width comes from the last other glyph, +/// kept as the breaker goes (it searched back over the whole run for every one). +#[test] +fn a_run_of_soft_hyphens_composes() { + let text = format!("Word{}end", "\u{AD}".repeat(40_000)); + let (d, sid, _) = doc_with(&text, Rect::new(36.0, 36.0, 300.0, 700.0), ParaAttrs::default()); + let t = std::time::Instant::now(); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + assert!(t.elapsed() < std::time::Duration::from_secs(20), "{:?}", t.elapsed()); + assert!(!all_lines(&cs).is_empty()); +} + #[test] fn composes_simple_paragraph() { let (d, sid, _) = doc_with(LOREM, Rect::new(36.0, 36.0, 300.0, 700.0), ParaAttrs::default()); From acf478e6e78abfc0b85c89a920b03815091b5162 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:35:07 +0000 Subject: [PATCH 3/9] Apply review fixes - crates/engine/src/cmd/file/open.rs --- crates/engine/src/cmd/file/open.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/file/open.rs b/crates/engine/src/cmd/file/open.rs index 86de7c369..41df29723 100644 --- a/crates/engine/src/cmd/file/open.rs +++ b/crates/engine/src/cmd/file/open.rs @@ -14,12 +14,12 @@ pub struct RecentPreview { pub rgba: Vec, } -/// The picture a `.designcraft` file saved of its first page; None when the file is gone, isn't -/// ours, or was saved before files had one (it gets one when saved again). /// The most pixels a recent file's preview may have to be shown (its PNG is bounded by /// `designcraft_format::MAX_PREVIEW_BYTES`, but a small PNG can claim a huge picture). const MAX_PREVIEW_PIXELS: u64 = 1 << 24; +/// The picture a `.designcraft` file saved of its first page; None when the file is gone, isn't +/// ours, or was saved before files had one (it gets one when saved again). pub fn recent_preview(path: &str) -> Option { #[cfg(not(target_arch = "wasm32"))] { From 032fd7c08acd30da0d03b805e640dab5b7d28df3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 20:41:04 +0000 Subject: [PATCH 4/9] epub: export budgets for data URIs and variables, note index lookups, graphics index, title cap --- crates/epub/src/html.rs | 28 ++++++++++++++++++-- crates/epub/src/package.rs | 15 +++++++++-- crates/epub/src/story_writer.rs | 46 ++++++++++++++++++++++++++++----- crates/epub/src/tests.rs | 30 +++++++++++++++++++++ 4 files changed, 108 insertions(+), 11 deletions(-) diff --git a/crates/epub/src/html.rs b/crates/epub/src/html.rs index 7f7fbe26f..6442d5045 100644 --- a/crates/epub/src/html.rs +++ b/crates/epub/src/html.rs @@ -129,6 +129,17 @@ pub(crate) fn body_html<'a>(doc: &'a Document, epub: bool, split: Option<&EpubSp } let mut body = String::new(); let mut toc = toc_entries(doc, &order); + // `Document::item` searches every spread: one pre-order index for all the placed graphics. + let mut items: std::collections::HashMap = std::collections::HashMap::new(); + if order.iter().any(|b| matches!(b, Block::Image(_))) { + for sp in doc.spreads.iter().map(|s| &**s).chain(doc.parents.iter().map(|s| &**s)) { + for it in &sp.items { + it.walk(&mut |i| { + items.entry(i.id).or_insert(i); + }); + } + } + } for (k, b) in order.iter().enumerate() { match b { // The endnote frame's text is generated from the endnotes, which end the body as a @@ -139,7 +150,7 @@ pub(crate) fn body_html<'a>(doc: &'a Document, epub: bool, split: Option<&EpubSp let _ = write!(body, "
\n{}
\n", writer.story(*sid, epub.then_some(anchor.as_str()))); } Block::Image(iid) => { - let Some(it) = doc.item(*iid) else { continue }; + let Some(&it) = items.get(iid) else { continue }; let Some((markup, media)) = writer.graphic(it) else { continue }; if media { let _ = writeln!(body, "
{markup}
"); @@ -185,13 +196,26 @@ pub struct HtmlOptions { pub generic_families: BTreeMap, } +/// Bytes of data URIs one HTML export may write (a document can place one large asset many times). +const MAX_DATA_URI_BYTES: usize = 512 * 1024 * 1024; + /// File › Export › HTML: one self-contained page (styles inline, images as data URIs) with the /// document's stories and graphics in reading order. pub fn export_html(doc: &Document, opts: &HtmlOptions) -> String { let title = opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string()); let language = opts.language.as_deref().unwrap_or("en"); let lang = language_tag(language); - let body = body_html(doc, false, None, &|_, mime, data| format!("data:{mime};base64,{}", base64(data))).html; + // Each placement embeds its own copy of the asset; past the budget a placement gets no source. + let spent = std::cell::Cell::new(0usize); + let body = body_html(doc, false, None, &|_, mime, data| { + let n = data.len().div_ceil(3).saturating_mul(4).saturating_add(mime.len() + 13); + if n > MAX_DATA_URI_BYTES - spent.get() { + return String::new(); + } + spent.set(spent.get() + n); + format!("data:{mime};base64,{}", base64(data)) + }) + .html; format!( "\n\n\n\n\n\n{}\n\n\n\n{body}\n\n", xml_text(&title), diff --git a/crates/epub/src/package.rs b/crates/epub/src/package.rs index fff4fd6fe..63e0399f0 100644 --- a/crates/epub/src/package.rs +++ b/crates/epub/src/package.rs @@ -21,6 +21,17 @@ fn modified(doc: &Document, opts: &EpubOptions) -> String { t.map_or_else(|| "2026-01-01T00:00:00Z".to_string(), iso8601) } +/// Characters of the title the package keeps: it is copied into every content file, so a huge one +/// would multiply by the number of chapters or pages. +const MAX_TITLE_CHARS: usize = 1024; + +fn capped_title(title: String) -> String { + match title.char_indices().nth(MAX_TITLE_CHARS) { + Some((at, _)) => title[..at].to_string(), + None => title, + } +} + /// The book's identifier: the given one, else a `urn:uuid` derived from the title and the /// document's creation time, so a book keeps its identifier from export to export and two books /// with the same title don't share one. @@ -70,7 +81,7 @@ pub struct FixedPage { /// A pre-paginated (fixed-layout) EPUB: one XHTML page per document page. pub fn export_fixed_epub(doc: &Document, pages: &[FixedPage], opts: &EpubOptions) -> Result, EpubError> { - let title = opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string()); + let title = capped_title(opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string())); let lang = language_tag(&opts.language); let id = identifier(doc, opts, &title); let modified = modified(doc, opts); @@ -161,7 +172,7 @@ fn write_package<'a>(opf: &str, nav: &str, entries: impl IntoIterator Result, EpubError> { - let title = opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string()); + let title = capped_title(opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string())); let id = identifier(doc, opts, &title); let modified = modified(doc, opts); let Body { html: body, images, toc, .. } = body_html(doc, true, opts.split.as_ref(), &|name, _, _| name.to_string()); diff --git a/crates/epub/src/story_writer.rs b/crates/epub/src/story_writer.rs index 40c95f135..d6fb05159 100644 --- a/crates/epub/src/story_writer.rs +++ b/crates/epub/src/story_writer.rs @@ -50,8 +50,17 @@ pub(crate) struct StoryWriter<'a, 'f> { heading_ids: Option, /// Where EPUB content files start (see [`starts_chapter`]). pub(crate) split: EpubSplit, + /// Per story (by address): the byte offsets of its footnote and endnote references, so a note's + /// index is a search, not a count over the text before it. + note_refs: std::collections::HashMap, Vec)>, + /// Bytes of variable values still allowed into the output (see [`MAX_VARIABLE_BYTES`]). + variable_bytes: usize, } +/// Bytes of variable values one export may write: one stored value referenced many times would +/// otherwise multiply into the output. +const MAX_VARIABLE_BYTES: usize = 64 * 1024 * 1024; + impl<'a, 'f> StoryWriter<'a, 'f> { pub(crate) fn new(doc: &'a Document, epub: bool, img_src: Option<&'f dyn Fn(&str, &str, &[u8]) -> String>) -> Self { StoryWriter { @@ -69,9 +78,22 @@ impl<'a, 'f> StoryWriter<'a, 'f> { no_alt: Vec::new(), heading_ids: None, split: doc.settings.epub_split.clone(), + note_refs: std::collections::HashMap::new(), + variable_bytes: MAX_VARIABLE_BYTES, } } + /// Index of the footnote (or endnote) whose reference is at byte `pos` of `st` + /// (`Story::notes_before` / `endnotes_before`, from offsets found once per story). + fn note_index(&mut self, st: &Story, pos: usize, endnote: bool) -> usize { + let (foot, end) = self.note_refs.entry(std::ptr::from_ref(st) as usize).or_insert_with(|| { + let at = |c: char| st.text.match_indices(c).map(|(i, _)| i).collect::>(); + (at(FOOTNOTE_REF), at(ENDNOTE_REF)) + }); + let list = if endnote { end } else { foot }; + list.partition_point(|&p| p < pos) + } + fn epub_type(&self, t: &str) -> String { if self.epub { format!(" epub:type=\"{t}\"") } else { String::new() } } @@ -182,7 +204,7 @@ impl<'a, 'f> StoryWriter<'a, 'f> { if c != FOOTNOTE_REF && c != ENDNOTE_REF && c != OBJECT_MARK { continue; } - t.push_str(&run_text(doc, raw.get(seg_start..i).unwrap_or_default())); + t.push_str(&run_text(doc, raw.get(seg_start..i).unwrap_or_default(), &mut self.variable_bytes)); seg_start = i + c.len_utf8(); if c == OBJECT_MARK { let k = mark.unwrap_or_else(|| st.text.get(..a).map_or(0, |t| t.matches(OBJECT_MARK).count())); @@ -198,7 +220,7 @@ impl<'a, 'f> StoryWriter<'a, 'f> { } } } - t.push_str(&run_text(doc, raw.get(seg_start..).unwrap_or_default())); + t.push_str(&run_text(doc, raw.get(seg_start..).unwrap_or_default(), &mut self.variable_bytes)); t } @@ -303,7 +325,8 @@ impl<'a, 'f> StoryWriter<'a, 'f> { /// The footnote whose reference is at byte `pos` of `st`: a note reference in `out`, the note /// itself in `after`. fn footnote(&mut self, st: &Story, pos: usize, depth: usize, out: &mut String, after: &mut String) { - let Some(note) = st.notes.get(st.notes_before(pos)) else { return }; + let k = self.note_index(st, pos, false); + let Some(note) = st.notes.get(k) else { return }; let doc = self.doc; let o = &doc.footnote_options; let n = self.next_footnote; @@ -325,7 +348,8 @@ impl<'a, 'f> StoryWriter<'a, 'f> { /// The endnote whose reference is at byte `pos` of `st`: a note reference in `out`; the note /// joins the endnotes section. fn endnote(&mut self, st: &Story, pos: usize, depth: usize, out: &mut String) { - let Some(note) = st.endnotes.get(st.endnotes_before(pos)) else { return }; + let k = self.note_index(st, pos, true); + let Some(note) = st.endnotes.get(k) else { return }; let n = self.next_endnote; self.next_endnote = n.saturating_add(1); self.endnote_ids = self.endnote_ids.saturating_add(1); @@ -416,13 +440,21 @@ impl<'a, 'f> StoryWriter<'a, 'f> { } } -/// Run text with variables replaced by their values. -fn run_text(doc: &Document, raw: &str) -> String { +/// Run text with variables replaced by their values; `left` is what the export may still write +/// of them (a value that doesn't fit is left out). +fn run_text(doc: &Document, raw: &str, left: &mut usize) -> String { if raw.chars().any(|c| designcraft_doc::vars::var_index(c).is_some()) { let sub: String = raw .chars() .map(|c| match designcraft_doc::vars::var_index(c) { - Some(i) => doc.variable_value(i, None).unwrap_or_default(), + Some(i) => { + let v = doc.variable_value(i, None).unwrap_or_default(); + if v.len() > *left { + return String::new(); + } + *left -= v.len(); + v + } None => c.to_string(), }) .collect(); diff --git a/crates/epub/src/tests.rs b/crates/epub/src/tests.rs index b3865dfc1..1fdd25157 100644 --- a/crates/epub/src/tests.rs +++ b/crates/epub/src/tests.rs @@ -28,6 +28,36 @@ fn exports_valid_package_in_reading_order() { /// #766: anchored graphics are exported where they are anchored (inline in the text, an /// above-line object as a block of its own), and their images go into the package. +/// Many footnotes and endnotes each export with their own text, in reference order. +#[test] +fn many_notes_keep_their_own_text() { + let mut d = Document::new(&NewDocument::default()); + let lid = d.default_layer(); + let (_, sid) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 36.0, 300.0, 400.0), lid, "x", ParaFormat::default()).unwrap(); + let st = d.story_mut(sid).unwrap(); + for i in 0..300 { + let end = st.len(); + st.insert_note(end, &format!("foot{i}z"), ParaFormat::default()); + let end = st.len(); + st.insert_endnote(end, &format!("end{i}z"), ParaFormat::default()); + } + let html = export_html(&d, &HtmlOptions::default()); + let at = |needle: &str| html.find(needle).unwrap_or_else(|| panic!("{needle}")); + assert!(at("foot0z") < at("foot1z") && at("foot298z") < at("foot299z")); + assert!(at("end0z") < at("end1z") && at("end298z") < at("end299z")); +} + +/// A huge title is cut before it is copied into every content file. +#[test] +fn a_huge_title_is_capped() { + let d = tables_and_notes(); + let opts = EpubOptions { title: Some("T".repeat(5000)), ..EpubOptions::default() }; + let mut z = zip::ZipArchive::new(std::io::Cursor::new(export_epub(&d, &opts).unwrap())).unwrap(); + let mut s = String::new(); + z.by_name("OEBPS/content.xhtml").unwrap().read_to_string(&mut s).unwrap(); + assert!(s.contains(&"T".repeat(1024)) && !s.contains(&"T".repeat(1025)), "{}", s.len()); +} + #[test] fn anchored_objects_are_exported_in_place() { let mut d = Document::new(&NewDocument::default()); From b0cf3f75fd544f0a7e6f2f9ac095ffcf2fcaf42d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 22:24:45 +0000 Subject: [PATCH 5/9] xtask: bound the corpus download (archive size, entries, expanded bytes) --- xtask/src/main.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index c1d08071b..0fe078ed0 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -249,6 +249,27 @@ fn cmd_ci() -> Result<(), String> { } const PNGSUITE_URL: &str = "http://www.schaik.com/pngsuite/PngSuite-2017jul19.tgz"; +/// The real archive is 68 KB, 178 entries and 256 KB expanded; the site has no HTTPS, so what +/// arrives is bounded instead of trusted: compressed size, entries, and expanded bytes. +const PNGSUITE_MAX_ARCHIVE: u64 = 1 << 20; +const PNGSUITE_MAX_ENTRIES: usize = 1000; +const PNGSUITE_MAX_EXTRACTED: u64 = 16 << 20; + +/// Bytes of the files under `dir`. +fn dir_bytes(dir: &std::path::Path) -> u64 { + let mut total = 0u64; + let mut stack = vec![dir.to_path_buf()]; + while let Some(d) = stack.pop() { + for e in std::fs::read_dir(&d).into_iter().flatten().flatten() { + match e.metadata() { + Ok(m) if m.is_dir() => stack.push(e.path()), + Ok(m) => total = total.saturating_add(m.len()), + Err(_) => {} + } + } + } + total +} fn cmd_corpus(download: bool) -> Result<(), String> { let corpus = root().join("corpus"); @@ -273,12 +294,27 @@ Tests that use a corpus skip cleanly when it is absent. std::fs::create_dir_all(&dest).map_err(|e| format!("create {}: {e}", dest.display()))?; let tgz = corpus.join("PngSuite-2017jul19.tgz"); let mut curl = Command::new("curl"); - curl.args(["-fsSL", "-o"]).arg(&tgz).arg(PNGSUITE_URL); + curl.args(["-fsSL", "--max-filesize"]).arg(PNGSUITE_MAX_ARCHIVE.to_string()).arg("-o").arg(&tgz).arg(PNGSUITE_URL); run(curl, &format!("curl {PNGSUITE_URL}"))?; + let refuse = |why: String| { + let _ = std::fs::remove_file(&tgz); + Err(why) + }; + if std::fs::metadata(&tgz).map_or(true, |m| m.len() > PNGSUITE_MAX_ARCHIVE) { + return refuse(format!("the PngSuite download is larger than {PNGSUITE_MAX_ARCHIVE} bytes")); + } + let listing = Command::new("tar").arg("-tzf").arg(&tgz).output().map_err(|e| format!("tar -tzf: failed to spawn: {e}"))?; + if !listing.status.success() || listing.stdout.iter().filter(|b| **b == b'\n').count() > PNGSUITE_MAX_ENTRIES { + return refuse(format!("the PngSuite download isn't a tar.gz of at most {PNGSUITE_MAX_ENTRIES} entries")); + } let mut tar = Command::new("tar"); tar.arg("-xzf").arg(&tgz).arg("-C").arg(&dest); run(tar, "tar -xzf PngSuite-2017jul19.tgz")?; let _ = std::fs::remove_file(&tgz); + if dir_bytes(&dest) > PNGSUITE_MAX_EXTRACTED { + let _ = std::fs::remove_dir_all(&dest); + return Err(format!("the PngSuite archive expanded to more than {PNGSUITE_MAX_EXTRACTED} bytes; removed {}", dest.display())); + } let n = std::fs::read_dir(&dest).map(|d| d.flatten().filter(|e| e.path().extension().is_some_and(|x| x == "png")).count()).unwrap_or(0); println!("PngSuite: {n} PNG files in {}", dest.display()); if n == 0 { Err("no PNG files extracted".into()) } else { Ok(()) } From b8af07ee680adeaf1bab3aa248fa5e18868612ce Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Tue, 3 Nov 2026 22:33:04 +0000 Subject: [PATCH 6/9] Apply review fixes - xtask/src/main.rs --- xtask/src/main.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 0fe078ed0..19f8557ad 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -255,16 +255,18 @@ const PNGSUITE_MAX_ARCHIVE: u64 = 1 << 20; const PNGSUITE_MAX_ENTRIES: usize = 1000; const PNGSUITE_MAX_EXTRACTED: u64 = 16 << 20; -/// Bytes of the files under `dir`. +/// Bytes of the files under `dir`; `u64::MAX` when any part of it cannot be read, so the caller refuses it. fn dir_bytes(dir: &std::path::Path) -> u64 { let mut total = 0u64; let mut stack = vec![dir.to_path_buf()]; while let Some(d) = stack.pop() { - for e in std::fs::read_dir(&d).into_iter().flatten().flatten() { + let Ok(rd) = std::fs::read_dir(&d) else { return u64::MAX }; + for e in rd { + let Ok(e) = e else { return u64::MAX }; match e.metadata() { Ok(m) if m.is_dir() => stack.push(e.path()), Ok(m) => total = total.saturating_add(m.len()), - Err(_) => {} + Err(_) => return u64::MAX, } } } @@ -309,7 +311,10 @@ Tests that use a corpus skip cleanly when it is absent. } let mut tar = Command::new("tar"); tar.arg("-xzf").arg(&tgz).arg("-C").arg(&dest); - run(tar, "tar -xzf PngSuite-2017jul19.tgz")?; + if let Err(e) = run(tar, "tar -xzf PngSuite-2017jul19.tgz") { + let _ = std::fs::remove_dir_all(&dest); + return refuse(e); + } let _ = std::fs::remove_file(&tgz); if dir_bytes(&dest) > PNGSUITE_MAX_EXTRACTED { let _ = std::fs::remove_dir_all(&dest); From 4071bd40e6de7e8d656f30c0baedc87e1d9219e4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 4 Nov 2026 00:39:05 +0000 Subject: [PATCH 7/9] address review: skip a graphic whose PDF crop is too large to trace --- crates/engine/src/cmd/object.rs | 41 ++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/object.rs b/crates/engine/src/cmd/object.rs index aac0d0eb1..d4584b6c4 100644 --- a/crates/engine/src/cmd/object.rs +++ b/crates/engine/src/cmd/object.rs @@ -2836,7 +2836,12 @@ fn clipping_path(s: &mut Session, p: &Value) -> Result { let Some(px) = designcraft_render::decode_pixmap_page(&asset.data, asset.page) else { continue }; // A placed PDF cropped to a box: trace that box, which graphic space spans. let px = match designcraft_render::shown_box(asset) { - Some(frac) => designcraft_render::crop_pixmap(&px, frac).unwrap_or(px), + // A crop too big to trace is skipped, as a graphic that fails to decode is: tracing + // the whole page instead would trace something else than the box. + Some(frac) => match designcraft_render::crop_pixmap(&px, frac) { + Some(cropped) => cropped, + None => continue, + }, None => px, }; let (pw, ph) = (px.width() as usize, px.height() as usize); @@ -4637,6 +4642,40 @@ mod clipping_tests { assert!(s.execute("object.clippingPath", &json!({"ids": [f], "threshold": 255})).is_err()); } + /// A placed PDF whose crop box is too large to trace is skipped, not traced from the whole page. + #[test] + fn clipping_path_skips_a_pdf_whose_crop_is_too_large_to_trace() { + let content = "0 0 0 rg 10 10 50 50 re f"; + let objs = [ + "<>".to_string(), + "<>".into(), + "<>".into(), + format!("<>\nstream\n{content}\nendstream", content.len()), + ]; + let mut bytes = b"%PDF-1.7\n".to_vec(); + let mut offsets = Vec::new(); + for (i, body) in objs.iter().enumerate() { + offsets.push(bytes.len()); + bytes.extend_from_slice(format!("{} 0 obj\n{body}\nendobj\n", i + 1).as_bytes()); + } + let xref = bytes.len(); + bytes.extend_from_slice(b"xref\n0 5\n0000000000 65535 f\r\n"); + for at in offsets { + bytes.extend_from_slice(format!("{at:010} 00000 n\r\n").as_bytes()); + } + bytes.extend_from_slice(format!("trailer\n<>\nstartxref\n{xref}\n%%EOF\n").as_bytes()); + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let f = s.execute("frame.create", &json!({"rect": [100, 100, 200, 200]})).unwrap()["id"].as_u64().unwrap(); + let placed = s + .execute("file.place", &json!({"base64": super::super::file::base64_encode(&bytes), "name": "wide.pdf", "frame": f, "pdfCrop": "media"})); + assert!(placed.is_ok(), "{placed:?}"); + let before = s.doc().unwrap().doc.item(ItemId(f)).unwrap().shape; + let e = s.execute("object.clippingPath", &json!({"ids": [f], "type": "alpha"})); + assert!(e.is_err(), "the graphic is skipped, so there is nothing to trace: {e:?}"); + assert_eq!(s.doc().unwrap().doc.item(ItemId(f)).unwrap().shape, before, "the frame keeps its shape"); + } + #[test] fn clipping_path_options_report_invert_and_remove_the_clip() { let mut s = Session::new(); From 3326d919bb3a8c742627f708c84463d4cb0e144a Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Wed, 4 Nov 2026 03:49:15 +0000 Subject: [PATCH 8/9] Apply review fixes - crates/compose/src/tests.rs --- crates/compose/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index dfae2c2d4..05a0ad375 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -50,7 +50,7 @@ fn all_lines(cs: &ComposedStory) -> Vec<&Line> { /// kept as the breaker goes (it searched back over the whole run for every one). #[test] fn a_run_of_soft_hyphens_composes() { - let text = format!("Word{}end", "\u{AD}".repeat(40_000)); + let text = format!("Word{}end", "\u{AD}".repeat(3_900)); let (d, sid, _) = doc_with(&text, Rect::new(36.0, 36.0, 300.0, 700.0), ParaAttrs::default()); let t = std::time::Instant::now(); let cs = compose_story(&d, sid, &ComposeOptions::default()); From a2cc32a8d1898a532f1749f328e3d159e2b806b8 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Wed, 4 Nov 2026 03:50:09 +0000 Subject: [PATCH 9/9] Apply review fixes - crates/epub/src/tests.rs --- crates/epub/src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/epub/src/tests.rs b/crates/epub/src/tests.rs index 1fdd25157..62683f91a 100644 --- a/crates/epub/src/tests.rs +++ b/crates/epub/src/tests.rs @@ -26,8 +26,6 @@ fn exports_valid_package_in_reading_order() { assert!(z.by_name("OEBPS/nav.xhtml").is_ok()); } -/// #766: anchored graphics are exported where they are anchored (inline in the text, an -/// above-line object as a block of its own), and their images go into the package. /// Many footnotes and endnotes each export with their own text, in reference order. #[test] fn many_notes_keep_their_own_text() { @@ -58,6 +56,8 @@ fn a_huge_title_is_capped() { assert!(s.contains(&"T".repeat(1024)) && !s.contains(&"T".repeat(1025)), "{}", s.len()); } +/// #766: anchored graphics are exported where they are anchored (inline in the text, an +/// above-line object as a block of its own), and their images go into the package. #[test] fn anchored_objects_are_exported_in_place() { let mut d = Document::new(&NewDocument::default());