From 4485040ca751add724bc4b6fbe8ff2b425d249f5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 12:55:52 +0000 Subject: [PATCH 01/14] fix(xfdf): bound field text, look up names, guard tab cells, protect fields XFDF import built every full field name from its parent's with no total; it is held to 64 MiB of names and values. FDF field arrays charge each decoded value before keeping it instead of after decoding all of them. merge_csv, XFDF field export and apply_values looked names up by linear scan per field; they use maps, keeping first-match behaviour. Tab-delimited export guards formula-like cells with a leading ' as CSV does, and import takes it off. A same-name comment import no longer replaces a widget whose subtype is a reference or whose /FT is inherited from its parent, existing annotations are borrowed instead of deep-copied per import, and an imported redaction mark's /Rect is grown to hold its /QuadPoints so nothing is removed outside its review outline. --- crates/xfdf/src/common.rs | 17 ++++++++- crates/xfdf/src/fdf.rs | 25 +++++++++---- crates/xfdf/src/lib.rs | 20 +++++++--- crates/xfdf/src/tests.rs | 77 +++++++++++++++++++++++++++++++++++++++ crates/xfdf/src/xfdf.rs | 63 +++++++++++++++++++++++++++----- 5 files changed, 178 insertions(+), 24 deletions(-) diff --git a/crates/xfdf/src/common.rs b/crates/xfdf/src/common.rs index 488277dc19..1968cd0e85 100644 --- a/crates/xfdf/src/common.rs +++ b/crates/xfdf/src/common.rs @@ -8,6 +8,9 @@ use super::{DataError, Dict, Document, Object}; const MAX_NODES: u32 = 2_000_000; pub(crate) const MAX_DEPTH: usize = 64; +/// Most bytes of full field names and values one XFDF import expands to: a long parent name is +/// written into every descendant's full name, so a small file can name far more than it holds. +pub(crate) const MAX_XFDF_TEXT: usize = 64 << 20; /// The deepest element nesting accepted before the text reaches the XML parser. /// @@ -240,7 +243,13 @@ pub(crate) fn xml_name(name: &str) -> String { s } -pub(crate) fn walk_xfdf(node: roxmltree::Node, prefix: &str, out: &mut Vec<(String, Vec)>, depth: usize) -> Result<(), DataError> { +pub(crate) fn walk_xfdf( + node: roxmltree::Node, + prefix: &str, + out: &mut Vec<(String, Vec)>, + depth: usize, + bytes: &mut usize, +) -> Result<(), DataError> { if depth > MAX_DEPTH { return Err(DataError::Malformed(format!("XFDF fields nest more than {MAX_DEPTH} deep"))); } @@ -258,9 +267,13 @@ pub(crate) fn walk_xfdf(node: roxmltree::Node, prefix: &str, out: &mut Vec<(Stri // leaf holding only `` is not read yet, so it leaves the field alone. let names_kids_or_rich = f.children().any(|c| c.has_tag_name("field") || c.has_tag_name("value-richtext")); if !vals.is_empty() || !names_kids_or_rich { + *bytes = bytes.saturating_add(full.len()).saturating_add(vals.iter().map(String::len).fold(0, usize::saturating_add)); + if *bytes > MAX_XFDF_TEXT { + return Err(DataError::Malformed(format!("the file expands to more than {} MiB of field names and values", MAX_XFDF_TEXT >> 20))); + } out.push((full.clone(), vals)); } - walk_xfdf(f, &full, out, depth + 1)?; + walk_xfdf(f, &full, out, depth + 1, bytes)?; } Ok(()) } diff --git a/crates/xfdf/src/fdf.rs b/crates/xfdf/src/fdf.rs index ceb4ad99eb..3fe90f32f3 100644 --- a/crates/xfdf/src/fdf.rs +++ b/crates/xfdf/src/fdf.rs @@ -419,16 +419,27 @@ pub(crate) fn walk_fdf( let full = if prefix.is_empty() { t } else { format!("{prefix}.{t}") }; budget.spend(0, full.len())?; if let Some(v) = d.get(b"V").map(|v| fdf.resolve(v)) { - let vals: Vec = match &*v { - Object::String(s) => vec![s.to_text()], - Object::Name(n) => vec![pdfcraft_forms::name_text(n)], + // Each value is charged as it is decoded, before the next: an array of references to + // one long string is not decoded whole and then refused. + let mut vals: Vec = Vec::new(); + let keep = |vals: &mut Vec, s: String, budget: &mut ImportBudget| -> Result<(), DataError> { + budget.spend(1, s.len())?; + vals.push(s); + Ok(()) + }; + match &*v { + Object::String(s) => keep(&mut vals, s.to_text(), budget)?, + Object::Name(n) => keep(&mut vals, pdfcraft_forms::name_text(n), budget)?, Object::Array(a) => { budget.spend(a.len(), 0)?; - a.iter().filter_map(|x| fdf.resolve(x).as_string().map(|s| s.to_text())).collect() + for x in a { + if let Some(s) = fdf.resolve(x).as_string() { + keep(&mut vals, s.to_text(), budget)?; + } + } } - _ => Vec::new(), - }; - budget.spend(vals.len(), vals.iter().map(String::len).fold(0, usize::saturating_add))?; + _ => {} + } out.push((full.clone(), vals)); } if let Some(kids) = d.get(b"Kids").map(|k| fdf.resolve(k)) diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rs index 16c554d72f..a5f8f7f12c 100644 --- a/crates/xfdf/src/lib.rs +++ b/crates/xfdf/src/lib.rs @@ -124,7 +124,11 @@ pub fn export_data(doc: &Document, format: Format) -> String { ) } _ => { - let clean = |s: &str| s.replace(['\t', '\n', '\r'], " "); + // A formula-like cell gets a leading `'`, as in CSV; `tab_delimited` takes it off. + let clean = |s: &str| { + let s = if csv_guarded(s) { format!("'{s}") } else { s.to_string() }; + s.replace(['\t', '\n', '\r'], " ") + }; format!( "{}\n{}\n", fields.iter().map(|f| clean(&f.0)).collect::>().join("\t"), @@ -171,7 +175,7 @@ pub fn data_values(bytes: &[u8]) -> Result)>, DataError } let mut values = Vec::new(); if let Some(fields) = root.children().find(|c| c.has_tag_name("fields")) { - walk_xfdf(fields, "", &mut values, 0)?; + walk_xfdf(fields, "", &mut values, 0, &mut 0)?; } Ok(values) } @@ -184,9 +188,10 @@ pub fn data_values(bytes: &[u8]) -> Result)>, DataError /// is written with a leading `'`, which keeps it text; a plain number such as `-12.5` is not. pub fn merge_csv(files: &[Vec<(String, Vec)>]) -> String { let mut columns: Vec<&str> = Vec::new(); + let mut known: HashSet<&str> = HashSet::new(); for f in files { for (k, _) in f { - if !columns.contains(&k.as_str()) { + if known.insert(k.as_str()) { columns.push(k); } } @@ -199,7 +204,12 @@ pub fn merge_csv(files: &[Vec<(String, Vec)>]) -> String { let mut out = columns.iter().map(|c| q(c)).collect::>().join(","); out.push('\n'); for f in files { - let row: Vec = columns.iter().map(|c| f.iter().find(|(k, _)| k == c).map_or(String::new(), |(_, v)| q(&v.join(", ")))).collect(); + // The first value of a name repeated in one file wins, as the scan it replaces found it. + let mut by_name: HashMap<&str, &Vec> = HashMap::new(); + for (k, v) in f { + by_name.entry(k.as_str()).or_insert(v); + } + let row: Vec = columns.iter().map(|c| by_name.get(c).map_or(String::new(), |v| q(&v.join(", ")))).collect(); out.push_str(&row.join(",")); out.push('\n'); } @@ -326,7 +336,7 @@ fn import_table(doc: &mut Document, text: &str) -> Result { fn tab_delimited(t: &str) -> Result)>, DataError> { let mut lines = t.lines(); let (Some(head), Some(row)) = (lines.next(), lines.next()) else { return Err(DataError::UnknownFormat) }; - Ok(head.split('\t').map(str::to_string).zip(row.split('\t')).map(|(k, v)| (k, vec![v.to_string()])).collect()) + Ok(head.split('\t').map(|k| csv_text(k.to_string())).zip(row.split('\t')).map(|(k, v)| (k, vec![csv_text(v.to_string())])).collect()) } /// Import comments and/or field values from XFDF, FDF, XML, CSV or tab-delimited text diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rs index 0da17c1e67..341de91c1c 100644 --- a/crates/xfdf/src/tests.rs +++ b/crates/xfdf/src/tests.rs @@ -2099,3 +2099,80 @@ fn an_fdf_comment_popup_without_a_rect_is_dropped() { let popups = page.iter().filter(|a| dst.resolve(a).as_dict().is_some_and(|d| d.name(b"Subtype") == Some(b"Popup".as_slice()))).count(); assert_eq!(popups, 0); } + +/// Tab-delimited export guards a formula-like cell as CSV does, and the import takes the guard +/// off, so a spreadsheet never runs form data and the value survives the round trip. +#[test] +fn tab_delimited_export_guards_formula_cells_and_round_trips() { + let mut doc = blank(); + set_value(&mut doc, "Name", &FieldValue::Text("=SUM(A1:A9)".into())).unwrap(); + let text = export_data(&doc, Format::Txt); + let row = text.lines().nth(1).unwrap(); + assert!(row.starts_with("'=SUM(A1:A9)"), "{row:?}"); + let mut again = blank(); + import(&mut again, text.as_bytes()).unwrap(); + assert_eq!(values(&again).iter().find(|(k, _)| k == "Name").map(|(_, v)| v.clone()), Some(vec!["=SUM(A1:A9)".to_string()])); +} + +/// Merging many files with many distinct names stays linear: every column once, in first-seen +/// order, and a row looks its names up rather than scanning them. +#[test] +fn merge_csv_lists_each_column_once_in_order() { + let files: Vec)>> = + (0..3).map(|f| (0..4).map(|i| (format!("c{}", (i + f) % 5), vec![format!("{f}-{i}")])).collect()).collect(); + let csv = merge_csv(&files); + let mut lines = csv.lines(); + assert_eq!(lines.next(), Some("c0,c1,c2,c3,c4")); + assert_eq!(lines.next(), Some("0-0,0-1,0-2,0-3,")); + assert_eq!(lines.next(), Some(",1-0,1-1,1-2,1-3")); + assert_eq!(lines.next(), Some("2-3,,2-0,2-1,2-2")); +} + +/// A long parent name is written into every descendant's full name: past the import's text +/// bound the file is refused rather than expanded. +#[test] +fn xfdf_field_names_past_the_text_bound_are_refused() { + let parent = "p".repeat(1 << 20); + let kids: String = (0..100).map(|i| format!("v")).collect(); + let xfdf = + format!("{kids}"); + let mut doc = blank(); + match import(&mut doc, xfdf.as_bytes()) { + Err(DataError::Malformed(m)) => assert!(m.contains("64 MiB"), "{m}"), + other => panic!("{other:?}"), + } +} + +/// A same-name import never replaces a widget whose field type is inherited, or whose subtype is +/// written as a reference: the field keeps its dictionary and the import goes beside it. +#[test] +fn same_name_import_keeps_a_widget_with_an_inherited_field_type() { + let mut doc = blank(); + let page = pdfcraft_model::pages(&doc)[0].obj; + // A field with one widget kid: /FT on the field, the widget names it as /Parent. + let mut field = Dict::new(); + field.set(b"FT".to_vec(), Object::name("Tx")); + field.set(b"T".to_vec(), Object::String(PdfString::text("inherited"))); + let field = doc.add(field); + let subtype = doc.add(Object::name("Widget")); + let mut widget = Dict::new(); + widget.set(b"Type".to_vec(), Object::name("Annot")); + widget.set(b"Subtype".to_vec(), Object::Ref(subtype)); + widget.set(b"Rect".to_vec(), arr(&[10.0, 10.0, 60.0, 30.0])); + widget.set(b"NM".to_vec(), Object::String(PdfString::text("w1"))); + widget.set(b"Parent".to_vec(), Object::Ref(field)); + let widget = doc.add(widget); + doc.update_dict(field, |d| d.set(b"Kids".to_vec(), Object::Array(vec![Object::Ref(widget)]))).unwrap(); + doc.update_dict(page, |p| p.set(b"Annots".to_vec(), Object::Array(vec![Object::Ref(widget)]))).unwrap(); + let mut note = Dict::new(); + note.set(b"Type".to_vec(), Object::name("Annot")); + note.set(b"Subtype".to_vec(), Object::name("Text")); + note.set(b"Rect".to_vec(), arr(&[10.0, 10.0, 30.0, 30.0])); + note.set(b"NM".to_vec(), Object::String(PdfString::text("w1"))); + let added = xfdf::place(&mut doc, page, note).unwrap(); + assert_ne!(added, widget); + assert_eq!(doc.get(widget).as_dict().unwrap().name(b"Type"), Some(b"Annot".as_slice())); + assert!(doc.get(widget).as_dict().unwrap().reference(b"Parent").is_some(), "the widget was replaced"); + let annots = doc.get(page).as_dict().unwrap().get(b"Annots").unwrap().as_array().unwrap().len(); + assert_eq!(annots, 2); +} diff --git a/crates/xfdf/src/xfdf.rs b/crates/xfdf/src/xfdf.rs index 06ad9f34ec..a9831ca331 100644 --- a/crates/xfdf/src/xfdf.rs +++ b/crates/xfdf/src/xfdf.rs @@ -148,6 +148,8 @@ fn xfdf_fields(doc: &Document, out: &mut String) { struct Node { values: Vec, kids: Vec<(String, Node)>, + /// Each child's place in `kids`, by name: a wide level is not scanned per field. + index: std::collections::HashMap, } let mut root = Node::default(); for f in pdfcraft_forms::fields(doc) { @@ -162,10 +164,11 @@ fn xfdf_fields(doc: &Document, out: &mut String) { // the last level stay together in the deepest element's name, and the import joins them // back with periods into the same full name. for part in f.name.splitn(MAX_DEPTH, '.') { - let i = match node.kids.iter().position(|(k, _)| k == part) { - Some(i) => i, + let i = match node.index.get(part) { + Some(&i) => i, None => { node.kids.push((part.to_string(), Node::default())); + node.index.insert(part.to_string(), node.kids.len() - 1); node.kids.len() - 1 } }; @@ -214,8 +217,13 @@ pub fn export_xfdf(doc: &Document, comments: bool, fields: bool, file: &str) -> /// Set the values of fields named in `values` (full names), counting what took. pub(crate) fn apply_values(doc: &mut Document, values: &[(String, Vec)], report: &mut Report) { let all = pdfcraft_forms::fields(doc); + // The first field of a name, as the scan it replaces found it. + let mut by_name: std::collections::HashMap<&str, usize> = std::collections::HashMap::new(); + for (i, f) in all.iter().enumerate() { + by_name.entry(f.name.as_str()).or_insert(i); + } for (name, vals) in values { - let Some(f) = all.iter().find(|f| &f.name == name) else { continue }; + let Some(f) = by_name.get(name.as_str()).and_then(|&i| all.get(i)) else { continue }; let first = vals.first().cloned().unwrap_or_default(); let v = match f.kind { FieldKind::Text => FieldValue::Text(first), @@ -369,7 +377,27 @@ pub(crate) const LOCKED: i64 = 128; /// widget or field (replacing it would set the field's value and policy around the form setter). fn import_protected(doc: &Document, x: &Dict) -> bool { let flags = x.get(b"F").and_then(|f| doc.resolve(f).as_int()).unwrap_or(0); - flags & LOCKED != 0 || matches!(x.name(b"Subtype"), Some(b"Redact" | b"Widget")) || x.contains(b"FT") + let subtype = x.get(b"Subtype").map(|s| doc.resolve(s)); + flags & LOCKED != 0 || matches!(subtype.as_deref().and_then(Object::as_name), Some(b"Redact" | b"Widget")) || is_field(doc, x) +} + +/// Whether `x` is a form field or a widget of one: it has `/FT`, or a `/Parent` up the field +/// tree does (a widget inherits its field's type). +fn is_field(doc: &Document, x: &Dict) -> bool { + if x.contains(b"FT") { + return true; + } + let mut parent = x.reference(b"Parent"); + let mut seen = std::collections::HashSet::new(); + while let Some(r) = parent.filter(|r| seen.len() < 32 && seen.insert(*r)) { + let o = doc.get(r); + let Some(d) = o.as_dict() else { return false }; + if d.contains(b"FT") { + return true; + } + parent = d.reference(b"Parent"); + } + false } pub(crate) fn place(doc: &mut Document, page_ref: ObjRef, mut d: Dict) -> Result { @@ -389,7 +417,19 @@ pub(crate) fn place(doc: &mut Document, page_ref: ObjRef, mut d: Dict) -> Result let (quads, rest) = v.as_chunks::<8>(); v.len() == q.len() && rest.is_empty() && !quads.is_empty() && quads.iter().all(quad_has_area) }); - if !quads_drawable { + if quads_drawable { + // The rectangle holds all the mark removes: its review outline is clipped to the + // rectangle, so a quad outside it would be removed unseen. + let v: Vec = d.get(b"QuadPoints").and_then(Object::as_array).into_iter().flatten().filter_map(Object::as_f64).collect(); + let mut r = rect_strict(doc, d.get(b"Rect")) + .map_or([f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY], |[x0, y0, x1, y1]| { + [x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)] + }); + for [x, y] in v.as_chunks::<2>().0 { + r = [r[0].min(*x), r[1].min(*y), r[2].max(*x), r[3].max(*y)]; + } + d.set(b"Rect".to_vec(), arr(&r)); + } else { // Quads the outline can't draw, or any quad that encloses no area, are replaced by the // /Rect's, or dropped when there is no usable /Rect either; such a mark then removes // nothing. @@ -418,9 +458,11 @@ pub(crate) fn place(doc: &mut Document, page_ref: ObjRef, mut d: Dict) -> Result // and add the imported dict beside it under no name of its own. let mut clash = false; for a in annots { - let existing = a.as_ref().and_then(|r| doc.get(r).as_dict().cloned()); - let same = nm.is_some() && existing.as_ref().and_then(|x| x.get(b"NM").and_then(|n| doc.resolve(n).as_string().map(|s| s.to_text()))) == nm; - if same && existing.as_ref().is_some_and(|x| import_protected(doc, x)) { + // Borrowed: the page's annotations are read once per imported one, never copied. + let held = a.as_ref().map(|r| doc.get(r)); + let existing = held.as_deref().and_then(Object::as_dict); + let same = nm.is_some() && existing.and_then(|x| x.get(b"NM").and_then(|n| doc.resolve(n).as_string().map(|s| s.to_text()))) == nm; + if same && existing.is_some_and(|x| import_protected(doc, x)) { clash = true; kept.push(a); } else if same { @@ -434,7 +476,8 @@ pub(crate) fn place(doc: &mut Document, page_ref: ObjRef, mut d: Dict) -> Result } // The replaced note's pop-up goes with it; otherwise it stays on the page pointing at a note that is no longer there. kept.retain(|a| { - let Some(x) = a.as_ref().map(|r| doc.get(r)).and_then(|o| o.as_dict().cloned()) else { return true }; + let Some(held) = a.as_ref().map(|r| doc.get(r)) else { return true }; + let Some(x) = held.as_dict() else { return true }; x.name(b"Subtype") != Some(b"Popup".as_slice()) || x.reference(b"Parent").is_none_or(|p| !removed.contains(&p)) }); // Keep the replaced comment's identity: replies and review statuses not included in a @@ -516,7 +559,7 @@ pub(crate) fn import_xfdf(doc: &mut Document, text: &str) -> Result Date: Tue, 3 Nov 2026 23:20:43 +0000 Subject: [PATCH 02/14] address review: follow /Parent 64 levels in is_field --- crates/xfdf/src/tests.rs | 17 +++++++++++++++++ crates/xfdf/src/xfdf.rs | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rs index 341de91c1c..37078a1d15 100644 --- a/crates/xfdf/src/tests.rs +++ b/crates/xfdf/src/tests.rs @@ -2176,3 +2176,20 @@ fn same_name_import_keeps_a_widget_with_an_inherited_field_type() { let annots = doc.get(page).as_dict().unwrap().get(b"Annots").unwrap().as_array().unwrap().len(); assert_eq!(annots, 2); } + +#[test] +fn a_field_type_up_to_64_parents_away_protects_the_annotation() { + // the forms crate follows /Parent 64 levels: so does the import's protection check + let mut doc = blank(); + let mut top = Dict::new(); + top.set(b"FT".to_vec(), Object::name("Tx")); + let mut parent = doc.add(Object::Dict(top)); + for _ in 0..40 { + let mut d = Dict::new(); + d.set(b"Parent".to_vec(), Object::Ref(parent)); + parent = doc.add(Object::Dict(d)); + } + let mut annot = Dict::new(); + annot.set(b"Parent".to_vec(), Object::Ref(parent)); + assert!(crate::xfdf::is_field(&doc, &annot)); +} diff --git a/crates/xfdf/src/xfdf.rs b/crates/xfdf/src/xfdf.rs index a9831ca331..54cef5bcff 100644 --- a/crates/xfdf/src/xfdf.rs +++ b/crates/xfdf/src/xfdf.rs @@ -383,13 +383,13 @@ fn import_protected(doc: &Document, x: &Dict) -> bool { /// Whether `x` is a form field or a widget of one: it has `/FT`, or a `/Parent` up the field /// tree does (a widget inherits its field's type). -fn is_field(doc: &Document, x: &Dict) -> bool { +pub(crate) fn is_field(doc: &Document, x: &Dict) -> bool { if x.contains(b"FT") { return true; } let mut parent = x.reference(b"Parent"); let mut seen = std::collections::HashSet::new(); - while let Some(r) = parent.filter(|r| seen.len() < 32 && seen.insert(*r)) { + while let Some(r) = parent.filter(|r| seen.len() < 64 && seen.insert(*r)) { let o = doc.get(r); let Some(d) = o.as_dict() else { return false }; if d.contains(b"FT") { From b2b6ec62f20f17b5d4eebc8170f00a988a2881c2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 08:33:33 -0500 Subject: [PATCH 03/14] Delegate RFC 3161 request parsing to shared PKCS7 --- crates/sign/src/timestamp.rs | 106 ++++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 21 deletions(-) diff --git a/crates/sign/src/timestamp.rs b/crates/sign/src/timestamp.rs index 01c17221ac..d9530b68b4 100644 --- a/crates/sign/src/timestamp.rs +++ b/crates/sign/src/timestamp.rs @@ -14,6 +14,7 @@ use crate::{SignError, Time}; const TST_INFO: &str = "1.2.840.113549.1.9.16.1.4"; const MAX_RESPONSE: usize = 4 * 1024 * 1024; const MAX_TOKEN: usize = 2 * 1024 * 1024; +#[cfg(test)] const MAX_REQUEST: usize = 1024 * 1024; /// RFC 3161 request data. `nonce` is optional but recommended for online requests. @@ -106,27 +107,24 @@ fn bad(message: impl Into) -> SignError { /// Decode a `TimeStampReq` as [`TimestampQuery::encode`] wrote it. pub fn parse_request(bytes: &[u8]) -> Result { - if bytes.is_empty() || bytes.len() > MAX_REQUEST { - return Err(bad("request is empty or exceeds the 1 MiB limit")); - } - let fields = Tlv::parse_all(bytes)?.expect(tag::SEQUENCE, "TimeStampReq")?.children()?; - let mut it = fields.into_iter(); - let _version = it.next().ok_or_else(|| bad("missing version"))?.u64()?; - let imprint = it.next().ok_or_else(|| bad("missing message imprint"))?.children()?; - let alg_oid = - imprint.first().ok_or_else(|| bad("missing imprint algorithm"))?.children()?.first().ok_or_else(|| bad("missing algorithm OID"))?.oid()?; - let digest = DigestAlg::from_oid(&alg_oid).ok_or_else(|| bad("unsupported imprint digest algorithm"))?; - let hash = imprint.get(1).ok_or_else(|| bad("missing imprint digest"))?.expect(tag::OCTET_STRING, "imprint digest")?.value.to_vec(); - let mut q = TimestampQuery::new(digest, hash)?; - for f in it { - match f.tag { - tag::OID => q.policy = Some(f.oid()?), - tag::INTEGER => q.nonce = Some(f.u64()?), - tag::BOOLEAN => q.certificate_request = f.value.first() == Some(&0xFF), - _ => {} - } - } - Ok(q) + let query = craft_pkcs7::timestamp::parse_request(bytes).map_err(der::app_error)?; + // Exhaustive mapping preserves the app's digest identity without reparsing or copying. + use craft_pkcs7::DigestAlg as Shared; + let digest = match query.digest { + Shared::Sha1 => DigestAlg::Sha1, + Shared::Sha224 => DigestAlg::Sha224, + Shared::Sha256 => DigestAlg::Sha256, + Shared::Sha384 => DigestAlg::Sha384, + Shared::Sha512 => DigestAlg::Sha512, + Shared::Sha512_224 => DigestAlg::Sha512_224, + Shared::Sha512_256 => DigestAlg::Sha512_256, + Shared::Sha3_224 => DigestAlg::Sha3_224, + Shared::Sha3_256 => DigestAlg::Sha3_256, + Shared::Sha3_384 => DigestAlg::Sha3_384, + Shared::Sha3_512 => DigestAlg::Sha3_512, + Shared::Ripemd160 => DigestAlg::Ripemd160, + }; + Ok(TimestampQuery { digest, imprint: query.imprint, policy: query.policy, nonce: query.nonce, certificate_request: query.certificate_request }) } /// Parse and validate a token (a bare CMS `ContentInfo`): TSTInfo content, bounded size, the @@ -251,3 +249,69 @@ mod tests { assert!(parse_request(&vec![0; MAX_REQUEST + 1]).is_err()); } } + +#[cfg(test)] +mod request_parity { + use super::*; + // Original request reader: Justin John; algorithm support: Thorsten Hirsch. + // Retained as an independent higher-level parser oracle (MIT OR Apache-2.0). + fn legacy_request(bytes: &[u8]) -> Result { + if bytes.is_empty() || bytes.len() > MAX_REQUEST { + return Err(bad("request is empty or exceeds the 1 MiB limit")); + } + let fields = Tlv::parse_all(bytes)?.expect(tag::SEQUENCE, "TimeStampReq")?.children()?; + let mut it = fields.into_iter(); + let _version = it.next().ok_or_else(|| bad("missing version"))?.u64()?; + let imprint = it.next().ok_or_else(|| bad("missing message imprint"))?.children()?; + let alg_oid = imprint + .first() + .ok_or_else(|| bad("missing imprint algorithm"))? + .children()? + .first() + .ok_or_else(|| bad("missing algorithm OID"))? + .oid()?; + let digest = DigestAlg::from_oid(&alg_oid).ok_or_else(|| bad("unsupported imprint digest algorithm"))?; + let hash = imprint.get(1).ok_or_else(|| bad("missing imprint digest"))?.expect(tag::OCTET_STRING, "imprint digest")?.value.to_vec(); + let mut q = TimestampQuery::new(digest, hash)?; + for f in it { + match f.tag { + tag::OID => q.policy = Some(f.oid()?), + tag::INTEGER => q.nonce = Some(f.u64()?), + tag::BOOLEAN => q.certificate_request = f.value.first() == Some(&0xFF), + _ => {} + } + } + Ok(q) + } + + fn compare(bytes: &[u8]) { + match (legacy_request(bytes), parse_request(bytes)) { + (Ok(a), Ok(b)) => assert_eq!(a, b), + (Err(a), Err(b)) => assert_eq!(format!("{a:?}"), format!("{b:?}")), + other => panic!("request parity mismatch: {other:?}"), + } + } + #[test] + fn shared_request_preserves_algorithms_fields_errors_and_limits() { + for digest in DigestAlg::ALL { + let mut query = TimestampQuery::new(digest, digest.digest(&[b"fixture"])).unwrap(); + query.policy = Some("1.2.3.4".into()); + query.nonce = Some(u64::MAX); + for cert in [false, true] { + query.certificate_request = cert; + let bytes = query.encode().unwrap(); + compare(&bytes); + for len in 0..bytes.len() { + compare(&bytes[..len]); + } + for pos in 0..bytes.len() { + let mut changed = bytes.clone(); + changed[pos] ^= 255; + compare(&changed); + } + } + } + compare(&vec![0; MAX_REQUEST]); + compare(&vec![0; MAX_REQUEST + 1]); + } +} From 79fc51b7406491d2e300f8046bc4882583bfd9bd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 09:59:30 -0500 Subject: [PATCH 04/14] Use shared XFA XML DOM while retaining app form policy --- Cargo.lock | 19 ++ crates/xfa/Cargo.toml | 1 + crates/xfa/src/xml.rs | 399 ++---------------------------------------- 3 files changed, 34 insertions(+), 385 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7230c7db39..e0cebc3ba9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1566,6 +1566,24 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "craft-xfa" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=1a0d5d607859a2e529851482de09b8e53aaeec3e#1a0d5d607859a2e529851482de09b8e53aaeec3e" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=1a0d5d607859a2e529851482de09b8e53aaeec3e)", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=1a0d5d607859a2e529851482de09b8e53aaeec3e#1a0d5d607859a2e529851482de09b8e53aaeec3e" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -5170,6 +5188,7 @@ version = "0.21.1" dependencies = [ "base64 0.23.1", "craft-image-codecs", + "craft-xfa", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "pdfcraft-cos", "pdfcraft-fonts", diff --git a/crates/xfa/Cargo.toml b/crates/xfa/Cargo.toml index ca7c3475e0..8c8b61c804 100644 --- a/crates/xfa/Cargo.toml +++ b/crates/xfa/Cargo.toml @@ -11,6 +11,7 @@ repository.workspace = true workspace = true [dependencies] +craft-xfa = { git = "https://github.com/storytold/craft-libs", rev = "1a0d5d607859a2e529851482de09b8e53aaeec3e" } pdfcraft-cos = { workspace = true } pdfcraft-fonts = { workspace = true } craft-xml = { workspace = true } diff --git a/crates/xfa/src/xml.rs b/crates/xfa/src/xml.rs index 7cc755f3f2..93b017fad8 100644 --- a/crates/xfa/src/xml.rs +++ b/crates/xfa/src/xml.rs @@ -1,389 +1,18 @@ -//! Lightweight namespace-aware XML DOM with source byte ranges, backed by `craft_xml`. +//! Namespace-aware XFA DOM delegated to the shared implementation. +pub(crate) use craft_xfa::xml::*; -use std::ops::Range; +#[cfg(test)] +mod tests { + use super::*; -const XML_NS: &str = "http://www.w3.org/XML/1998/namespace"; -const XMLNS_NS: &str = "http://www.w3.org/2000/xmlns/"; - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub(crate) struct NodeId(pub(crate) usize); - -#[derive(Clone, Debug)] -pub(crate) struct ParsingOptions { - pub(crate) allow_dtd: bool, - pub(crate) nodes_limit: u32, -} - -impl Default for ParsingOptions { - fn default() -> Self { - Self { allow_dtd: false, nodes_limit: u32::MAX } - } -} - -#[derive(Clone, Debug)] -struct AttrData { - ns: Option, - name: String, - value: String, -} - -#[derive(Clone, Debug)] -enum NodeKind { - Element { - ns: Option, - name: String, - attrs: Vec, - /// In-scope `(prefix, uri)` bindings at this element (`""` for default namespace). - namespaces: Vec<(String, String)>, - range: Range, - }, - Text(String), -} - -#[derive(Clone, Debug)] -struct NodeData { - kind: NodeKind, - children: Vec, -} - -pub(crate) struct Document { - nodes: Vec, - root: usize, -} - -#[derive(Clone, Copy)] -pub(crate) struct Node<'a> { - doc: &'a Document, - id: usize, -} - -impl PartialEq for Node<'_> { - fn eq(&self, other: &Self) -> bool { - self.id == other.id && std::ptr::eq(self.doc, other.doc) - } -} - -impl Eq for Node<'_> {} - -#[derive(Clone, Copy)] -pub(crate) struct ExpandedName<'a> { - ns: Option<&'a str>, - name: &'a str, -} - -impl<'a> ExpandedName<'a> { - pub(crate) fn name(&self) -> &'a str { - self.name - } - - pub(crate) fn namespace(&self) -> Option<&'a str> { - self.ns - } -} - -pub(crate) trait AttributeKey { - fn matches(&self, ns: Option<&str>, name: &str) -> bool; -} - -impl AttributeKey for &str { - fn matches(&self, ns: Option<&str>, name: &str) -> bool { - ns.is_none() && name == *self - } -} - -impl AttributeKey for (&str, &str) { - fn matches(&self, ns: Option<&str>, name: &str) -> bool { - ns == Some(self.0) && name == self.1 - } -} - -#[derive(Clone, Copy)] -pub(crate) struct Attribute<'a> { - attr: &'a AttrData, -} - -impl<'a> Attribute<'a> { - pub(crate) fn name(&self) -> &'a str { - &self.attr.name - } - - pub(crate) fn namespace(&self) -> Option<&'a str> { - self.attr.ns.as_deref() - } - - pub(crate) fn value(&self) -> &'a str { - &self.attr.value - } -} - -#[derive(Clone, Copy)] -pub(crate) struct Namespace<'a> { - prefix: &'a str, - uri: &'a str, -} - -impl<'a> Namespace<'a> { - pub(crate) fn name(&self) -> Option<&'a str> { - (!self.prefix.is_empty()).then_some(self.prefix) - } - - pub(crate) fn uri(&self) -> &'a str { - self.uri - } -} - -fn split_qname(qname: &str) -> (&str, &str) { - match qname.split_once(':') { - Some((p, l)) => (p, l), - None => ("", qname), - } -} - -fn resolve_ns(scope: &[(String, String)], prefix: &str, is_attr: bool) -> Result, String> { - if prefix == "xml" { - return Ok(Some(XML_NS.to_string())); - } - if prefix == "xmlns" { - return Ok(Some(XMLNS_NS.to_string())); - } - if prefix.is_empty() { - if is_attr { - return Ok(None); - } - return Ok(scope.iter().rev().find(|(p, _)| p.is_empty()).and_then(|(_, u)| (!u.is_empty()).then(|| u.clone()))); - } - match scope.iter().rev().find(|(p, _)| p == prefix) { - Some((_, u)) if !u.is_empty() => Ok(Some(u.clone())), - _ => Err(format!("undeclared namespace prefix: {prefix}")), - } -} - -impl Document { - #[cfg(test)] - pub(crate) fn parse(text: &str) -> Result { - Self::parse_with_options(text, ParsingOptions::default()) - } - - pub(crate) fn parse_with_options(text: &str, options: ParsingOptions) -> Result { - let mut opts = craft_xml::Options { - strict: true, - doctype: if options.allow_dtd { craft_xml::Doctype::Ignore } else { craft_xml::Doctype::Reject }, - ..Default::default() - }; - opts.limits.max_nodes = options.nodes_limit as usize; - opts.limits.max_depth = crate::MAX_XML_DEPTH; - - let mut events = opts.events(text.as_bytes()); - let mut nodes: Vec = Vec::new(); - let mut open: Vec<(usize, usize)> = Vec::new(); // (node_id, scope_len_before) - let mut scope: Vec<(String, String)> = Vec::new(); - let mut root: Option = None; - - while let Some(ev) = events.next().map_err(|e| e.to_string())? { - let span = events.span(); - match ev { - craft_xml::Event::Start { name, attrs } => { - if nodes.len() >= options.nodes_limit as usize { - return Err("nodes limit reached".to_string()); - } - let scope_mark = scope.len(); - for (k, v) in &attrs { - if k == "xmlns" { - scope.push((String::new(), v.clone())); - } else if let Some(prefix) = k.strip_prefix("xmlns:") { - scope.push((prefix.to_string(), v.clone())); - } - } - let (el_prefix, el_local) = split_qname(&name); - let el_ns = resolve_ns(&scope, el_prefix, false)?; - let mut el_attrs = Vec::new(); - for (k, v) in attrs { - if k == "xmlns" || k.starts_with("xmlns:") { - continue; - } - let (ap, al) = split_qname(&k); - let ans = resolve_ns(&scope, ap, true)?; - el_attrs.push(AttrData { ns: ans, name: al.to_string(), value: v }); - } - // Deduplicate in-scope namespace bindings, keeping the innermost binding per prefix. - let mut namespaces: Vec<(String, String)> = Vec::new(); - for (p, u) in scope.iter().rev() { - if !namespaces.iter().any(|(ep, _)| ep == p) { - namespaces.push((p.clone(), u.clone())); - } - } - namespaces.retain(|(_, u)| !u.is_empty()); - let id = nodes.len(); - nodes.push(NodeData { - kind: NodeKind::Element { ns: el_ns, name: el_local.to_string(), attrs: el_attrs, namespaces, range: span.start..span.end }, - children: Vec::new(), - }); - if let Some(&(parent, _)) = open.last() { - nodes[parent].children.push(id); - } else if root.is_none() { - root = Some(id); - } - open.push((id, scope_mark)); - } - craft_xml::Event::End { .. } => { - if let Some((id, scope_mark)) = open.pop() { - if let NodeKind::Element { range, .. } = &mut nodes[id].kind { - range.end = span.end; - } - scope.truncate(scope_mark); - } - } - craft_xml::Event::Text(t) | craft_xml::Event::CData(t) => { - if let Some(&(parent, _)) = open.last() { - if let Some(&last_child) = nodes[parent].children.last() - && let NodeKind::Text(existing) = &mut nodes[last_child].kind - { - existing.push_str(&t); - continue; - } - if nodes.len() >= options.nodes_limit as usize { - return Err("nodes limit reached".to_string()); - } - let id = nodes.len(); - nodes.push(NodeData { kind: NodeKind::Text(t), children: Vec::new() }); - nodes[parent].children.push(id); - } - } - craft_xml::Event::Pi { .. } | craft_xml::Event::Comment(_) => {} - } - } - - let Some(root) = root else { - return Err("empty XML document".to_string()); - }; - Ok(Self { nodes, root }) - } - - pub(crate) fn root_element(&self) -> Node<'_> { - Node { doc: self, id: self.root } - } - - pub(crate) fn get_node(&self, id: NodeId) -> Option> { - (id.0 < self.nodes.len()).then_some(Node { doc: self, id: id.0 }) - } - - pub(crate) fn descendants(&self) -> Descendants<'_> { - Descendants { doc: self, stack: vec![self.root] } - } -} - -pub(crate) struct Descendants<'a> { - doc: &'a Document, - stack: Vec, -} - -impl<'a> Iterator for Descendants<'a> { - type Item = Node<'a>; - - fn next(&mut self) -> Option { - let id = self.stack.pop()?; - let children = &self.doc.nodes[id].children; - for &c in children.iter().rev() { - self.stack.push(c); - } - Some(Node { doc: self.doc, id }) - } -} - -impl<'a> Node<'a> { - pub(crate) fn id(&self) -> NodeId { - NodeId(self.id) - } - - pub(crate) fn document(&self) -> &'a Document { - self.doc - } - - pub(crate) fn is_element(&self) -> bool { - matches!(self.doc.nodes[self.id].kind, NodeKind::Element { .. }) - } - - pub(crate) fn is_text(&self) -> bool { - matches!(self.doc.nodes[self.id].kind, NodeKind::Text(_)) - } - - pub(crate) fn tag_name(&self) -> ExpandedName<'a> { - match &self.doc.nodes[self.id].kind { - NodeKind::Element { ns, name, .. } => ExpandedName { ns: ns.as_deref(), name }, - NodeKind::Text(_) => ExpandedName { ns: None, name: "" }, - } - } - - pub(crate) fn attribute(&self, key: K) -> Option<&'a str> { - match &self.doc.nodes[self.id].kind { - NodeKind::Element { attrs, .. } => attrs.iter().find(|a| key.matches(a.ns.as_deref(), &a.name)).map(|a| a.value.as_str()), - NodeKind::Text(_) => None, - } - } - - pub(crate) fn attributes(&self) -> impl ExactSizeIterator> + use<'a> { - let attrs = match &self.doc.nodes[self.id].kind { - NodeKind::Element { attrs, .. } => attrs.as_slice(), - NodeKind::Text(_) => &[], - }; - attrs.iter().map(|attr| Attribute { attr }) - } - - pub(crate) fn namespaces(&self) -> impl ExactSizeIterator> + use<'a> { - let ns = match &self.doc.nodes[self.id].kind { - NodeKind::Element { namespaces, .. } => namespaces.as_slice(), - NodeKind::Text(_) => &[], - }; - ns.iter().map(|(p, u)| Namespace { prefix: p, uri: u }) - } - - pub(crate) fn lookup_prefix(&self, uri: &str) -> Option<&'a str> { - if uri == XML_NS { - return Some("xml"); - } - match &self.doc.nodes[self.id].kind { - NodeKind::Element { namespaces, .. } => namespaces.iter().find(|(_, u)| u == uri).map(|(p, _)| p.as_str()), - NodeKind::Text(_) => None, - } - } - - pub(crate) fn lookup_namespace_uri(&self, prefix: Option<&str>) -> Option<&'a str> { - if prefix == Some("xml") { - return Some(XML_NS); - } - if prefix == Some("xmlns") { - return Some(XMLNS_NS); - } - let target = prefix.unwrap_or(""); - match &self.doc.nodes[self.id].kind { - NodeKind::Element { namespaces, .. } => namespaces.iter().find(|(p, _)| p == target).map(|(_, u)| u.as_str()), - NodeKind::Text(_) => None, - } - } - - pub(crate) fn children(&self) -> impl Iterator> + use<'a> { - let doc = self.doc; - doc.nodes[self.id].children.iter().map(move |&id| Node { doc, id }) - } - - pub(crate) fn descendants(&self) -> Descendants<'a> { - Descendants { doc: self.doc, stack: vec![self.id] } - } - - pub(crate) fn text(&self) -> Option<&'a str> { - match &self.doc.nodes[self.id].kind { - NodeKind::Text(t) => Some(t.as_str()), - NodeKind::Element { .. } => self.doc.nodes[self.id].children.first().and_then(|&cid| match &self.doc.nodes[cid].kind { - NodeKind::Text(t) => Some(t.as_str()), - _ => None, - }), - } - } - - pub(crate) fn range(&self) -> Range { - match &self.doc.nodes[self.id].kind { - NodeKind::Element { range, .. } => range.clone(), - NodeKind::Text(_) => 0..0, - } + #[test] + fn shared_dom_keeps_depth_policy_and_rejects_duplicate_attributes() { + assert_eq!(crate::MAX_XML_DEPTH, craft_xfa::MAX_XML_DEPTH); + assert!(Document::parse("").is_err()); + assert!(Document::parse("").is_err()); + let doc = Document::parse("value").unwrap(); + let field = doc.root_element().children().next().unwrap(); + assert_eq!(field.tag_name().namespace(), Some("u")); + assert_eq!(field.text(), Some("value")); } } From 42b301531991e7db385d13c201a9c2c184ed09dd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 10:42:51 -0500 Subject: [PATCH 05/14] Read XFDF field values through shared bounded parser --- Cargo.lock | 20 ++++++++++++++++++++ crates/xfdf/Cargo.toml | 1 + crates/xfdf/src/lib.rs | 18 +++++------------- crates/xfdf/src/tests.rs | 26 ++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7230c7db39..5425c1b8d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1566,6 +1566,25 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "craft-xfdf" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=aa975a4bea2a4a7b012199b081e68c1a09935969#aa975a4bea2a4a7b012199b081e68c1a09935969" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=aa975a4bea2a4a7b012199b081e68c1a09935969)", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=aa975a4bea2a4a7b012199b081e68c1a09935969#aa975a4bea2a4a7b012199b081e68c1a09935969" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -5183,6 +5202,7 @@ version = "0.21.1" dependencies = [ "base64 0.23.1", "craft-csv", + "craft-xfdf", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "pdfcraft-annot", "pdfcraft-cos", diff --git a/crates/xfdf/Cargo.toml b/crates/xfdf/Cargo.toml index d1e005ddc9..ca1ab3f45e 100644 --- a/crates/xfdf/Cargo.toml +++ b/crates/xfdf/Cargo.toml @@ -9,6 +9,7 @@ license.workspace = true workspace = true [dependencies] +craft-xfdf = { git = "https://github.com/storytold/craft-libs", rev = "aa975a4bea2a4a7b012199b081e68c1a09935969", version = "0.1.0" } pdfcraft-cos = { workspace = true } pdfcraft-annot = { workspace = true } pdfcraft-forms = { workspace = true } diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rs index fc6d77ac43..b15f43581b 100644 --- a/crates/xfdf/src/lib.rs +++ b/crates/xfdf/src/lib.rs @@ -27,7 +27,7 @@ mod xfdf; #[cfg(test)] use common::{MAX_DEPTH, MAX_XML_DEPTH, arr, nums_of, parse_hex, text_of, xml_nesting_depth}; -use common::{check_form_xml_depth, el_text, esc, parse_form_xml, walk_xfdf, xml_name}; +use common::{check_form_xml_depth, el_text, esc, parse_form_xml, xml_name}; pub use fdf::export_fdf; use fdf::{ImportBudget, fdf_body, import_fdf, walk_fdf}; #[cfg(test)] @@ -179,18 +179,10 @@ pub fn data_values(bytes: &[u8]) -> Result)>, DataError }) .collect()); } - let text = String::from_utf8_lossy(bytes); - let t = text.trim_start_matches('\u{feff}').trim_start(); - check_form_xml_depth(t)?; - let root = parse_form_xml(t).map_err(|_| DataError::UnknownFormat)?; - if root.local() != "xfdf" { - return Err(DataError::UnknownFormat); - } - let mut values = Vec::new(); - if let Some(fields) = root.child(craft_xml::Local("fields")) { - walk_xfdf(fields, "", &mut values, 0, &mut 0)?; - } - Ok(values) + craft_xfdf::read_fields(bytes).map_err(|error| match error { + craft_xfdf::Error::UnknownFormat => DataError::UnknownFormat, + craft_xfdf::Error::Malformed(message) => DataError::Malformed(message), + }) } /// Merge Data Files into Spreadsheet: one CSV row per file, one column per field name (in the diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rs index f9e97cf3bf..0567dc2a09 100644 --- a/crates/xfdf/src/tests.rs +++ b/crates/xfdf/src/tests.rs @@ -4,6 +4,7 @@ use pdfcraft_annot::{Markup, Meta, NewAnnotation, Shape, Style, TextReplacement, use pdfcraft_cos::{SaveOptions, write_full, write_incremental}; use pdfcraft_forms::{NewField, add_field, fields, set_value}; +use super::common::walk_xfdf; use super::xfdf::PDFCRAFT_NS; use super::*; @@ -2762,3 +2763,28 @@ fn merge_csv_keeps_first_repeated_value() { let rows = vec![("Name".into(), vec!["first".into()]), ("Name".into(), vec!["second".into()])]; assert_eq!(merge_csv(&[rows]), "Name\nfirst\n"); } + +#[test] +fn shared_xfdf_fields_match_retained_reader() { + // Independent retained app walker remains used by annotation imports. + for xml in [ + "", + "abcskiplast", + "v", + ] { + let root = parse_form_xml(xml).unwrap(); + let mut expected = Vec::new(); + if let Some(fields) = root.child(craft_xml::Local("fields")) { + walk_xfdf(fields, "", &mut expected, 0, &mut 0).unwrap(); + } + assert_eq!(data_values(xml.as_bytes()).unwrap(), expected); + } + assert!(matches!(data_values(b""), Err(DataError::UnknownFormat))); + assert!(matches!(data_values(b""), Err(DataError::UnknownFormat))); + let deep = format!("{}{}", "".repeat(65), "".repeat(65)); + assert!(matches!(data_values(deep.as_bytes()), Err(DataError::Malformed(_)))); + assert_eq!( + data_values(b"\xef\xbb\xbf \xff").unwrap(), + vec![("n".into(), vec!["\u{fffd}".into()])] + ); +} From 955205860a1ef2ff28fcfb6751c92ac713889df1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 11:49:58 -0500 Subject: [PATCH 06/14] Specify shared XFA crate version for dependency policy --- crates/xfa/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/xfa/Cargo.toml b/crates/xfa/Cargo.toml index 8c8b61c804..73e02252b4 100644 --- a/crates/xfa/Cargo.toml +++ b/crates/xfa/Cargo.toml @@ -11,7 +11,7 @@ repository.workspace = true workspace = true [dependencies] -craft-xfa = { git = "https://github.com/storytold/craft-libs", rev = "1a0d5d607859a2e529851482de09b8e53aaeec3e" } +craft-xfa = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "1a0d5d607859a2e529851482de09b8e53aaeec3e" } pdfcraft-cos = { workspace = true } pdfcraft-fonts = { workspace = true } craft-xml = { workspace = true } From 0b6306165e0b9810eccdbf6031289e4ef893bf87 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 11:56:36 -0500 Subject: [PATCH 07/14] Parse CRL evidence through shared PKCS7 components --- Cargo.lock | 2 +- crates/sign/Cargo.toml | 2 +- crates/sign/src/revocation.rs | 56 +++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7230c7db39..c0e440a282 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1384,7 +1384,7 @@ dependencies = [ [[package]] name = "craft-pkcs7" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=8479a1f3bbabb0e0b25194c508ab2eb14aa5490f#8479a1f3bbabb0e0b25194c508ab2eb14aa5490f" +source = "git+https://github.com/storytold/craft-libs?rev=a62ce81f2447b1791a8d8c0aa6317fb6388f189b#a62ce81f2447b1791a8d8c0aa6317fb6388f189b" dependencies = [ "aes", "aws-lc-rs", diff --git a/crates/sign/Cargo.toml b/crates/sign/Cargo.toml index 72698021f5..86e0215cbd 100644 --- a/crates/sign/Cargo.toml +++ b/crates/sign/Cargo.toml @@ -12,7 +12,7 @@ workspace = true [dependencies] # Existing shared ASN.1 codec; PDF signature policy remains local. -craft-pkcs7 = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "8479a1f3bbabb0e0b25194c508ab2eb14aa5490f" } +craft-pkcs7 = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "a62ce81f2447b1791a8d8c0aa6317fb6388f189b" } pdfcraft-cos = { workspace = true } pdfcraft-annot = { workspace = true } pdfcraft-content = { workspace = true } diff --git a/crates/sign/src/revocation.rs b/crates/sign/src/revocation.rs index 93191b2528..5630bfcba5 100644 --- a/crates/sign/src/revocation.rs +++ b/crates/sign/src/revocation.rs @@ -65,6 +65,21 @@ pub struct CertificateList { impl CertificateList { pub fn parse(bytes: &[u8]) -> Result { + let parsed = craft_pkcs7::revocation::CertificateList::parse(bytes).map_err(crate::der::app_error)?.into_parts(); + Ok(Self { + tbs: parsed.tbs, + sig_alg: parsed.sig_alg, + signature: parsed.signature, + issuer_raw: parsed.issuer_raw, + this_update: parsed.this_update, + next_update: parsed.next_update, + revoked: parsed.revoked, + }) + } + + // Retained parity oracle: original CRL parser by Justin John (4d0078aeff). + #[cfg(test)] + fn parse_legacy(bytes: &[u8]) -> Result { if bytes.is_empty() || bytes.len() > MAX_EVIDENCE { return Err(bad("CRL is empty or exceeds the 2 MiB limit")); } @@ -307,3 +322,44 @@ impl OcspResponse { } } } + +#[cfg(test)] +mod shared_crl_tests { + use super::*; + use crate::der; + + #[test] + fn shared_crl_parser_matches_retained_app_reader() { + let date = Time { year: 2026, month: 6, day: 15, hour: 12, minute: 0, second: 0 }; + let algorithm = DigestAlg::Sha256.algorithm(); + let tbs = der::seq(&[ + &der::int(1), + &algorithm, + &der::seq(&[]), + &date.encode(), + &date.encode(), + &der::seq(&[&der::seq(&[&der::uint(&[0x80]), &date.encode()])]), + ]); + let valid = der::seq(&[&tbs, &algorithm, &der::bit_string(&[1, 2, 3])]); + let compare = |bytes: &[u8]| match (CertificateList::parse(bytes), CertificateList::parse_legacy(bytes)) { + (Ok(a), Ok(b)) => { + assert_eq!( + (a.tbs, a.sig_alg, a.signature, a.issuer_raw, a.this_update, a.next_update, a.revoked), + (b.tbs, b.sig_alg, b.signature, b.issuer_raw, b.this_update, b.next_update, b.revoked) + ); + } + (Err(a), Err(b)) => assert_eq!(format!("{a:?}"), format!("{b:?}")), + _ => panic!("shared and retained CRL parsers disagree"), + }; + for end in 0..=valid.len() { + compare(&valid[..end]); + } + for i in 0..valid.len() { + let mut mutated = valid.clone(); + mutated[i] ^= 0x80; + compare(&mutated); + } + compare(&vec![0; MAX_EVIDENCE]); + compare(&vec![0; MAX_EVIDENCE + 1]); + } +} From c8d0f17b251e1316b03b330eebf19d0af1b6dd36 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 13:57:22 -0500 Subject: [PATCH 08/14] xfa: cache repeated prototype child counts without changing work charges --- crates/xfa/src/proto.rs | 21 ++++++++++++++++++++- crates/xfa/src/proto/tests.rs | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/crates/xfa/src/proto.rs b/crates/xfa/src/proto.rs index ae1505804e..99dc18b689 100644 --- a/crates/xfa/src/proto.rs +++ b/crates/xfa/src/proto.rs @@ -74,6 +74,7 @@ pub(crate) fn expand(template: Node) -> (Option, Vec) { soms: HashMap::new(), search: MAX_SEARCH, work: MAX_WORK, + child_counts: HashMap::new(), active: HashMap::new(), missing: false, external: false, @@ -124,6 +125,8 @@ struct Writer<'a> { search: usize, /// Children, attributes and namespaces left to look at while merging layers. work: usize, + /// Bounded memoization only; each use still pays the full child count. + child_counts: HashMap, /// The elements being written around the current one: inheriting from one is a loop. active: HashMap, missing: bool, @@ -132,6 +135,22 @@ struct Writer<'a> { layers_cut: bool, } +/// Counting a reused prototype must not itself repeatedly walk its descendants. +/// Cache only complete direct-child counts, and cap the optimization's memory. +/// Truncated counts are not exact and must never be cached. Budget charging is unchanged. +fn child_count(node: Node<'_>, work: usize, cache: &mut HashMap) -> usize { + const MAX_CACHED_COUNTS: usize = 4_096; + let stop = work.saturating_add(1); + if let Some(count) = cache.get(&node.id()) { + return (*count).min(stop); + } + let count = node.children().take(stop).count(); + if count < stop && cache.len() < MAX_CACHED_COUNTS { + cache.insert(node.id(), count); + } + count +} + fn escape(s: &str, attribute: bool, out: &mut String) { for c in s.chars() { match c { @@ -439,7 +458,7 @@ impl<'a> Writer<'a> { // further than the budget left), attributes and namespaces, and the scope around it. let mut cost = scope.len().saturating_add(layers.len()); for l in &layers { - let children = l.children().take(self.work.saturating_add(1)).count(); + let children = child_count(*l, self.work, &mut self.child_counts); cost = cost.saturating_add(children).saturating_add(l.attributes().len()).saturating_add(l.namespaces().len()); } if self.failed.is_some() || !self.charge(cost) { diff --git a/crates/xfa/src/proto/tests.rs b/crates/xfa/src/proto/tests.rs index ba7f6f8316..636886bb62 100644 --- a/crates/xfa/src/proto/tests.rs +++ b/crates/xfa/src/proto/tests.rs @@ -399,3 +399,28 @@ fn a_som_expression_past_its_length_limit_is_not_followed() { assert_eq!(field(&t, "long").common.h, None); assert!(close(field(&t, "short").common.h, 2.0 * MM)); } + +#[test] +fn cached_child_counts_preserve_truncation_and_charge_when_budget_shrinks() { + let doc = crate::xml::Document::parse("").unwrap(); + let node = doc.root_element(); + let mut cache = std::collections::HashMap::new(); + assert_eq!(super::child_count(node, 1, &mut cache), 2); + assert!(cache.is_empty(), "truncated counts are not reusable"); + assert_eq!(super::child_count(node, 10, &mut cache), 3); + assert_eq!(cache.get(&node.id()), Some(&3)); + for remaining in 0..=5 { + assert_eq!(super::child_count(node, remaining, &mut cache), node.children().take(remaining + 1).count()); + } + assert_eq!(super::child_count(node, 10, &mut cache), 3, "a cached use is still charged"); +} + +#[test] +fn child_count_memoization_has_a_fixed_entry_limit() { + let doc = crate::xml::Document::parse(&format!("{}", "".repeat(5_000))).unwrap(); + let mut cache = std::collections::HashMap::new(); + for node in doc.root_element().children() { + assert_eq!(super::child_count(node, 1, &mut cache), 0); + } + assert_eq!(cache.len(), 4_096); +} From 2f8820eda34a5b83af2912098201564e390c24bb Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 13:57:45 -0500 Subject: [PATCH 09/14] test(xfa): verify cached layers consume the same writer budget --- crates/xfa/src/proto/tests.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/xfa/src/proto/tests.rs b/crates/xfa/src/proto/tests.rs index 636886bb62..efbec768a0 100644 --- a/crates/xfa/src/proto/tests.rs +++ b/crates/xfa/src/proto/tests.rs @@ -424,3 +424,38 @@ fn child_count_memoization_has_a_fixed_entry_limit() { } assert_eq!(cache.len(), 4_096); } + +#[test] +fn cached_layers_still_subtract_work_and_stop_at_the_budget() { + let doc = crate::xml::Document::parse("").unwrap(); + let node = doc.root_element(); + let mut writer = super::Writer { + template: node, + out: String::new(), + limit: super::MAX_OUTPUT, + failed: None, + ids: None, + refs: std::collections::HashMap::new(), + soms: std::collections::HashMap::new(), + search: super::MAX_SEARCH, + work: 100, + child_counts: std::collections::HashMap::new(), + active: std::collections::HashMap::new(), + missing: false, + external: false, + looped: false, + layers_cut: false, + }; + writer.element(&[node], &super::Scope::new(), 0); + assert!(writer.failed.is_none()); + let first_cost = 100 - writer.work; + assert!(first_cost > 0); + assert!(!writer.child_counts.is_empty()); + let before = writer.work; + writer.element(&[node], &super::Scope::new(), 0); + assert!(writer.failed.is_none()); + assert_eq!(before - writer.work, first_cost, "cached layers pay the same work charge"); + writer.work = 0; + writer.element(&[node], &super::Scope::new(), 0); + assert!(writer.failed.as_ref().is_some_and(|why| why.contains("children, attributes and namespaces"))); +} From f715e415075a8b5bea58dc985e2573df266af725 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 02:07:01 -0500 Subject: [PATCH 10/14] Propagate XFDF parity test errors without unwrap --- crates/xfdf/src/tests.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rs index 0567dc2a09..d427bade73 100644 --- a/crates/xfdf/src/tests.rs +++ b/crates/xfdf/src/tests.rs @@ -2765,26 +2765,27 @@ fn merge_csv_keeps_first_repeated_value() { } #[test] -fn shared_xfdf_fields_match_retained_reader() { +fn shared_xfdf_fields_match_retained_reader() -> Result<(), Box> { // Independent retained app walker remains used by annotation imports. for xml in [ "", "abcskiplast", "v", ] { - let root = parse_form_xml(xml).unwrap(); + let root = parse_form_xml(xml)?; let mut expected = Vec::new(); if let Some(fields) = root.child(craft_xml::Local("fields")) { - walk_xfdf(fields, "", &mut expected, 0, &mut 0).unwrap(); + walk_xfdf(fields, "", &mut expected, 0, &mut 0)?; } - assert_eq!(data_values(xml.as_bytes()).unwrap(), expected); + assert_eq!(data_values(xml.as_bytes())?, expected); } assert!(matches!(data_values(b""), Err(DataError::UnknownFormat))); assert!(matches!(data_values(b""), Err(DataError::UnknownFormat))); let deep = format!("{}{}", "".repeat(65), "".repeat(65)); assert!(matches!(data_values(deep.as_bytes()), Err(DataError::Malformed(_)))); assert_eq!( - data_values(b"\xef\xbb\xbf \xff").unwrap(), + data_values(b"\xef\xbb\xbf \xff")?, vec![("n".into(), vec!["\u{fffd}".into()])] ); + Ok(()) } From ae30592466a813c22fdd145536b18447ab9660fc Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:55:06 +0700 Subject: [PATCH 11/14] Update annotation tests after SaveOptions, Style, LinkAction, and Shape changes #2523's tests were written against earlier field lists. Later work added id_entropy, border and opacity style fields, a zoom on page links, and a line on custom stamps, so cargo xtask ci now fails at clippy on main. The tests keep the same intent: page links still use Fit Page, the view they had before they carried a zoom, and the extra style and stamp fields take their defaults. --- crates/annot/tests/malformed.rs | 24 ++++++++++++++++-------- crates/annot/tests/properties.rs | 8 ++++---- crates/annot/tests/round_trip.rs | 17 ++++++++++++----- crates/annot/tests/support/fixture.rs | 8 +++++++- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/crates/annot/tests/malformed.rs b/crates/annot/tests/malformed.rs index 64fc241baf..096a921298 100644 --- a/crates/annot/tests/malformed.rs +++ b/crates/annot/tests/malformed.rs @@ -382,7 +382,8 @@ fn a_link_area_that_is_too_small_or_broken_is_refused_before_it_is_written() { ]; for (label, rect, want_in_message) in cases { let mut doc = blank(2); - let e = links::add(&mut doc, 0, rect, &links::LinkAction::Page(1), &links::LinkStyle::default()).err(); + let e = + links::add(&mut doc, 0, rect, &links::LinkAction::Page { page: 1, zoom: links::LinkZoom::FitPage }, &links::LinkStyle::default()).err(); let msg = match &e { Some(AnnotError::Invalid(m)) => m.clone(), other => panic!("{label}: expected Invalid, got {other:?}"), @@ -440,10 +441,10 @@ fn non_finite_builder_input_is_refused_and_leaves_the_document_untouched() { } for (label, style) in [ - ("nan opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::NAN, width: 1.0, fill: None }), - ("infinite opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::INFINITY, width: 1.0, fill: None }), - ("nan colour", Style { color: [f64::NAN, 0.0, 0.0], opacity: 1.0, width: 1.0, fill: None }), - ("nan width", Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width: f64::NAN, fill: None }), + ("nan opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::NAN, width: 1.0, fill: None, ..Style::default() }), + ("infinite opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::INFINITY, width: 1.0, fill: None, ..Style::default() }), + ("nan colour", Style { color: [f64::NAN, 0.0, 0.0], opacity: 1.0, width: 1.0, fill: None, ..Style::default() }), + ("nan width", Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width: f64::NAN, fill: None, ..Style::default() }), ] { let mut doc = blank(1); let e = add_annotation( @@ -593,7 +594,14 @@ fn unvalidated_stamp_size_and_signature_contours_are_rewritten_silently_in_the_f &mut doc, &NewAnnotation { page: 0, - shape: Shape::CustomStamp { rect: [10.0, 20.0, 60.0, 50.0], name: "Sig".into(), picture, image: true, size: (1e300, f64::NAN) }, + shape: Shape::CustomStamp { + rect: [10.0, 20.0, 60.0, 50.0], + name: "Sig".into(), + picture, + image: true, + size: (1e300, f64::NAN), + line: None, + }, style: Style::default(), contents: String::new(), author: "A".into(), @@ -656,7 +664,7 @@ fn a_nan_fill_is_accepted_on_create_and_refused_on_edit() { &NewAnnotation { page: 0, shape: Shape::Rectangle { rect: [10.0, 20.0, 60.0, 40.0] }, - style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(nan) }, + style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(nan), ..Style::default() }, contents: String::new(), author: "A".into(), }, @@ -842,7 +850,7 @@ fn the_same_huge_fill_is_accepted_on_create_and_refused_on_edit() { &NewAnnotation { page: 0, shape: Shape::Rectangle { rect: [10.0, 20.0, 60.0, 40.0] }, - style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(probe) }, + style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(probe), ..Style::default() }, contents: String::new(), author: "A".into(), }, diff --git a/crates/annot/tests/properties.rs b/crates/annot/tests/properties.rs index 8efb6b7046..53a37cf067 100644 --- a/crates/annot/tests/properties.rs +++ b/crates/annot/tests/properties.rs @@ -165,7 +165,7 @@ proptest! { #[test] fn opacity_and_flags_follow_their_rules(opacity in any::()) { let mut doc = blank(1); - let added = add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: [10.0, 10.0, 60.0, 40.0] }, Style { color: [0.5, 0.25, 0.75], opacity, width: 1.0, fill: None }, String::new(), "Ada".into()), &meta("c".into())); + let added = add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: [10.0, 10.0, 60.0, 40.0] }, Style { color: [0.5, 0.25, 0.75], opacity, width: 1.0, fill: None, ..Style::default() }, String::new(), "Ada".into()), &meta("c".into())); if !opacity.is_finite() { prop_assert!(added.is_err(), "a non-finite opacity ({:?}) must be refused, not written", opacity); return Ok(()); @@ -243,7 +243,7 @@ proptest! { 2 => -0.5, _ => 0.0, }; - add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: *r }, Style { color: [0.9, 0.1, 0.1], opacity, width: 2.0, fill: None }, format!("c{i}"), "Ada".into()), &meta(format!("k{i}"))) + add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: *r }, Style { color: [0.9, 0.1, 0.1], opacity, width: 2.0, fill: None, ..Style::default() }, format!("c{i}"), "Ada".into()), &meta(format!("k{i}"))) .expect("adds"); want_rects.push(norm(*r)); } @@ -333,7 +333,7 @@ proptest! { #[test] fn width_and_fill_are_written_as_they_were_given(width in -4.0f64..12.0, fill in prop::option::of(prop::array::uniform3(0.0f64..1.0))) { let mut doc = blank(1); - add_annotation(&mut doc, &comment(0, Shape::Oval { rect: [5.0, 5.0, 65.0, 35.0] }, Style { color: [1.0, 0.0, 0.0], opacity: 1.0, width, fill }, String::new(), "Ada".into()), &meta("f".into())) + add_annotation(&mut doc, &comment(0, Shape::Oval { rect: [5.0, 5.0, 65.0, 35.0] }, Style { color: [1.0, 0.0, 0.0], opacity: 1.0, width, fill, ..Style::default() }, String::new(), "Ada".into()), &meta("f".into())) .expect("adds"); for path in [Save::Incremental, Save::FullTable] { let (file, reopened) = save_and_reopen(&doc, path); @@ -360,7 +360,7 @@ proptest! { fn a_line_keeps_its_endpoints_and_its_rectangle_spans_them(from in prop::array::uniform2(coord()), dx in 1.0f64..300.0, dy in -300.0f64..300.0, width in 0.5f64..8.0) { let to = [(from[0] + dx).min(450.0), from[1] + dy]; let mut doc = blank(1); - add_annotation(&mut doc, &comment(0, Shape::Line { from, to, start: LineEnding::None, end: LineEnding::OpenArrow }, Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width, fill: None }, String::new(), "Ada".into()), &meta("l".into())) + add_annotation(&mut doc, &comment(0, Shape::Line { from, to, start: LineEnding::None, end: LineEnding::OpenArrow }, Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width, fill: None, ..Style::default() }, String::new(), "Ada".into()), &meta("l".into())) .expect("adds"); let span = bounds_of(&[from, to]); for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { diff --git a/crates/annot/tests/round_trip.rs b/crates/annot/tests/round_trip.rs index 3e60601498..625cefc897 100644 --- a/crates/annot/tests/round_trip.rs +++ b/crates/annot/tests/round_trip.rs @@ -48,7 +48,7 @@ fn ann(page: usize, shape: Shape, contents: &str) -> NewAnnotation { } fn styled(page: usize, shape: Shape, contents: &str, color: [f64; 3], opacity: f64, width: f64, fill: Option<[f64; 3]>) -> NewAnnotation { - NewAnnotation { page, shape, style: Style { color, opacity, width, fill }, contents: contents.into(), author: "Ada".into() } + NewAnnotation { page, shape, style: Style { color, opacity, width, fill, ..Style::default() }, contents: contents.into(), author: "Ada".into() } } /// The dictionary of the `k`-th `/Annots` entry of a page. @@ -714,7 +714,7 @@ fn links_keep_page_targets_uri_and_dash_structure_across_renumbering() { &mut doc, 0, [120.0, 700.0, 240.0, 720.0], - &links::LinkAction::Page(2), + &links::LinkAction::Page { page: 2, zoom: links::LinkZoom::FitPage }, &links::LinkStyle { visible: true, width: 2.0, dashed: true, ..Default::default() }, ) .expect("links to page 3"); @@ -735,7 +735,7 @@ fn links_keep_page_targets_uri_and_dash_structure_across_renumbering() { .into_iter() .map(|l| { let a = match l.action { - links::LinkAction::Page(p) => format!("page {p}"), + links::LinkAction::Page { page: p, zoom: _ } => format!("page {p}"), links::LinkAction::Uri(u) => u, links::LinkAction::Other(o) => format!("other:{o}"), }; @@ -770,7 +770,14 @@ fn links_keep_page_targets_uri_and_dash_structure_across_renumbering() { fn removing_all_links_keeps_comments() { let mut doc = blank(2); add_annotation(&mut doc, &ann(0, Shape::Note { at: [50.0, 700.0], icon: NoteIcon::Help }, "note"), &meta("n")).expect("adds"); - links::add(&mut doc, 0, [200.0, 700.0, 300.0, 715.0], &links::LinkAction::Page(0), &links::LinkStyle::default()).expect("links"); + links::add( + &mut doc, + 0, + [200.0, 700.0, 300.0, 715.0], + &links::LinkAction::Page { page: 0, zoom: links::LinkZoom::FitPage }, + &links::LinkStyle::default(), + ) + .expect("links"); let removed = links::remove_all(&mut doc, None).expect("removes all links"); assert_eq!(removed, 1); @@ -980,7 +987,7 @@ fn a_full_rewrite_into_object_streams_describes_the_same_comments_as_a_plain_tab assert_eq!(summaries(&plain).len(), summaries(&packed).len()); // The plain-table output is bigger here: object streams compress, which is the point of them. - let opts = SaveOptions { mod_date: None, id_seed: 0x5052_494E_5443_5241, object_streams: true, drop_unlinked: false }; + let opts = SaveOptions { mod_date: None, id_seed: 0x5052_494E_5443_5241, object_streams: true, drop_unlinked: false, id_entropy: None }; let packed_bytes = pdfcraft_cos::write_full(&doc, &opts).expect("writes"); assert!( packed_bytes.len() < pdfcraft_cos::write_full(&doc, &SaveOptions { object_streams: false, ..opts }).expect("writes").len(), diff --git a/crates/annot/tests/support/fixture.rs b/crates/annot/tests/support/fixture.rs index 69567b34e9..1b466c4a54 100644 --- a/crates/annot/tests/support/fixture.rs +++ b/crates/annot/tests/support/fixture.rs @@ -118,7 +118,13 @@ pub fn save_and_reopen(doc: &Document, path: Save) -> (Vec, Document) { /// Save with the path's options. `/Info /ModDate` is left unset so a full save adds nothing that /// would make byte comparisons depend on the clock. pub fn save(doc: &Document, path: Save) -> Vec { - let opts = SaveOptions { mod_date: None, id_seed: 0x5052_494E_5443_5241, object_streams: path == Save::FullStreams, drop_unlinked: false }; + let opts = SaveOptions { + mod_date: None, + id_seed: 0x5052_494E_5443_5241, + object_streams: path == Save::FullStreams, + drop_unlinked: false, + id_entropy: None, + }; match path { Save::Incremental => write_incremental(doc, &opts), Save::FullTable | Save::FullStreams => write_full(doc, &opts), From ef2d06e303ab12ed100709e0f766173351c84bfc Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 05:13:23 -0500 Subject: [PATCH 12/14] build: preserve Cargo canonical XML package order --- Cargo.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d36b2a0752..73d02e026b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1578,19 +1578,19 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=e5f00f5bf658ef2cce7bf96fbbd769a6fff5bc72#e5f00f5bf658ef2cce7bf96fbbd769a6fff5bc72" +source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" dependencies = [ "quick-xml 0.41.0", - "serde", "thiserror 2.0.21", ] [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" +source = "git+https://github.com/storytold/craft-libs?rev=e5f00f5bf658ef2cce7bf96fbbd769a6fff5bc72#e5f00f5bf658ef2cce7bf96fbbd769a6fff5bc72" dependencies = [ "quick-xml 0.41.0", + "serde", "thiserror 2.0.21", ] From 2d2c2f7e183f00e44b0375a896edbb1b2acf860b Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:39:57 +0700 Subject: [PATCH 13/14] fix(xfdf): retain canonical Cargo lock package order (#7698) From 562c428c6ff89706bcbce96efce3f4c80d282b59 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:08:31 +0700 Subject: [PATCH 14/14] test(annot): retain current opacity and review-state semantics (#7721) * test(annot): retain current opacity and review-state semantics * style: wrap fixture imports at repository width --- crates/annot/tests/malformed.rs | 13 +++++++++---- crates/annot/tests/round_trip.rs | 9 +++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/annot/tests/malformed.rs b/crates/annot/tests/malformed.rs index 096a921298..6b8f007de7 100644 --- a/crates/annot/tests/malformed.rs +++ b/crates/annot/tests/malformed.rs @@ -294,15 +294,20 @@ fn reference_loops_terminate_and_a_delete_takes_the_cycle_with_it() { // ── wrong types where the reader wants a number or a name ───────────────────────────────────── /// Values of the wrong type in flag, opacity and colour entries: the reader falls back to its -/// defaults instead of panicking. NOTE on opacity: reading does **not** clamp, so `/CA 7.5` comes -/// back as 7.5 and would hand a nonsense value to a 0–1 slider; writing clamps (`add_annotation` -/// refuses non-finite and clamps the range). The asymmetry is recorded here, not endorsed. +/// defaults instead of panicking. Opacity is clamped on read to the supported 0-1 range; +/// the original malformed dictionary remains unchanged. #[test] fn wrong_typed_flag_opacity_and_colour_entries_fall_back_without_panicking() { let doc = one_annot(b"<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /CA 7.5 /F /NotAnInt /C [1 2] >>"); read_everything(&doc); let p = props(&doc, 0, 0).expect("still a comment"); - assert_eq!(p.opacity, 7.5, "the out-of-range opacity is passed through as written"); + assert_eq!(p.opacity, 1.0, "the out-of-range opacity is clamped on read"); + // Exercise both clamp edges and an unchanged interior value, not just a constant fallback. + for (raw, expected) in [("-0.5", 0.0), ("0.4", 0.4), ("1.5", 1.0)] { + let bytes = format!("<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /CA {raw} >>"); + let probe = one_annot(bytes.as_bytes()); + assert_eq!(props(&probe, 0, 0).expect("still a comment").opacity, expected, "opacity {raw}"); + } assert!(!p.locked, "a name where /F belongs reads as no flags at all"); assert_eq!(p.color, None, "/C with two components is not a colour"); let listed = summaries(&doc); diff --git a/crates/annot/tests/round_trip.rs b/crates/annot/tests/round_trip.rs index 625cefc897..dde7f8ebc1 100644 --- a/crates/annot/tests/round_trip.rs +++ b/crates/annot/tests/round_trip.rs @@ -33,8 +33,8 @@ use pdfcraft_cos::{Dict, Document, ObjRef, Object, SaveOptions}; use support::bytes; use support::fixture::{ - ALL_SAVES, FLAG_NO_ROTATE, FLAG_NO_ZOOM, FLAG_PRINT, FLAG_READONLY, Save, annot_entries, blank, blank_with_indirect_annots, dict_of, name_of, - num, nums, page_object_numbers, pdf, save_and_reopen, str_of, + ALL_SAVES, FLAG_HIDDEN, FLAG_NO_ROTATE, FLAG_NO_ZOOM, FLAG_PRINT, FLAG_READONLY, Save, annot_entries, blank, blank_with_indirect_annots, dict_of, + name_of, num, nums, page_object_numbers, pdf, save_and_reopen, str_of, }; /// A deterministic `/M` date and `/NM` id, so nothing here depends on the clock. @@ -658,8 +658,9 @@ fn reply_threads_and_the_delete_cascade_survive_every_save_path() { assert_eq!(str_of(&reopened, &st, b"State").as_deref(), Some("Accepted"), "{path:?}"); assert_eq!(str_of(&reopened, &st, b"StateModel").as_deref(), Some("Review"), "{path:?}"); assert_eq!(str_of(&reopened, &st, b"Subj").as_deref(), Some("Status"), "{path:?}"); - // Replies are printed but keep the icon flags of a comment; no Hidden bit here. - assert_eq!(st.int(b"F"), Some(FLAG_PRINT | FLAG_NO_ZOOM | FLAG_NO_ROTATE), "{path:?}"); + // Review-state records are hidden; ordinary text replies retain their visible comment flags. + assert_eq!(st.int(b"F"), Some(FLAG_HIDDEN | FLAG_PRINT | FLAG_NO_ZOOM | FLAG_NO_ROTATE), "{path:?}"); + assert_eq!(annot_at(&reopened, 0, 2, path).int(b"F"), Some(FLAG_PRINT | FLAG_NO_ZOOM | FLAG_NO_ROTATE), "{path:?}: ordinary reply"); let listed = summaries(&reopened); assert_eq!(listed.iter().filter(|s| s.in_reply_to.as_deref() == Some("p1")).count(), 2, "{path:?}: both replies name the parent's /NM"); assert_eq!(listed.iter().filter(|s| s.state.as_deref() == Some("Accepted")).count(), 1, "{path:?}");