fix: handle case-mismatched zip entry names in .docx files#1820
Open
octo-patch wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: handle case-mismatched zip entry names in .docx files#1820octo-patch wants to merge 1 commit intomicrosoft:mainfrom
octo-patch wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…rosoft#1812) Some .docx files produced by certain tools have inconsistent casing between the zip central directory and local file headers (e.g. 'customXml/item2.xml' vs 'customXML/item2.xml'). Python's zipfile raises BadZipFile on this mismatch, causing DocxConverter to throw FileConversionException. Add _fix_zip_name_casing() to pre_process.py that patches local file headers in memory to match the authoritative central directory before the zip is opened. Add a unit test that verifies the fix resolves the BadZipFile error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1812
Problem
Some
.docxfiles produced by certain tools (e.g. legal document systems) have inconsistent casing between the zip central directory and local file headers — for example, the central directory listscustomXml/item2.xmlbut the local file header containscustomXML/item2.xml. This is technically a violation of the zip spec, but is produced by certain versions of Microsoft Word and third-party tools.Python's
zipfilemodule strictly validates this match and raisesBadZipFile:Solution
Add
_fix_zip_name_casing()toconverter_utils/docx/pre_process.py. This function:bytearrayzipfile.ZipFile(which reads the central directory first — this succeeds even with mismatched local headers)header_offsetBytesIOif any headers were fixed, otherwise returns the original stream unchangedThe central directory is authoritative per the zip spec. The patch is safe: it only applies to case-only differences (same byte length in ASCII paths), so no offset recalculation is needed.
pre_process_docx()now calls_fix_zip_name_casing()as its first step, before opening the zip for math pre-processing.Testing
Added
test_fix_zip_name_casing()intest_module_misc.pythat:BadZipFile(reproduces the bug)_fix_zip_name_casing()and asserts both entries are now readable without error