Skip to content

JSE-71: inspect() checks a workbook without building cells - #142

Merged
securedeveloper merged 23 commits into
mainfrom
jse-71
Dec 2, 2026
Merged

securedeveloper merged 23 commits into
mainfrom
jse-71

Conversation

@securedeveloper

@securedeveloper securedeveloper commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Adds inspect(input, { limits, signal }) to javascript-excel/read. It checks an .xlsx, .xlsm, .xltx or .xltm file part by part without building cells, so a server or browser can accept or refuse an upload without reading it into the model.

Guarantee

Under the same limits, every file read refuses (with its default options), inspect refuses with the same error class, code and details. Every UNSUPPORTED_PART warning read gives, inspect returns, and its kind is in partKinds. Read options that read less (sheets, styles: false, formulas: false, richText, dates, dateZone, rawText) refuse nothing that inspect accepts. maxRows is outside the guarantee: a capped read counts cells whose shared string is missing toward maxCells. A test documents this.

Design

  • inspect runs readImpl itself through an internal inspect option. The order of checks is unchanged. The sheet and shared-string parsers run in a mode that converts, checks and counts each cell and string, then drops it. Merges, selections and row properties are counted, not kept. A hyperlink is kept only when its cell could exceed maxColumns. Anything readImpl later checks or reports (including relationship scanning) applies to inspect with no extra code.
  • After the read, every zip entry that was not parsed is inflated, so zip limits and the CRC apply to it. This includes optional parts whose malformed XML read forgave. The bytes of every such part are searched for <!DOCTYPE, whatever its name or content type. The search finds the UTF-8, UTF-16, UTF-32 and UTF-7 forms, across slice boundaries. It consults the signal and yields between 256 KB pieces.
  • Every XML part must be UTF-8 or UTF-16, and may carry a well-formed XML declaration only at its very start, or none. This includes the parts read parses. A part counts as XML by its content type, its name, or a relationship of a non-binary kind that targets it.
    • The same per-part check runs whether ctx.parse feeds it (parts read parses) or the byte scan does (parts it does not). It reads the first bytes, handles UTF-16 with no byte order mark, and gathers the declaration up to ?> across chunks, bounded by maxXmlTokenLength.
    • Anything else is refused with XML_MALFORMED: a disallowed encoding (details.encoding), a declaration that breaks the XMLDecl grammar (details.declaration), or a <?xml processing instruction anywhere but at the very start.
    • Refusals for parts read parses come only after read has succeeded, so read's own errors still come first.
  • Known false positives, documented in §5.2.1: a DOCTYPE inside a comment or CDATA section of a part read does not parse; an SVG with <!DOCTYPE svg …>; any binary part whose bytes contain <!DOCTYPE; and an ASCII part that declares a single-byte code page. None of these occur in the fixtures or the corpus, and a test asserts that.
  • Shared-formula masters: a master keeps its text only until a copy needs it, at most 8 MiB of UTF-8 per sheet; beyond that, the oldest are shaped first. Shaping is one allocation-free pass that keeps only numbers: the bounds, and whether copies fit maxCellTextLength. When the limit falls inside the range a copy's length can take, the master keeps per-reference records instead, held in an Int32Array outside the heap. Every live master and every record counts against maxItems; past it, inspect refuses with LIMIT_EXCEEDED and details.sharedFormulaMasters / sharedFormulaReferences. This is the one documented case where inspect can refuse before read would. read's own shared-formula code is identical to main.
  • Result: format, sheets (name, index, state, part, cells, dimension), partKinds, warnings (the UNSUPPORTED_PART warnings only, not capped), parts, inflatedBytes, cells, sharedStrings.
  • read is unchanged. The new modes are off by default, and the existing read tests pass.

Evidence

  • Shared-formula masters: 120,000 masters of 600 characters (72 MB of formula text) cost inspect 70 MB of peak heap, against 66 MB for 4-character masters. Before this change the 600-character file cost 126 MB.
  • Differential test (counts from before the review fixes; now 96 files, 207 runs, 31 refused only by inspect, all of them hostile): 91 files (fixtures, the conformance corpus, generated hostile files) under the default limits, a tight set and each file's own limits. That is 197 runs: 85 refused by both, 91 accepted by both, 21 refused only by inspect, plus 788 runs of read with reduced options.
  • Seeded fuzz: 3,000 mutated workbooks, seed 20261202 (part text, part list and relationships, zipped bytes, random tight limits). 2,238 refused by both, 679 accepted by both, 83 refused only by inspect. No case breaks the guarantee.
  • Memory (Node 26, peak heap): for a 1M-cell sheet, read uses about 120 MB and inspect about 30 MB (the same as for 100k cells); this is checked in a test. For a 19.8 MiB file with 3.42M cells, read uses about 410 MB and inspect about 30 MB. inspect passes with --max-old-space-size=32; read runs out of memory at 256 MB.

Not in this PR

  • inspect is not exposed through the worker client.
  • detectFormat still ignores limits and signal.

Docs: docs/api.md §2, §5.2, new §5.2.1, §8; package README; minor changeset.

