Repository navigation
fix(data): reject malformed ODS nesting and scope ZIP offset inference #2389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, 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() { | ||
|
|
@@ -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(); | ||
|
|
||
There was a problem hiding this comment.
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.