Skip to content

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

Issue #774: keep the catalog out of object streams in encrypted files (Acrobat) (#836)

Tristan von Kries010b67782971
diff --git a/crates/cos/src/writer.rs b/crates/cos/src/writer.rs
index c8e2e0040..26dcfc46f 100644
--- a/crates/cos/src/writer.rs
+++ b/crates/cos/src/writer.rs
@@ -284,13 +284,18 @@ pub fn write_full(doc: &Document, opts: &SaveOptions) -> Result<Vec<u8>, CosErro
collect_refs(&reader.get(r), &mut |x| pending.push(x));
}
}
+ // Acrobat refuses an encrypted file whose catalog sits in an object stream ("the file is
+ // damaged and could not be repaired", before it even asks for the password), while other
+ // readers accept it. qpdf keeps the catalog out of object streams too. Verified with
+ // Acrobat DC on macOS (#774).
+ let root_outside = if reader.document.output_security().0.is_some() { trailer_in.reference(b"Root") } else { None };
let mut rows: BTreeMap<u32, Row> = BTreeMap::new();
// Keep only references, not renamed copies of the entire object graph. Standalone
// objects still precede object streams, preserving the established output order.
let mut packed: Vec<(u32, ObjRef)> = Vec::new();
for (i, (r, is_stream)) in order.iter().enumerate() {
let num = output_number(i)?;
- let in_stream = opts.object_streams && !is_stream && !encryption_objects.contains(r);
+ let in_stream = opts.object_streams && !is_stream && !encryption_objects.contains(r) && root_outside != Some(*r);
if in_stream {
packed.push((num, *r));
} else {
diff --git a/crates/cos/tests/writer.rs b/crates/cos/tests/writer.rs
index 4be398bf2..448cf3577 100644
--- a/crates/cos/tests/writer.rs
+++ b/crates/cos/tests/writer.rs
@@ -152,6 +152,35 @@ fn qpdf_accepts_object_stream_output() {
assert!(check.status.success() && report.contains("No syntax or stream encoding errors"), "{report}");
}
+#[test]
+fn the_catalog_of_an_encrypted_document_stays_outside_object_streams() {
+ // Acrobat reports an encrypted file as damaged when its catalog is in an object stream,
+ // before asking for the password (#774). Unencrypted output still packs the catalog.
+ fn root_is_packed(bytes: &[u8], password: Option<&str>) -> bool {
+ let doc = Document::open_with_password(Arc::new(bytes.to_vec()), password).unwrap();
+ let root = doc.trailer().reference(b"Root").unwrap();
+ let direct = format!("\n{} 0 obj", root.num);
+ !bytes.windows(direct.len()).any(|w| w == direct.as_bytes())
+ }
+ let plain = Document::open(Arc::new(fixture())).unwrap();
+ assert!(root_is_packed(&write_full(&plain, &SaveOptions::default()).unwrap(), None), "unencrypted: catalog in an object stream");
+ let mut enc = plain.clone();
+ enc.set_encryption(&pdfcraft_cos::NewEncryption {
+ algorithm: pdfcraft_cos::Algorithm::Aes256,
+ user_password: "user",
+ owner_password: "owner",
+ permissions: -4,
+ encrypt_metadata: true,
+ seed: [7; 32],
+ })
+ .unwrap();
+ let packed = write_full(&enc, &SaveOptions::default()).unwrap();
+ assert!(!root_is_packed(&packed, Some("user")), "encrypted: catalog must be a standalone object");
+ let back = Document::open_with_password(Arc::new(packed.clone()), Some("user")).expect("reopens");
+ assert!(back.repair_log().is_empty(), "{:?}", back.repair_log());
+ assert_eq!(title(&back), b"Caf\xe9 (draft)");
+}
+
#[test]
fn objects_referenced_by_the_encrypt_dictionary_stay_outside_object_streams() {
// Encrypt a document, then move its crypt filters into indirect objects, as some producers