Skip to content

Sniff ZIP central directories to identify OOXML documents - #169

Merged
jeremy merged 1 commit into
mainfrom
zip-central-directory-sniffing
Aug 19, 2026
Merged

jeremy merged 1 commit into
mainfrom
zip-central-directory-sniffing

Conversation

@jeremy

@jeremy jeremy commented Aug 19, 2026

Copy link
Copy Markdown
Member

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:

Both need what a byte-pattern table cannot express: parsing the ZIP structure.

Approach

Marcel::Magic::Zip layers a procedural refinement over the declarative magic result — the same architectural move Tika makes with its container-aware detector. MimeType.for_data calls Zip.refine(io, type) right after Magic.by_magic; refine returns the type untouched unless it's a ZIP-family container.

When it is, the sniffer:

  1. Requires seekable, sized content (unseekable pipes and prefix reads refine nothing — consistent with the documented partial-read boundary).
  2. Locates the end-of-central-directory record with a bounded tail read, following Zip64 records when the classic fields overflow.
  3. Walks central-directory entries, collecting only the six discriminating OPC part names. Member names only — nothing is decompressed.
  4. Classifies: word/document.xml / xl/workbook.xml / ppt/presentation.xml picks the family; a sibling vbaProject.bin upgrades 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:check passes 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-detecting sheet.macroenabled.12 would stop a .xltm filename from refining to template.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.rb builds archives in memory (stored entries, valid CRCs — no fixture blobs, no new dependencies):

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
jeremy force-pushed the zip-central-directory-sniffing branch from 52fc91d to ec509f8 Compare August 19, 2026 22:14
@jeremy
jeremy merged commit 097d677 into main Aug 19, 2026
20 checks passed
@jeremy
jeremy deleted the zip-central-directory-sniffing branch August 19, 2026 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant