Repository navigation
JSE-71: inspect() checks a workbook without building cells - #142
Merged
Merged
Conversation
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.
… positives and maxItems
…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).
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.
Adds
inspect(input, { limits, signal })tojavascript-excel/read. It checks an.xlsx,.xlsm,.xltxor.xltmfile 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
readrefuses (with its default options),inspectrefuses with the same error class,codeanddetails. EveryUNSUPPORTED_PARTwarningreadgives,inspectreturns, and its kind is inpartKinds. Read options that read less (sheets,styles: false,formulas: false,richText,dates,dateZone,rawText) refuse nothing thatinspectaccepts.maxRowsis outside the guarantee: a capped read counts cells whose shared string is missing towardmaxCells. A test documents this.Design
inspectrunsreadImplitself through an internalinspectoption. 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 exceedmaxColumns. AnythingreadImpllater checks or reports (including relationship scanning) applies toinspectwith no extra code.readforgave. 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.readparses. A part counts as XML by its content type, its name, or a relationship of a non-binary kind that targets it.ctx.parsefeeds it (partsreadparses) 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 bymaxXmlTokenLength.XML_MALFORMED: a disallowed encoding (details.encoding), a declaration that breaks the XMLDecl grammar (details.declaration), or a<?xmlprocessing instruction anywhere but at the very start.readparses come only afterreadhas succeeded, soread's own errors still come first.readdoes 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.maxCellTextLength. When the limit falls inside the range a copy's length can take, the master keeps per-reference records instead, held in anInt32Arrayoutside the heap. Every live master and every record counts againstmaxItems; past it,inspectrefuses withLIMIT_EXCEEDEDanddetails.sharedFormulaMasters/sharedFormulaReferences. This is the one documented case whereinspectcan refuse beforereadwould.read's own shared-formula code is identical to main.format,sheets(name, index, state, part, cells, dimension),partKinds,warnings(theUNSUPPORTED_PARTwarnings only, not capped),parts,inflatedBytes,cells,sharedStrings.readis unchanged. The new modes are off by default, and the existing read tests pass.Evidence
inspect70 MB of peak heap, against 66 MB for 4-character masters. Before this change the 600-character file cost 126 MB.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 byinspect, plus 788 runs ofreadwith reduced options.inspect. No case breaks the guarantee.readuses about 120 MB andinspectabout 30 MB (the same as for 100k cells); this is checked in a test. For a 19.8 MiB file with 3.42M cells,readuses about 410 MB andinspectabout 30 MB.inspectpasses with--max-old-space-size=32;readruns out of memory at 256 MB.Not in this PR
inspectis not exposed through the worker client.detectFormatstill ignores limits and signal.Docs:
docs/api.md§2, §5.2, new §5.2.1, §8; package README; minor changeset.