From c0ef994b263c86cfa308dc1af75543a7660d29f2 Mon Sep 17 00:00:00 2001 From: grabowski Date: Mon, 23 Nov 2026 10:35:33 +0700 Subject: [PATCH] Pack: a PDF whose pages cannot be copied gets a note page The second fuzz run on cnx01 found a PDF (a page's font dictionary with a key and a broken value) on which the PDF reader, hayro-syntax 0.7.2 (Dict::entries, an unwrap), panics while the page is copied. The app lives on (the pack renders in spawn_blocking), but the person's pack and the card's sides failed with a 500 every time, for as long as the file was among their documents. piece() now copies an uploaded PDF's pages once into a throwaway document with panics caught (pack::pdf::copies); a file that panics prints as a note page like any unreadable file. Only a panic counts, so a PDF 2.0 file still goes to render's retry. tests/fuzz_cases/upload/pdf-dict-key-without-value.pdf is the finding, replayed on every push (it panicked on stable too). --- src/pack/pdf.rs | 24 +++++++++++++++++- tests/fuzz_cases/upload/README.md | 6 +++++ .../upload/pdf-dict-key-without-value.pdf | Bin 0 -> 1211 bytes 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/fuzz_cases/upload/pdf-dict-key-without-value.pdf diff --git a/src/pack/pdf.rs b/src/pack/pdf.rs index 5b7cb94..369c0b7 100644 --- a/src/pack/pdf.rs +++ b/src/pack/pdf.rs @@ -118,10 +118,15 @@ fn piece(what: &str, file: Option) -> Piece { Ok(pdf) => { let sizes: Vec<(f32, f32)> = pdf.pages().iter().map(|p| p.render_dimensions()).collect(); + let doc = PdfDocument::new(Arc::new(pdf)); if sizes.is_empty() { Piece::Note(format!("{what}: {name} has no pages.")) + } else if !copies(&doc, &sizes) { + Piece::Note(format!( + "{what}: {name} is a PDF the app cannot copy pages from. {later}" + )) } else { - Piece::Pdf(PdfDocument::new(Arc::new(pdf)), sizes) + Piece::Pdf(doc, sizes) } } Err(e) => Piece::Note(format!( @@ -137,6 +142,23 @@ fn piece(what: &str, file: Option) -> Piece { } } +/// Whether the pages of an uploaded PDF can be copied: tried once into a +/// throwaway document. The PDF reader panics on some broken files (the +/// nightly fuzzing found a font dictionary with a key and no value), which +/// would fail the whole pack, every time, for as long as the file is there; +/// such a file gets a note page instead. Only a panic counts: an error from +/// `finish` (a PDF 2.0 file in a 1.7 document) is for `render` to handle. +fn copies(pdf: &PdfDocument, sizes: &[(f32, f32)]) -> bool { + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let mut doc = Document::new(); + for (k, &(w, h)) in sizes.iter().enumerate() { + pdf_page(&mut doc, pdf, k, w, h); + } + let _ = doc.finish(); + })) + .is_ok() +} + /// An image to print: the picture, how to turn it upright, its pixels' /// size on paper and the box its kind prints in. struct Shot { diff --git a/tests/fuzz_cases/upload/README.md b/tests/fuzz_cases/upload/README.md index 2626f4f..7534459 100644 --- a/tests/fuzz_cases/upload/README.md +++ b/tests/fuzz_cases/upload/README.md @@ -1,2 +1,8 @@ Inputs that once broke the fuzz target `upload` (src/fuzzing.rs), replayed by `tests/fuzz_cases.rs`. Name each after what it broke. + +- `pdf-dict-key-without-value.pdf`: a page whose font dictionary has a key + and a broken value (found Nov 23, 2026). The PDF reader (hayro-syntax 0.7.2, + `Dict::entries`) panicked while the page was copied, so the pack failed + with a 500 every time; `pack::pdf::copies` now tries the pages once and + the file gets a note page. diff --git a/tests/fuzz_cases/upload/pdf-dict-key-without-value.pdf b/tests/fuzz_cases/upload/pdf-dict-key-without-value.pdf new file mode 100644 index 0000000000000000000000000000000000000000..63b76cf57216e41c766398ac971d584a216726aa GIT binary patch literal 1211 zcmY!laBY*?ioSDxX_8Nfgx2*q%aPISZd zeN$616P@xaq78tu7A8gtrk19$`a$_6i6yBD2Kqs%#rdU0$w04wtO){ot0)!d7Kp!` zfz|@sR=v(or~_b01r*v)ANi!_ft+q)ir?jP9&0@@ zGz>7FLr4x7FHCcrf~t&2$umo zxf1CHt^rPYNU?_{3=WYO2nH~qU}kD+Y^so^zy%Wl<{~gtArCHQXavk(Ab>1pj;YSb z*uW5^3R#`85is={xjI>xJDIy!07)|!Lqj`IMk_8!EGhxVi@AXzm#V6(zZ(|-VX>wM literal 0 HcmV?d00001