Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crates/data/src/sheets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"<table:table table:name="S"><table:table-row><table:table-cell office:string-value="message"/></table:table-row><table:table-row><table:table-cell office:value-type="string">{content}</table:table-cell></table:table-row></table:table>"#
))
};
refused_before_calamine(&book("<office:annotation><office:annotation/></office:annotation>"), "nested annotation");
for cell in ["<table:table-cell/>", "<table:covered-table-cell/>"] {
refused_before_calamine(&book(cell), "nested cell");
}
let clean = book("<office:annotation><text:p>note</text:p></office:annotation><text:p><text:span>value</text:span></text:p>");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage gap on the "clean input gives no notice" side. The nested-annotation check depends on the open annotation being cleared at its matching End. This clean case only has one annotation per cell, so a regression that rejects two sibling annotations would not be caught.

Suggest adding a case such as <text:p>a<office:annotation><text:p>x</text:p></office:annotation>b<office:annotation><text:p>y</text:p></office:annotation></text:p> that must import with no notices. Confidence that the current code handles it correctly: high. The gap is in the tests.

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] {
Expand Down
11 changes: 9 additions & 2 deletions crates/data/src/sheets/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
}
Expand Down
39 changes: 24 additions & 15 deletions crates/data/src/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behavior change for non-spreadsheet ZIP readers (KMZ and others). Before this PR, read inferred a preamble for any archive whose recorded offsets missed it. Now an archive with unrecorded leading bytes fails with not a zip archive (or a damaged one). The archive is valid, so that message is misleading, and nothing tells the user why the file was refused.

The PR says this is intended, so I'm not calling it a bug. Please either give this path a clearer error (for example, "the recorded directory offset doesn't match the archive layout; leading bytes aren't supported") or name the regression in the PR description. Confidence: medium. I didn't find a fixture that covers a KMZ-style archive with a preamble.

};
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() {
Expand Down Expand Up @@ -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();
Expand Down
Loading