Repository navigation
Sniff ZIP central directories to identify OOXML documents - #169
Merged
Merged
Conversation
The declarative magic tables scan a bounded prefix of the file: matchers are capped at 64KB offsets when the tables are generated. Two OOXML failures follow from that ceiling: * Documents whose [Content_Types].xml is stored past byte 65536 (such as large Google-exported spreadsheets) fall back to application/zip (#59). * Macro-enabled documents are byte-identical to their non-macro peers within any prefix, so .docm/.xlsm/.pptm content collapses to the non-macro type, recoverable only from the filename (#115). Both need what a byte-offset pattern table cannot express: parsing the ZIP structure. Marcel::Magic::Zip layers a procedural refinement over the declarative magic result. When magic identifies a ZIP-family container, it locates the end-of-central-directory record (a bounded read from the end of seekable content, following Zip64 records when the classic fields overflow), walks the central directory entries, and classifies the document from standardized OPC part names: the main part (word/document.xml, xl/workbook.xml, ppt/presentation.xml) picks the family, and a sibling vbaProject.bin part upgrades it to the macro-enabled type. Member names only; nothing is decompressed. The refinement is fail-closed and bounded throughout: unseekable IOs, partial prefix reads, truncated or malformed archives, and multi-disk archives refine nothing, leaving the magic result untouched. The backward EOCD scan is candidate-capped so a trailer stuffed with signature bytes cannot force a quadratic scan, and the directory walk caps both bytes read and entries visited. A 12MB spreadsheet classifies in under a millisecond. The 64KB build-time ceiling on the magic tables stays: it guards the declarative matchers, which are unchanged. Template and add-in macro types gain their family's macro document type as a parent so filename hints keep refining content-detected macro types (.xltm over content-sniffed sheet.macroenabled.12), mirroring how their non-macro peers already refine the non-macro document types. Fixes #59 Fixes #115
jeremy
force-pushed
the
zip-central-directory-sniffing
branch
from
August 19, 2026 22:14
52fc91d to
ec509f8
Compare
This was referenced Aug 20, 2026
Merged
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 #59. Fixes #115.
Problem
Marcel's magic engine is a declarative byte-offset/pattern matcher, and its matchers are capped at 64KB offsets at table-generation time. Two OOXML detection failures follow:
[Content_Types].xmlis stored past byte 65536 (large Google-exported spreadsheets, for example) fall back toapplication/zip. The ceiling is enforced at build time, so it can't simply be widened..docm/.xlsm/.pptmcontent collapses to the non-macro type. Macro-enabled is only ever recovered from the filename — the one hint an uploader controls.Both need what a byte-pattern table cannot express: parsing the ZIP structure.
Approach
Marcel::Magic::Ziplayers a procedural refinement over the declarative magic result — the same architectural move Tika makes with its container-aware detector.MimeType.for_datacallsZip.refine(io, type)right afterMagic.by_magic;refinereturns the type untouched unless it's a ZIP-family container.When it is, the sniffer:
word/document.xml/xl/workbook.xml/ppt/presentation.xmlpicks the family; a siblingvbaProject.binupgrades it to the macro-enabled type. No OOXML parts → the base type stands, so plain zips are never upgraded.Fail-closed and bounded throughout: truncated, malformed, and multi-disk archives refine nothing; the backward EOCD scan is candidate-capped so a trailer stuffed with signature bytes can't force a quadratic scan; the directory walk caps both bytes read and entries visited, stopping early once a family and its macro part are both seen. A 12MB spreadsheet classifies in ~0.6ms.
The 64KB build-time ceiling stays — it guards the declarative tables, which are unchanged (
tables:checkpasses untouched).Parent-graph addition
Template and add-in macro types (
.dotm,.xltm,.xlam,.potm,.ppsm,.ppam) gain their family's macro document type as an additional parent. Without this, content-detectingsheet.macroenabled.12would stop a.xltmfilename from refining totemplate.macroenabled.12(it was only a child of the non-macro sheet type). This mirrors how the non-macro template types already parent to the non-macro document types.Tests
test/zip_sniffing_test.rbbuilds archives in memory (stored entries, valid CRCs — no fixture blobs, no new dependencies):[Content_Types].xmlpast 64KB → specific OOXML type for all three families (Google Docs/Sheets/PPT detection is being limited to 64kb (offset 30:65536) #59)vbaProject.binpresent → macro-enabled type from content alone, for all three families (Macro-enabled Excel workbooks incorrectly identified as non-macro-enabled content type #115).xlsxname → macro type wins (the spoofing case).xltmname → template macro type still refinesapplication/zip[](stdlib only; the sniffer itself needs nothing beyondStringIO)