DOCX: the parts of a package that cannot be read are listed on the document (#1902)

This commit is contained in:
Joseph Anglada authored and GitHub committed 2026-11-16 21:30:17 -05:00
1 parent 9e614b082f
commit b1d3abc55e
8 files changed
+90 -10

No files matched your search

+5
View File
@@ -233,6 +233,11 @@ impl Package {
self.opc.size()
}
/// The entries of the zip that could not be read and were left out, by name.
pub fn skipped(&self) -> &[String] {
self.opc.skipped()
}
/// A package from parts already in memory (a Flat OPC file, a converted document), by part
/// name without the leading `/`.
pub fn from_parts(files: BTreeMap<String, Vec<u8>>) -> Package {
+1
View File
@@ -370,6 +370,7 @@ fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize,
}
let (left, bytes, graphic) = (r.chunks.left, r.chunks.bytes, r.chunks.graphic);
let mut doc = r.doc;
doc.skipped_parts.extend(pkg.skipped().iter().cloned());
doc.ensure_nonempty();
Ok((doc, left, bytes, graphic))
}
+12 -4
View File
@@ -63,7 +63,7 @@ impl Reader<'_> {
let chunk = AltChunk { content_type: &ct, part: &part, bytes };
// A WordprocessingML chunk is a package; one that isn't Word's fails to read (logged).
let is_docx = bytes.starts_with(b"PK\x03\x04");
let doc = if is_docx { self.read_nested(bytes, depth) } else { self.chunks.import.and_then(|f| f(&chunk)) };
let doc = if is_docx { self.read_nested(bytes, depth, &part) } else { self.chunks.import.and_then(|f| f(&chunk)) };
let Some(mut doc) = doc else {
log::warn!("docx: alternative format chunk {part} ({ct}) is in a format WordCraft can't bring in");
return;
@@ -83,6 +83,8 @@ impl Reader<'_> {
// The pictures the chunk's own reader dropped are the file's too (strict export, #6005),
// each named by the chunk and its part in it (`word/afchunk1.docx > word/header1.xml`).
// A chunk in another format brings none: those importers record nothing yet (#6007).
let skipped = std::mem::take(&mut doc.skipped_parts);
self.doc.skipped_parts.extend(skipped.into_iter().map(|name| format!("{part} > {name}")));
let nested = std::mem::take(&mut doc.missing_pictures);
let list_len = nested.list.len() as u64;
for mut item in nested.list {
@@ -103,7 +105,7 @@ impl Reader<'_> {
/// A WordprocessingML chunk: its document, read with the same importer and what is left of
/// the budgets, its XML depth counting on from `depth`.
fn read_nested(&mut self, bytes: &[u8], depth: usize) -> Option<Document> {
fn read_nested(&mut self, bytes: &[u8], depth: usize, part: &str) -> Option<Document> {
if self.chunks.nesting >= MAX_CHUNK_NESTING {
log::warn!("docx: alternative format chunks nest deeper than {MAX_CHUNK_NESTING}; the inner ones are left out");
return None;
@@ -112,8 +114,14 @@ impl Reader<'_> {
Package::open_limited(bytes, MAX_PART, self.chunks.bytes).map_err(|e| log::warn!("docx: unreadable nested document chunk: {e}")).ok()?;
self.chunks.bytes = self.chunks.bytes.saturating_sub(pkg.size());
let state = ChunkState { nesting: self.chunks.nesting + 1, depth: depth + 1, ..self.chunks };
let (doc, left, bytes, graphic) =
super::read_opened_in(&pkg, state).map_err(|e| log::warn!("docx: unreadable nested document chunk: {e}")).ok()?;
let (doc, left, bytes, graphic) = match super::read_opened_in(&pkg, state) {
Ok(read) => read,
Err(e) => {
log::warn!("docx: unreadable nested document chunk: {e}");
self.doc.skipped_parts.extend(pkg.skipped().iter().map(|name| format!("{part} > {name}")));
return None;
}
};
self.chunks.left = left;
self.chunks.bytes = bytes;
self.chunks.graphic = graphic;
+26 -1
View File
@@ -48,7 +48,7 @@ fn package_styled(body: &str, chunk: Option<(&str, &str, &[u8])>, styles: Option
files.push(("[Content_Types].xml".into(), types.into_bytes()));
let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
for (name, bytes) in files {
zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap();
zw.start_file(name, zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored)).unwrap();
zw.write_all(&bytes).unwrap();
}
zw.finish().unwrap().into_inner()
@@ -76,6 +76,19 @@ fn a_chunk_comes_in_through_the_importer_where_it_stands() {
assert_eq!(body_lines(&read(&bytes).unwrap()), ["Before", "After"]);
}
#[test]
fn unreadable_nested_main_part_is_reported_with_its_chunk() {
let mut inner = package(&p("Chunk"), None);
let document = inner.windows(b"<w:document".len()).position(|w| w == b"<w:document").unwrap();
inner[document] ^= 1; // A bad ZIP checksum makes the required main part unreadable.
let outer = package(&format!("{}{}", p("Before"), CHUNK), Some(("docx", DOCX_MAIN, &inner)));
let d = read(&outer).unwrap();
assert_eq!(d.skipped_parts, vec!["word/afchunk.docx > word/document.xml".to_string()]);
assert_eq!(body_lines(&d), ["Before"]);
}
#[test]
fn a_bookmark_opened_before_a_chunk_at_the_start_of_the_body_is_kept() {
let body = format!(r#"<w:bookmarkStart w:id="0" w:name="Here"/>{CHUNK}<w:bookmarkEnd w:id="0"/>{}"#, p("After"));
@@ -444,3 +457,15 @@ fn a_chunk_imported_twice_counts_its_dropped_pictures_past_the_list_once() {
assert_eq!(d.missing_pictures.list.len(), craft_wordmodel::MAX_MISSING_PICTURES);
assert_eq!(d.missing_pictures.total, n as u64);
}
#[test]
fn a_part_a_chunk_could_not_read_is_listed_with_the_chunk() {
let bytes = package(&format!("{}{CHUNK}", p("Before")), Some(("htm", "application/xhtml+xml", b"<p>x</p>")));
let import = |_: &AltChunk| {
let mut d = Document::from_text("Chunk");
d.skipped_parts = vec!["word/media/image1.png".to_string()];
Some(d)
};
let d = read_with(&bytes, &ReadOptions { chunks: Some(&import), ..Default::default() }).unwrap();
assert_eq!(d.skipped_parts, vec!["word/afchunk.htm > word/media/image1.png".to_string()]);
}
+25
View File
@@ -87,6 +87,31 @@ fn a_malformed_main_part_is_an_error_not_a_truncated_document() {
assert_eq!(d.plain_text(craft_wordmodel::StoryRef::Body), "a <- b\nlast", "well-formed text still reads whole");
}
#[test]
fn a_part_with_a_bad_checksum_is_left_out_and_reported() {
let body = format!("<w:document {W_NS}><w:body><w:p><w:r><w:t>kept</w:t></w:r></w:p></w:body></w:document>");
let build = |extra: &[u8]| {
let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
zw.start_file("_rels/.rels", stored).unwrap();
zw.write_all(ROOT_RELS.as_bytes()).unwrap();
zw.start_file("word/document.xml", stored).unwrap();
zw.write_all(body.as_bytes()).unwrap();
zw.start_file("word/media/image1.png", stored).unwrap();
zw.write_all(extra).unwrap();
zw.finish().unwrap().into_inner()
};
let sound = read(&build(b"PICTURE-BYTES-0123456789")).unwrap();
assert!(sound.skipped_parts.is_empty(), "a sound file reports nothing");
// The entry is stored as is, so one changed byte fails its checksum when it is read.
let mut z = build(b"PICTURE-BYTES-0123456789");
let at = z.windows(7).position(|w| w == b"PICTURE").unwrap();
z[at] = b'p';
let damaged = read(&z).unwrap();
assert_eq!(damaged.plain_text(craft_wordmodel::StoryRef::Body), "kept", "the rest still opens");
assert_eq!(damaged.skipped_parts, vec!["word/media/image1.png".to_string()]);
}
#[test]
fn deep_nesting_is_rejected() {
let mut x = format!("<w:document {W_NS}><w:body>");
+14 -5
View File
@@ -39,6 +39,8 @@ pub struct Package {
/// Lowercased part name → its stored spelling: part names are case-insensitive (ECMA-376
/// Part 2 §6.2.2.3), so every spelling is one part.
lower: HashMap<String, String>,
/// Names of the entries the archive could not produce, in archive order.
skipped: Vec<String>,
}
impl Package {
@@ -55,6 +57,7 @@ impl Package {
let mut zip = open_zip(bytes, limits, u64::MAX)?;
let skip = limits.oversized == Oversized::Skip;
let mut files = BTreeMap::new();
let mut skipped = Vec::new();
let (mut total, mut work) = (0u64, 0u64);
let entries: Vec<String> = zip.names().map(|n| n.into_owned()).collect();
for (i, entry) in entries.iter().enumerate() {
@@ -97,19 +100,20 @@ impl Package {
Err(e) => {
work = work.saturating_add(size);
log::warn!("opc: zip entry {name} unreadable: {e}");
skipped.push(name);
}
}
}
Ok(Package::new(files))
Ok(Package::new(files, skipped))
}
/// A package from parts already in memory (a Flat OPC file, a converted document), by part
/// name.
pub fn from_parts(files: BTreeMap<String, Vec<u8>>) -> Package {
Package::new(files.into_iter().map(|(name, bytes)| (stored_name(&name), Arc::new(bytes))).collect())
Package::new(files.into_iter().map(|(name, bytes)| (stored_name(&name), Arc::new(bytes))).collect(), Vec::new())
}
fn new(mut files: BTreeMap<String, Arc<Vec<u8>>>) -> Package {
fn new(mut files: BTreeMap<String, Arc<Vec<u8>>>, skipped: Vec<String>) -> Package {
let mut lower: HashMap<String, String> = HashMap::new();
let mut collides = false;
for name in files.keys() {
@@ -121,7 +125,7 @@ impl Package {
}
}
if !collides {
return Package { files, lower };
return Package { files, lower, skipped };
}
// Entries whose names differ only in case are one part, so every spelling of the name
// reaches the same stored entry. The spelling kept is the one a relationship names, so
@@ -140,7 +144,12 @@ impl Package {
files.retain(|name, _| lower.get(&name.to_ascii_lowercase()) == Some(name));
let dropped = before.saturating_sub(files.len());
log::warn!("opc: {dropped} zip entries are other spellings of parts already read; they are left out");
Package { files, lower }
Package { files, lower, skipped }
}
/// The entries of the archive that could not be read and were left out, by name.
pub fn skipped(&self) -> &[String] {
&self.skipped
}
/// The bytes of all its parts.
+4
View File
@@ -78,6 +78,7 @@ impl Document {
media_index: MediaIndex::default(),
kept: Default::default(),
missing_pictures: Default::default(),
skipped_parts: Vec::new(),
}
}
@@ -156,6 +157,7 @@ impl Document {
media_index: _,
kept: _,
missing_pictures: _,
skipped_parts,
} = self;
let Document {
body: other_body,
@@ -177,6 +179,7 @@ impl Document {
media_index: _,
kept: _,
missing_pictures: _,
skipped_parts: other_skipped_parts,
} = other;
same_blocks(body, other_body)
&& same_parts(parts, other_parts)
@@ -194,6 +197,7 @@ impl Document {
&& same_bytes(media, other_media)
&& same_bytes(passthrough, other_passthrough)
&& same_glossary(glossary, other_glossary)
&& skipped_parts == other_skipped_parts
}
pub fn story(&self, s: StoryRef) -> Option<&Blocks> {
+3
View File
@@ -144,6 +144,9 @@ pub struct Document {
/// export reports them); never saved, never drawn, ignored by `==`.
#[serde(skip)]
pub missing_pictures: MissingPictures,
/// Package parts a reader could not read and left out, by name; never saved.
#[serde(skip)]
pub skipped_parts: Vec<String>,
}
#[cfg(test)]