Skip to content

JSE-62: bounded shared formulas (scan once, Excel's limit for masters, copies under maxCellTextLength), importDataset without shifting - #138

Merged
securedeveloper merged 3 commits into
mainfrom
fix/jse-62-shared-formulas
Dec 1, 2026
Merged

securedeveloper merged 3 commits into
mainfrom
fix/jse-62-shared-formulas

Conversation

@securedeveloper

@securedeveloper securedeveloper commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Gap

Two security reviews (on 2.2.0 and 2.3.0) measured:

  1. Shared-formula expansion multiplied work and memory. Every cell that shares a formula rescanned the master character by character (shiftFormula) and built its text with out +=, a rope of millions of pieces. A master <f t="shared" si="0"> of 1,048,576 characters shared by N cells: 10 cells 2.2 s / 314 MB, 100 cells 26.7 s / 2.9 GB, 1,000 cells out of memory at 4 GB, from a 3-4 KB file. Each copy is about 1.7 times the master, past maxCellTextLength, which did not count copies.
  2. Still slow within maxCellTextLength: 1000. 1,000 rows × 100 cells sharing one 999-character formula (428 KB) took 48.6 s and 3.8 GB over five reads in the consumer's page; 10,000 × 90 gave no result in 10 minutes.
  3. importDataset always read with formulas: true and shifted every shared formula, though the dataset only uses values; callers could not opt out. read with formulas: false reads the same file in about 0.1 s.

Design

The smallest change that makes the measured files fast, in three parts:

  • Scan once, build once. compileFormula scans a master once into the text between its references and the references (cell, column range, row range; anchors and numbers parsed). A reference with only $ parts is the same in every cell and is folded into the text; equal references share one entry. A cell that shares the formula then costs its distinct references plus its own length: canShift tells in constant time (min/max of the relative rows and columns) whether every reference stays on the sheet, and shiftCompiled computes the copy's length first and builds it once as a flat string (join). The master is compiled when the first cell needs it and is replaced when a new master takes the same si. The scanner is the 2.3.0 one, split into a parse step that does not depend on the offset and a format step, so results are the same (a seeded test compares it with the 2.3.0 shifter on 20,000 random formulas and offsets, and the fuzz below).

  • Excel's formula limit for masters. A master longer than 8,192 characters (MAX_SHARED_FORMULA_LENGTH, Excel's own limit) is not expanded: the cells that share it keep their cached values with the existing warning SHARED_FORMULA_UNRESOLVED (data loss), whose message names the master cell and its length: The shared formula of cell A2 could not be resolved: its master formula in cell A1 has 1048576 characters, more than Excel's limit of 8192; only its cached value was read. No new code, so ExcelWarningCode does not widen; the master keeps its own text, as any formula within maxCellTextLength.

  • Copies are cell text. A shifted copy's length is known before it is built; one longer than maxCellTextLength is not built, and the cell keeps its cached value with SHARED_FORMULA_UNRESOLVED (soft since the review round; see below).

  • importDataset does not shift. It reads through an internal read option (InternalReadOptions.sharedFormulaText: 'master', not public) that gives a cell sharing a formula the master's text unshifted. What the pipeline uses formulas for:

    • whether a cell has a formula at all: a formula cell without a cached value still counts as filled (toDataset's empty-row test, detectHeader's cell kind), and is kept as a cell rather than dropped;
    • for skipTotals: 'auto', whether every formula in the last data row is SUBTOTAL(…) (/^\s*=?\s*SUBTOTAL\s*\(/i).

    Both are the same for the master's text as for each shifted copy: a copy exists exactly when the shift succeeds (the reader still runs canShift, constant time, so unresolvable cells keep warning and lose their formula as before), shifting never touches the leading =, spaces or the function name, and the master is never empty. Nothing else in the result carries formula text. The master is compiled once for canShift, at most 8,192 characters.

Lazy translation was considered and left out. Keeping formula as an accessor that shifts on access would keep the keys and values but not plain data: property descriptors change, libraries that copy descriptors or turn getters into computed values (immer in strict mode, MobX) behave differently, a frozen cell could not cache, and a worker transfer or JSON.stringify builds every copy anyway. So read with formulas still returns one string per cell (see "What remains").

Measurements

Node 26.13.1, Apple-silicon laptop, one process per row, --max-old-space-size=4096, files built by a probe script (same shapes as the tests). RSS is the process peak. Two rounds were taken; the second ran while other jobs loaded the machine, so its times are higher, and the slower files were run only there.

File API 2.3.0 This branch
2.5 KB, 1,048,576-character master, 10 children read 1.3 s, 428 MB, 12.2 M characters of formulas 42 ms, 69 MB (10 warnings)
3.1 KB, same, 100 children read 15.2 s, 3.1 GB, 138.8 M characters 88 ms, 70 MB
8.5 KB, same, 1,000 children read out of memory (abort) at 4 GB 0.05 to 0.1 s, 78 MB (1,000 warnings)
same importDataset out of memory (abort) at 4 GB 77 to 90 ms, 79 MB
574 KB, 1,000 × 100 cells sharing a 999-character formula importDataset, maxCellTextLength: 1000 5.4 s, 1.9 GB (loaded: 11.9 s) 0.35 s, 140 MB (loaded: 0.62 s)
same read, maxCellTextLength: 1000 5.6 s, 1.9 GB (loaded: 12.1 s) 1.2 to 1.4 s, 240 MB (loaded: 1.8 s); 100,000 formulas, 99.9 M characters
same read, formulas: false 0.1 s, 117 MB unchanged
5.7 MB, 10,000 × 90 cells, same formula importDataset, maxCellTextLength: 1000 out of memory (abort) at 4 GB (the consumer: no result in 10 minutes) 2.4 s, 387 MB (loaded: 3.8 s)
same read, formulas: false 1.5 s, 381 MB 1.4 s, 380 MB
same read, maxCellTextLength: 1000 out of memory (abort) at 4 GB 10.6 s, 1.16 GB; 900,000 formulas, 899.1 M characters

The 999-character formula is IF(AA10000>0,AB10000*2,$C$1)+… (70 relative references), whose copies keep their length. With a formula whose copies grow past the limit, read is refused at the first such cell.

The 10,000 × 90 importDataset time is the cost of 900,000 cells (the formulas: false read alone is 1.4 s); no formula is shifted.

What remains

read with formulas returns one string per cell that shares a formula, so its output grows with the number of such cells times their length: 900 million characters for the 10,000 × 90 file, about 11 s and 1.2 GB. That is now proportional to what it returns, and bounded per cell by min(maxCellTextLength, about 5 × 8,192), but not in total except through maxCells. The docs (§8) say so, and the server guidance now recommends formulas: false when formula text is not needed. A total budget for built formula text would need a new limit; not part of this change.

Tests

Written first. The red run on the unchanged source (with only the two new exports stubbed so the files load; the 10,000 × 90 import and the 1,000-child cases left out because 2.3.0 does not finish them): 6 failed (the 1,048,576-character masters with 10 and 100 children, the 8,192/8,193 boundary, the refusal of a copy over the limit, the 1,000 × 100 work counts in read and in importDataset, which took 35 s there), 13 passed (the regression guards: ordinary shifting, edges, totals rows, formulas: false). Then green.

  • test/read/shared-formulas.test.ts: masters of 1,048,576 characters with 10, 100 and 1,000 children under default limits (files under 16 KB; children keep cached values; one warning each, message pinned; no compile and no shift: shiftWork all zero; time bound 5 s); 8,192 expands, 8,193 does not; formulas: false warns nothing; a copy at the limit reads, one over is refused with details { value: max + 1, max, ref } and nothing built; 1,000 × 100 with maxCellTextLength: 1000: exact copies at A2, B3 and CV1001, 100,000 formulas, 1 compile of 999 characters, 99,999 shifts building exactly 99,999 × 999 characters (time bound 20 s, measured 1.1 to 1.7 s in vitest); 10,000 × 90 with formulas off (no work) and with a formula whose copies grow (refused at AA2 after 25 shifts); relative, absolute, mixed, ranges, other-sheet and quoted-sheet references, strings with reference-like text, names, structured references; the last row and column shift, one further is unresolved; a master redefined with the same si.
  • test/read/shift-formula.test.ts: the compiled shifter equals the 2.3.0 shifter (copied into the test as the reference) on 20,000 seeded random formulas and offsets, including canShift and the length-before-building result; normalisation as before (a1+$a$01 → B2+$A$1); equal references formatted once; length known before building.
  • test/import/shared-formulas.test.ts: 1,000 × 100 and 10,000 × 90 with maxCellTextLength: 1000 through importDataset: values, no issues or warnings, shiftWork = 1 compile of 999 characters and 0 shifts (time bounds 10 s and 60 s; measured 0.4 s and 3.4 s in vitest); the 1,000-child master; skipTotals: 'auto' with plain SUBTOTAL formulas, a shared SUBTOTAL master and child, shared children of a master elsewhere, a shared non-SUBTOTAL formula, SUBTOTAL next to a shared child that is not, and next to a shared child whose references leave the sheet (no formula, warning), each compared with toDataset on the full read and with the full read's warnings; formula cells without cached values still count.

Local: npm run typecheck; npm -w packages/javascript-excel run test: 1,312 passed, 51 skipped, 1 failed: test/format/libreoffice.test.ts (number-format probes against the local LibreOffice, unrelated to formulas; it is not in the required job and was reported failing the same way locally in #132); npm run build, npm run lint, npm run smoke, dependency guard, npm run docs:check.

Differential fuzz against 2.3.0

A seeded generator of workbooks with one or two sheets, at the top-left and near the last row and column: shared masters (with ref, si 0 to 3, redefined), shared children (including si without a master and children of masters over 8,192 characters, about 3% of masters), plain, array and empty <f>, with and without cached values (n, str, e, b, s, inline), styles; formula text from every reference kind (A1, $A1, A$1, $A$1, lowercase, leading zeros, rows 0 and 9,999,999, columns past XFD, ranges, A:C, $1:2, Sheet2!, 'My Sheet'!, 'It''s'!, [1]Sheet1!), strings with reference-like text and "", names (TaxRate, ABCD1, LOG10(, _xlfn.XLOOKUP(, A1B2, R1C1), structured references, SUBTOTAL( prefixes with spaces, = and lowercase, and references at 9/99/999 and Z/ZZ so copies grow. Each workbook runs read twice (random formulas, richText, rawText, styles, maxRows, dates, small maxCellTextLength and maxWarnings; in 30% of the second runs maxCellTextLength is exactly the longest formula in the file) and importDataset once (random header, skipTotals, range, types, preview, errorCells, maxCellTextLength, sheet, maxRows), against the published 2.3.0 tarball. Results and errors must be deep-equal, except:

  • long master: this branch's result without its over-long-master warnings must deep-equal 2.3.0 on the same file with those children's <f> removed;
  • refused by the limit: this branch refuses with maxCellTextLength naming a cell, and 2.3.0 with the limit lifted holds a formula longer than the limit in that cell (2.3.0 either read the file or refused a later, longer plain text).
seed workbooks cases (read / importDataset) identical errors in both, identical long master refused by the limit diverged
62 9,000 27,000 (18,000 / 9,000) 26,208 5,477 711 81 0
2026 9,000 27,000 (18,000 / 9,000) 26,234 5,376 680 86 0
4747 9,000 27,000 (18,000 / 9,000) 26,256 5,474 661 83 0
9001 9,000 27,000 (18,000 / 9,000) 26,197 5,486 736 67 0
total 36,000 108,000 104,895 21,813 2,788 317 0

The first full run reported 75 divergences, all of one kind: both versions refused with maxCellTextLength, this branch at an earlier cell, a shifted copy over the limit, before 2.3.0 reached the longer plain text it refused. The classifier was widened to check that case against 2.3.0 with the limit lifted (above); the rerun has none.

Compatibility

No public API change: no new option, export or warning code. importDataset results are identical to 2.3.0 for every input (fuzz in the review section). read differs only for those masters and for cells whose shifted copy would be longer than maxCellTextLength (default 1,048,576, which no copy of a master within 8,192 characters can reach): those keep their cached values with a warning. The change stays in formula.ts, the shared-formula branch of sheet.ts, one internal read option in reader.ts and the read call in the import pipeline.

Changeset: minor. No new API, but a new refusal (copies count against maxCellTextLength) and a new case of a data-loss warning, which the repo has released as minor (maxXmlTokenLength in 2.1, maxCellTextLength in 2.3). Docs: §3.4.2, the maxCellTextLength row and the text table in §5.2, the values bullet in §5.2, the xlsx step in §5.6, the warning table in §7.3, the "Huge text" row of the §8 security table (shared formulas move out of the not-bounded list, with what remains) and the server guidance (formulas: false), §12 Q6 (the design, and why not lazy). README: the untrusted-files line.

Review fixes (second round)

  • M1, importDataset equal to 2.3.0 for long masters. The 8,192-character guard ran before the importDataset ("master") branch, so the children of a long master lost their formula there too. A SUBTOTAL totals row whose only formula was such a child was no longer skipped, and a child without a cached value no longer counted its row. In "master" mode the guard is now skipped. canShift still runs, so off-sheet children stay unresolved as in 2.3.0. Only the master's bounds are scanned, once (compileFormula(f, true): no segments, no copies). Tests use both reproducers, with 2.3.0's results as the expected values: (a) totalsRowSkipped: true, 2 rows, range A1:B4, no warning; (b) range A1:B3, 2 rows, no warning. The 1,048,576-character master with 1,000 children now imports with no warnings, as 2.3.0 would.
  • L2, the copy limit is soft. A shifted copy longer than maxCellTextLength is no longer refused. The cell keeps its cached value with SHARED_FORMULA_UNRESOLVED: …could not be resolved: shifted, it would have 13 characters, more than maxCellTextLength (12); only its cached value was read. The read goes on and no text longer than the limit is built. A master's own <f> text is still refused, as any cell text (test). The 10,000 × 90 file whose copies grow past 1,000 now reads: 233 copies are built, and the other 899,766 cells keep their cached values with the warning (kept plus dropped counted).
  • L1, the scanner uses character codes. Per-character regex tests are gone, and the three reference regexes are replaced by hand-written scanners that match exactly the same text. A new test compares them with the 2.3.0 shifter on 38 hand-picked edge tokens × 5 prefixes × 4 offsets, besides the 20,000 random formulas. Test file: 2,000 distinct reference-dense 8,192-character masters (about 1,600 references each), each with one child, through importDataset (machine shared with other jobs, two runs each):
importDataset
2.3.0 4.7 to 5.8 s
first round (880fa4f) 4.8 s
this round 1.3 to 1.6 s
read, formulas: false (floor) 0.22 s

What is left is about 0.35 µs per reference (3.2 million references), mostly the reference scanner.

  • N1, no machine-readable cause. ExcelWarning carries code, message, part, sheet, ref, and only code-specific partKind/dropped; there is no details field. A cause field would widen the public type, so the cause stays in the message (three wordings: off-sheet or missing master, master too long, copy over the limit).
  • Docs: §3.4.2, the §5.2 maxCellTextLength row and values bullet, §5.6, §7.3, §8 (soft copy limit; the reviewer's worst case: 273 KB, 100,000 children of an 8,192-character master from the bottom-right corner give 3.0 billion characters, 22 s and 3.4 GB; formulas: false avoids it; a total budget is a separate ticket), and Q6. The changeset is reworded to match.
  • Fuzz rerun against 2.3.0, two read and two importDataset cases per workbook. importDataset must now be deep-equal with no exception, long masters included. read may differ only by long-master and over-limit cells: each over-limit cell must hold a longer formula in 2.3.0 with the limit lifted, and the result without those warnings must equal 2.3.0 on the file without those cells' <f>.
seed workbooks read / import cases imports with long-master children identical errors in both, identical long master (read) soft limit (read) diverged
62 9,000 18,000 / 18,000 1,196 35,471 7,657 453 76 0
2026 9,000 18,000 / 18,000 1,170 35,514 7,692 424 62 0
4747 9,000 18,000 / 18,000 1,168 35,508 7,643 430 62 0
9001 9,000 18,000 / 18,000 1,090 35,543 7,678 397 60 0
total 36,000 72,000 / 72,000 4,624 142,036 30,670 1,704 260 0

- A shared formula's master is scanned once into the text between its
  references and the references; each cell that shares it costs its
  references plus its own length, built once as a flat string, and whether
  its references stay on the sheet is known in constant time. 2.3.0 rescanned
  the master per cell and appended character by character.
- A master longer than 8,192 characters (Excel's formula limit) is not
  expanded: the cells that share it keep their cached values with
  SHARED_FORMULA_UNRESOLVED naming the master cell and its length.
- Each shifted copy counts against maxCellTextLength and is refused before it
  is built (details.ref names the cell).
- importDataset reads with an internal option that gives the cells sharing a
  formula the master's text unshifted: whether a cell has a formula, and
  whether it is SUBTOTAL(...), is the same, so results are unchanged.
- Docs: 3.4.2, 5.2 limit and text tables, 5.6, warning table, 8 security
  table and server guidance, Q6. README limits line. Changeset: minor.
… limit, faster scan

- importDataset ("master" mode) skips the 8,192-character guard: children of
  a long master keep the master's text as in 2.3.0 (totals rows and rows
  without cached values are unchanged). Only the bounds of the master are
  scanned there, once, to keep off-sheet children unresolved.
- read: a shifted copy longer than maxCellTextLength is no longer refused;
  the cell keeps its cached value with SHARED_FORMULA_UNRESOLVED saying the
  copy's length, and the read goes on. A master's own <f> text is still
  refused by the limit.
- The scanner uses character codes and hand-written reference scanners
  instead of a regex per character and per word; a test compares it with the
  2.3.0 shifter on random formulas and on hand-picked edge tokens.
- Docs (3.4.2, 5.2, 5.6, 7.3, 8 with the measured worst case, Q6) and the
  changeset say so.
@securedeveloper
securedeveloper merged commit 19dbdba into main Dec 1, 2026
9 checks passed
@securedeveloper
securedeveloper deleted the fix/jse-62-shared-formulas branch December 1, 2026 22:35
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