Behaviour tests (result shape, part kinds, every refusal read makes, the
signal, peak heap against read on a 1,000,000-cell sheet), a differential
test against read over the fixtures, the conformance corpus and generated
hostile files, and a seeded fuzz of mutated workbooks.
inspect runs read's own pipeline with the sheet and shared-string parsers
in a mode that converts, checks and counts each cell and string and then
drops it, so it refuses what read refuses with the same error and reports
read's UNSUPPORTED_PART warnings. It then inflates every part read leaves
unopened (or stopped at malformed XML) and searches XML parts for a
DOCTYPE. read's output is unchanged.
…TYPE search

A sheet of 120,000 long shared-formula masters against short ones under a
relative heap bound; copies' resolution decided as read decides it for any
maxCellTextLength. XML parts in UTF-32, UTF-7, EBCDIC or declaring another
encoding; DOCTYPE in parts named or typed as images or binaries, in UTF-7
and UTF-32 forms and across a slice boundary. The differential test now
asserts no inspect-only refusal outside the hostile files.
… part; XML encodings

Without keepCells a shared-formula master keeps its position, length,
relative bounds and whether its copies fit maxCellTextLength (from the
range of lengths a copy can have), not its text, unless the limit falls
inside that range. inspect searches the bytes of every part read does not
parse for <!DOCTYPE (UTF-8, UTF-16, UTF-32 and UTF-7 forms) and refuses an
XML part (by content type, name or relationship) encoded in anything but
UTF-8 or UTF-16 with XML_MALFORMED and details.encoding, after the read has
succeeded. read's output is unchanged.
…masters, rels counted once

A sheet declaring UTF-7 after 1,100 spaces; the same sheet inflated as a
10-byte first chunk; unparsed parts with a long declaration and UTF-7
DOCTYPE spellings; an upper-case <?XML; a declaration longer than
maxXmlTokenLength; a short printable details.encoding; a file at exactly
read's maxTotalBytes; masters whose copy length range straddles a small
maxCellTextLength under a relative heap bound, and their reference-record
budget; shiftedLength against the text shiftCompiled builds.
…ds instead of formula text

The encoding of a part read parses comes from the tokenizer's declaration
token and its first four bytes gathered across chunks; an unparsed XML part
has its declaration gathered across chunks up to ?>, bounded by
maxXmlTokenLength. details.encoding is at most 40 printable characters.
Relationship parts read parsed are reused, not inflated again. A lean
master whose copies' length range straddles maxCellTextLength keeps one
record per distinct relative row or column (exact copy length without the
text), counted against maxItems with the read's other items; past it,
LIMIT_EXCEEDED with details.sharedFormulaReferences. read is unchanged.
…ntee rests on, and the reference-record budget
Conflicts: the warning tap and the relationships hook sit next to JSE-72's
warning 'parts' and malformed-relationship bookkeeping; docs/api.md keeps
JSE-72's security row and adds the inspect row with the new part kinds.
…overs JSE-72's kinds

read now parses every relationships part once (scanPackageParts): inspect
takes the XML targets from those relationships (canonical part keys,
alternative readings included) instead of parsing .rels parts itself, and
does not inflate them again. The differential test asserts that read and
inspect report the same warnings for activeX, embeddedPackage, customUI,
webExtension, unreferencedPart (with parts), invalidPartName, Excel's
xlMacrosheet and a VBA signature, and that equivalent part names fail with
the same ZIP_INVALID.
…clarations

400,000 cells each a master of its own si under a relative heap bound and
the maxItems budget; a 50,000 x 10 shared-formula sheet under maxItems
100,000; inspect's time against read's on masters without copies; a
redefined si releasing its records; XMLDecl grammar, declarations not at
the very start and BOM-less UTF-16 declarations on parsed and unparsed
parts; formulaShape against the compiler on 20,000 seeded formulas.
…eclarations

A master keeps its text (UTF-8, at most 8 MiB per sheet) until a copy needs
it, then numbers only from one allocation-free pass (formulaShape); every
live master and reference record counts against maxItems, and a redefined
si releases its records. read's shared-formula path is main's again.
Every XML part's start is read by one per-part check, fed by ctx.parse or
by the byte scan: first bytes, BOM-less UTF-16, the declaration held to the
XMLDecl grammar, and no <?xml instruction other than at the very start.
…g text, strict declarations and binary kinds
A master redefined before every copy (records each time) under relative
bounds on heap and external memory, and 300,000 redefinitions of one si
with a short master against as many ordinary formulas.
…s into the tests

release() drops the master's records, and the waiting queue is compacted
once masters that no longer wait outnumber those that do, so a replaced
master is unreachable. partsOf sorts in a reused buffer. lengthParts and
shiftedLengthRange, used only as oracles, move to shift-formula.test.ts.
Garbage awaiting collection differs by Node version (CI's Node 24 kept
26 MB of freed records external); the test now samples after a full
collection, so it measures what inspect still reaches: 241 MB external
without the fix, 2 to 4 MB with it.
A shared CI runner measured inspect at 109 ms against read's 62 for work
that is the same under the pending-text budget; best of five, and a 2x
bound under the budget.
…s measure reachable memory

A record set is three numbers per distinct relative part; a typed array
per master cost more than its records and sat outside the heap a worker's
cap counts. The test that masters keep no text now compares long masters
with short ones of the same structure, by reachable memory after a full
collection, so garbage the collector has not freed yet (more on Node 24)
does not count.
A single aborted read against best-of-three baselines failed on Node 24
when heavier test files ran beside it (174 ms against 157).
@securedeveloper
securedeveloper merged commit 0513d7d into main Dec 2, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant