From 29b989af67fd97edbc41d88cda23eaffb8438e59 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Wed, 18 Nov 2026 08:23:11 +0100 Subject: [PATCH] fix(data): reject malformed ODS nesting and scope ZIP offset inference - Refuse nested cells and annotations during ODS preflight. - Preserve the recorded ZIP directory lookup for non-spreadsheet readers. --- crates/data/src/sheets.rs | 17 +++++++++++++++ crates/data/src/sheets/ods.rs | 11 ++++++++-- crates/data/src/zip.rs | 39 +++++++++++++++++++++-------------- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/crates/data/src/sheets.rs b/crates/data/src/sheets.rs index 8b0e2a46b..09dc6cf90 100644 --- a/crates/data/src/sheets.rs +++ b/crates/data/src/sheets.rs @@ -932,6 +932,23 @@ mod tests { assert!(report.messages.is_empty()); } + #[test] + fn ods_rejects_malformed_nesting_but_preserves_ordinary_annotated_text() { + let book = |content| { + ods(&format!( + r#"{content}"# + )) + }; + refused_before_calamine(&book(""), "nested annotation"); + for cell in ["", ""] { + refused_before_calamine(&book(cell), "nested cell"); + } + let clean = book("notevalue"); + let (ds, report) = read(&clean, "book", None, &CsvOptions::default()).unwrap(); + assert_eq!(ds.value(0, 0), &Value::Text("value".into())); + assert!(report.messages.is_empty()); + } + #[test] fn ordinary_repeats_preambles_and_mixed_case_members_keep_values_and_notices() { for null in [false, true] { diff --git a/crates/data/src/sheets/ods.rs b/crates/data/src/sheets/ods.rs index 52d15eec8..03148b240 100644 --- a/crates/data/src/sheets/ods.rs +++ b/crates/data/src/sheets/ods.rs @@ -122,12 +122,19 @@ pub(super) fn check(bytes: &[u8], cell_limit: usize, byte_limit: usize) -> Resul if let Some(c) = cell.as_mut() { match &event { Event::Start(e) => { + let name = e.name(); + if matches!(name.as_ref(), "table:table-cell" | "table:covered-table-cell") { + return Err(xml_error("nested cell")); + } c.depth = add(c.depth, 1, bytes.len())?; - if e.name().as_ref() == "office:annotation" && c.annotation.is_none() { + if name.as_ref() == "office:annotation" { + if c.annotation.is_some() { + return Err(xml_error("nested annotation")); + } c.annotation = Some(c.depth); } if c.string && c.annotation.is_none() { - let n = if e.name().as_ref() == "text:s" { repeat(e, "text:c", byte_limit)? } else { 1 }; + let n = if name.as_ref() == "text:s" { repeat(e, "text:c", byte_limit)? } else { 1 }; c.bytes = add(c.bytes, n, byte_limit)?; } } diff --git a/crates/data/src/zip.rs b/crates/data/src/zip.rs index 6894ed6f1..af45ec0e6 100644 --- a/crates/data/src/zip.rs +++ b/crates/data/src/zip.rs @@ -248,23 +248,22 @@ fn read_within(bytes: &[u8], want: impl Fn(&str) -> bool, budget: usize, spreads if count > MAX_ENTRIES { return Err(Failure::Member(format!("the archive holds {count} entries; at most {MAX_ENTRIES} can be read"))); } - let mut p = u32_at(bytes, add(&[eocd, 16]).ok_or_else(bad)?).ok_or_else(bad)?; - // Like calamine's ZIP reader, allow a preamble whose bytes weren't included in the - // recorded offsets, as well as archives whose offsets already include those bytes. - let central_size = u32_at(bytes, add(&[eocd, 12]).ok_or_else(bad)?).ok_or_else(bad)?; - let central_end = if spreadsheet { eocd.saturating_sub(3) } else { eocd }; - let abutting = eocd.checked_sub(central_size).filter(|start| *start >= p && (!spreadsheet || *start < central_end)); - let central = if count == 0 { - p - } else if let Some(start) = abutting.filter(|start| u32_at(bytes, *start) == Some(0x0201_4B50)) { - start + let recorded = u32_at(bytes, add(&[eocd, 16]).ok_or_else(bad)?).ok_or_else(bad)?; + let central = if spreadsheet && count != 0 { + // Like calamine's ZIP reader, allow a preamble not included in recorded offsets. + let size = u32_at(bytes, add(&[eocd, 12]).ok_or_else(bad)?).ok_or_else(bad)?; + let end = eocd.saturating_sub(3); + let is_header = |start| u32_at(bytes, start) == Some(0x0201_4B50); + eocd.checked_sub(size) + .filter(|&start| start >= recorded && start < end && is_header(start)) + // A digital-signature trailer may separate the directory from EOCD. + .or_else(|| (recorded..end).find(|&start| is_header(start))) + .ok_or_else(bad)? } else { - // A digital-signature trailer may separate the directory from EOCD. Match - // calamine's ZIP lookup: otherwise take the first central header from p. - (p..central_end).find(|start| u32_at(bytes, *start) == Some(0x0201_4B50)).ok_or_else(bad)? + recorded }; - let offset = central.checked_sub(p).ok_or_else(bad)?; - p = central; + let offset = central.checked_sub(recorded).ok_or_else(bad)?; + let mut p = central; let mut out = Vec::new(); for _ in 0..count { if spreadsheet && crate::progress::check().is_err() { @@ -372,6 +371,16 @@ mod tests { } } + #[test] + fn only_spreadsheet_reads_infer_unrecorded_zip_preambles() { + let entries = [("a.txt".to_string(), b"hello".to_vec())]; + let archive = write(&entries).unwrap(); + assert_eq!(read(&archive).unwrap(), entries); + let prefixed = [b"preamble".as_slice(), &archive].concat(); + assert!(read(&prefixed).is_err()); + assert!(matches!(read_all_within(&prefixed, 5), Ok(actual) if actual == entries)); + } + #[test] fn round_trip() { let big = "RustGIS ".repeat(1000).into_bytes();