Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Issue #2367: an object stream whose entry cannot place it is found by its header (#2398)

wstock72dac33f8e82
diff --git a/crates/cos/src/document.rs b/crates/cos/src/document.rs
index f12f539da..e94355590 100644
--- a/crates/cos/src/document.rs
+++ b/crates/cos/src/document.rs
@@ -1153,7 +1153,19 @@ impl Document {
if let Some(s) = self.objstms.lock().map_err(|_| CosError::Poisoned)?.get(&num) {
return Ok(s.clone());
}
- let Object::Stream(s) = &*self.try_get(num)? else { return Err(CosError::MissingObject(num)) };
+ // A file entry that cannot place the stream (one that says it is itself compressed
+ // inside another stream, as in #2367, or points nowhere) is tried by the stream's own
+ // header, as a plain object with a wrong offset is. An edited or freed stream, in the
+ // overlay or in a saved revision's table, is what that says it is: the file's copy
+ // must not come back.
+ let loaded = match self.try_get(num) {
+ Ok(o) if matches!(*o, Object::Stream(_)) => o,
+ _ if self.overlay.contains_key(&num) || matches!(self.xref_entry(num), Some(XrefEntry::Free { .. })) => {
+ return Err(CosError::MissingObject(num));
+ }
+ _ => Arc::new(self.scan_for(num).map(|(id, o)| self.decrypted(id, o)).ok_or(CosError::MissingObject(num))?),
+ };
+ let Object::Stream(s) = &*loaded else { return Err(CosError::MissingObject(num)) };
let n = s.dict.int(b"N").unwrap_or(0).clamp(0, 1_000_000) as usize;
let first = s.dict.int(b"First").unwrap_or(0).max(0) as usize;
let data = s.decoded_within(self.stream_limit)?;
@@ -2597,6 +2609,93 @@ mod tests {
assert!(old.get(ObjRef::new(1, 0)).as_dict().unwrap().get(b"Lang").is_none());
}
+ /// qpdf's `xref-compressed-in-compressed.pdf` in miniature: object 3 is an object stream
+ /// holding object 4, and the cross-reference stream says object 3 itself lives in stream 99.
+ fn compressed_in_compressed_fixture() -> Document {
+ let mut out = b"%PDF-1.5\n".to_vec();
+ let at = |out: &mut Vec<u8>, num: u32, body: &str| {
+ let off = out.len();
+ out.extend_from_slice(format!("{num} 0 obj\n{body}\nendobj\n").as_bytes());
+ off as u16
+ };
+ let o1 = at(&mut out, 1, "<< /Type /Catalog /Pages 2 0 R /Outlines 4 0 R >>");
+ let o2 = at(&mut out, 2, "<< /Type /Pages /Kids [] /Count 0 >>");
+ let inner = "<< /Type /Outlines /Count 0 >>";
+ at(&mut out, 3, &format!("<< /Type /ObjStm /N 1 /First 4 /Length {} >>\nstream\n4 0 {inner}\nendstream", 4 + inner.len()));
+ // /W [1 2 1]: object 3 is said to live in stream 99 (which does not exist), 4 in stream 3.
+ let rows: Vec<[u8; 4]> = vec![
+ [0, 0, 0, 255],
+ [1, 0, o1 as u8, 0],
+ [1, 0, o2 as u8, 0],
+ [2, 0, 99, 0],
+ [2, 0, 3, 0],
+ [1, (out.len() >> 8) as u8, out.len() as u8, 0],
+ ];
+ let rows: Vec<u8> = rows.concat();
+ let xref = out.len();
+ out.extend_from_slice(
+ format!("5 0 obj\n<< /Type /XRef /W [1 2 1] /Index [0 6] /Size 6 /Root 1 0 R /Length {} >>\nstream\n", rows.len()).as_bytes(),
+ );
+ out.extend_from_slice(&rows);
+ out.extend_from_slice(format!("\nendstream\nendobj\nstartxref\n{xref}\n%%EOF\n").as_bytes());
+ Document::open(Arc::new(out)).unwrap()
+ }
+
+ /// An object stream whose own cross-reference entry says it is compressed inside another
+ /// stream (which the format forbids, and which does not exist here: qpdf's
+ /// `xref-compressed-in-compressed.pdf`, #2367) could not be loaded, so every object in it
+ /// read as null and a full save wrote `/Outlines null`. The stream is now found by its own
+ /// header, as a plain object with a wrong offset is.
+ #[test]
+ fn an_object_stream_marked_as_compressed_is_found_by_its_header() {
+ let doc = compressed_in_compressed_fixture();
+ assert_eq!(doc.revisions().len(), 1, "the chain itself reads: {:?}", doc.repair_log());
+ let outlines = doc.get(ObjRef::new(4, 0));
+ assert_eq!(outlines.as_dict().and_then(|d| d.name(b"Type")), Some(&b"Outlines"[..]), "{outlines:?}");
+ let saved = crate::write_full(&doc, &crate::SaveOptions::default()).unwrap();
+ let back = Document::open(Arc::new(saved)).unwrap();
+ let catalog = back.get(back.root().unwrap());
+ let outlines = back.resolve(catalog.as_dict().unwrap().get(b"Outlines").unwrap());
+ assert_eq!(outlines.as_dict().and_then(|d| d.name(b"Type")), Some(&b"Outlines"[..]), "the full save keeps it: {outlines:?}");
+ }
+
+ /// The header fallback is for file entries only: once the stream is freed (or replaced) in
+ /// the overlay, its objects must not come back from the file's copy of it.
+ #[test]
+ fn a_freed_object_stream_is_not_found_by_its_header() {
+ let doc = compressed_in_compressed_fixture();
+ assert!(doc.get(ObjRef::new(4, 0)).as_dict().is_some());
+ let mut edited = doc.clone();
+ edited.free(ObjRef::new(3, 0));
+ let fresh = edited.object_reader().document;
+ assert!(matches!(*fresh.get(ObjRef::new(4, 0)), Object::Null), "{:?}", fresh.get(ObjRef::new(4, 0)));
+ let mut replaced = doc.clone();
+ replaced.set(ObjRef::new(3, 0), Object::Int(0));
+ let fresh = replaced.object_reader().document;
+ assert!(matches!(*fresh.get(ObjRef::new(4, 0)), Object::Null), "{:?}", fresh.get(ObjRef::new(4, 0)));
+ }
+
+ /// The same once the free is saved: the revision's table says the stream is free, and
+ /// neither reading the file back nor rewriting it in full brings its objects back.
+ #[test]
+ fn a_container_freed_in_a_saved_revision_is_not_found_by_its_header() {
+ let mut edited = compressed_in_compressed_fixture();
+ edited.free(ObjRef::new(3, 0));
+ let incremental = crate::write_incremental(&edited, &crate::SaveOptions::default()).unwrap();
+ let reopened = Document::open(Arc::new(incremental)).unwrap();
+ assert_eq!(reopened.revisions().len(), 2);
+ assert!(matches!(reopened.xref_entry(3), Some(XrefEntry::Free { .. })));
+ assert!(matches!(*reopened.get(ObjRef::new(4, 0)), Object::Null), "{:?}", reopened.get(ObjRef::new(4, 0)));
+ for object_streams in [true, false] {
+ let opts = crate::SaveOptions { object_streams, ..Default::default() };
+ let full = crate::write_full(&reopened, &opts).unwrap();
+ let back = Document::open(Arc::new(full)).unwrap();
+ let catalog = back.get(back.root().unwrap());
+ let outlines = catalog.as_dict().unwrap().get(b"Outlines").map(|o| back.resolve(o));
+ assert!(outlines.as_ref().is_none_or(|o| matches!(**o, Object::Null)), "the full save keeps it out: {outlines:?}");
+ }
+ }
+
#[test]
fn damaged_xref_is_reconstructed() {
let mut bytes = build(&["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [] /Count 0 >>"], "/Root 1 0 R");