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 0000000..63b76cf Binary files /dev/null and b/tests/fuzz_cases/upload/pdf-dict-key-without-value.pdf differ