Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

PDF: a null OCProperties is no optional content; an unreadable Prev is damage

KnottyDyes4c580086a0ad
diff --git a/crates/pdf/src/optional_content.rs b/crates/pdf/src/optional_content.rs
index 0dd0c4879..3a9e09289 100644
--- a/crates/pdf/src/optional_content.rs
+++ b/crates/pdf/src/optional_content.rs
@@ -123,6 +123,9 @@ fn append(bytes: &[u8], changes: &[(ObjectIdentifier, Vec<u8>)], root: ObjectIde
return Err(invalid());
}
offset = section.get::<usize>(b"Prev");
+ if offset.is_none() && has_entry(&section, b"Prev") {
+ return Err(damaged());
+ }
}
if offset.is_some() {
return Err(damaged());
@@ -180,7 +183,8 @@ pub(crate) fn prepare(bytes: &Arc<Vec<u8>>, choice: Option<&[String]>) -> Result
let pdf = Pdf::new(bytes.clone()).map_err(|_| invalid())?;
let xref = pdf.xref();
let root = xref.get::<Dict<'_>>(xref.root_id()).ok_or_else(invalid)?;
- if !root.contains_key(b"OCProperties") {
+ // A null value, literal or referenced, is the same as no entry.
+ if matches!(root.get::<Object<'_>>(b"OCProperties"), None | Some(Object::Null(_))) {
let has_optional_content = pdf
.objects()
.into_iter()
@@ -433,6 +437,16 @@ mod tests {
assert!(has_entry(&trailer_at(bytes, 0).unwrap(), b"Encrypt"));
}
+ #[test]
+ fn an_unreadable_prev_is_damage_and_a_null_one_ends_the_chain() {
+ for value in ["(12)", "/Twelve", "-1", "[12]", "<<>>", "true", "9 0 R"] {
+ let bytes = default_hidden(&format!("<</Size 5/Root 1 0 R/Prev {value}>>"), None);
+ let error = update(&bytes).unwrap_err().to_string();
+ assert!(error.contains("cross-reference"), "{value}: {error}");
+ }
+ assert!(update(&default_hidden("<</Size 5/Root 1 0 R/Prev null>>", None)).is_ok());
+ }
+
#[test]
fn a_cyclic_prev_chain_is_damage() {
let mut bytes = default_hidden("<</Size 5/Root 1 0 R>>", None).to_vec();
diff --git a/crates/pdf/src/registration_tests.rs b/crates/pdf/src/registration_tests.rs
index d87c4c838..278100304 100644
--- a/crates/pdf/src/registration_tests.rs
+++ b/crates/pdf/src/registration_tests.rs
@@ -137,6 +137,25 @@ fn unlayered_placed_pdf_skips_optional_content_registration() {
check_unlayered(&placed_document(unlayered_source()), true);
}
+#[test]
+fn null_optional_content_properties_are_no_optional_content() {
+ let content = "0.125 0.625 0.875 rg 0 0 100 100 re f";
+ for (properties, extra) in [("/OCProperties null", None), ("/OCProperties 5 0 R", Some("null"))] {
+ let mut objects = vec![
+ format!("<</Type/Catalog/Pages 2 0 R{properties}>>"),
+ "<</Type/Pages/Kids[3 0 R]/Count 1>>".into(),
+ "<</Type/Page/Parent 2 0 R/MediaBox[0 0 100 100]/Resources<<>>/Contents 4 0 R>>".into(),
+ format!("<</Length {}>>\nstream\n{content}\nendstream", content.len()),
+ ];
+ objects.extend(extra.map(String::from));
+ let doc = placed_document(assemble(&objects));
+ for standard in [Standard::None, Standard::PdfX4] {
+ let bytes = export_pdf(&doc, &Cache::new(), &PdfOptions { standard, ..Default::default() }).unwrap();
+ check_unlayered_pdf(&bytes, 1, true);
+ }
+ }
+}
+
#[test]
fn layered_and_resource_only_inputs_register_optional_content() {
for hidden in [true, false] {