Skip to content

JSE-68: maxColumns read limit, dataset empty fields counted against maxCells - #136

Merged
securedeveloper merged 4 commits into
mainfrom
fix/jse-68-max-columns
Dec 1, 2026
Merged

securedeveloper merged 4 commits into
mainfrom
fix/jse-68-max-columns

Conversation

@securedeveloper

@securedeveloper securedeveloper commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Gap

A 614 KB .xlsx whose row 1 has a title in every column up to XFD (16,384 columns), followed by 10,000 rows of 3 cells, made importDataset run out of a 4 GB heap after about 8 s (measured by a consumer's security review on 2.3.0). Every data row became an object with 16,384 keys (about 164 million fields). maxCells counts stored cells only (about 46,000 here), so it never tripped; 1-character titles behave the same (width, not text). The library gave callers no width limit.

Why every row has 16,384 keys: toDataset's contract is that each data row carries a key for every column, null where the row has no cell. The contract is kept; its cost is now bounded.

Measurements (Node 26, Apple-silicon laptop, --max-old-space-size=4096, files built with the library's writer)

File / call 2.3.0 This branch
16,384 titles + 10,000 rows of 3 cells (328 KB), importDataset defaults out of memory (4.5 GB RSS) refused in 0.59 s, 134 MB RSS: LimitError, details: { limit: 'maxCells', value: 10008791, max: 10000000, rows: 611, columns: 16384 }
same, limits: { maxColumns: 256 } (option ignored) out of memory refused in 0.17 s, 117 MB RSS: details: { limit: 'maxColumns', value: 257, max: 256, part: 'xl/worksheets/sheet1.xml', ref: 'IW1' }
16,384 titles + 60 rows (123 KB), importDataset defaults 60 rows × 16,384 columns, 2.5 s, 271 MB RSS identical result, 2.5 s, 263 MB RSS (983,040 empty fields, under 10 M)
same, limits: { maxColumns: 256 } — refused in 0.11 s, 100 MB RSS
same, read defaults 0.32 s 0.19 s, identical

API and defaults

ReadLimits.maxColumns (optional member, in DEFAULT_READ_LIMITS, default 16,384). The highest column used by any cell the read keeps, on each sheet read: a cell with a value, a styled empty cell (with styles on), a hyperlink's cell. Checked as each cell is kept while the sheet streams in, so a wider sheet stops at its first cell past the limit and later rows are not read; <dimension> is not trusted, and an empty unstyled <c> (which the reader does not keep) does not count. LimitError LIMIT_EXCEEDED, details: { limit: 'maxColumns', value, max, part, ref }, value = the offending cell's 1-based column number. Passed through read, importDataset (limits.maxColumns) and the worker client; readSheetNames accepts and validates it (it reads no cells).

  • Default 16,384 = Excel's (and LibreOffice's) own width, so no valid sheet is refused and the default changes nothing: references beyond XFD were already skipped with CELL_OUT_OF_BOUNDS and still are. Docs and the server guidance say to set it low (for example 256) for untrusted files.

CSV maxColumns (new CsvParseOptions member, beside maxFieldBytes/maxCellTextLength; no fields-per-record limit existed). Fields in one record, refused while the record is read with the same error (details: { limit: 'maxColumns', value: max + 1, max, ref }); a trailing delimiter is a field; the record read only to find maxRows reached is not checked. Default 1,048,576: 64 times Excel's width and wider than any spreadsheet, so no CSV that 2.3 parsed is refused; a record that wide is at least 1 MiB of text, so parseCsv output stays linear in its input. importDataset passes limits.maxColumns to CSV only when given (otherwise the CSV default applies, not the xlsx one). Note: importDataset already rejected CSV wider than 16,384 columns in 2.3 (ValidationError from the dataset step); unchanged.

Dataset size in importDataset. The empty fields the dataset pads rows with (a column a data row has no cell in) are counted against limits.maxCells (default 10,000,000), for xlsx and CSV, row by row before any row is built; past it: LimitError, details: { limit: 'maxCells', value, max, rows, columns }. Cells the rows do have are not counted (for xlsx the reader already counted them), so dense data is never refused by it, and a dataset holds at most the sheet's cells plus maxCells fields. A pre-check skips the count when rows × columns cannot pass the limit, so ordinary imports do no extra work. Reusing maxCells instead of a new option: it is the budget callers already set for "values the read builds", and the padding is exactly that. toDataset (public) is unchanged and has no budget. For CSV this is the first use of maxCells; a CSV import that pads more than 10 M empty fields (ragged or wide-and-short) is now refused under the defaults.

Additive: new optional members and options; results for inputs within the limits are identical to 2.3.0 (fuzz below). An invalid limits.maxCells for a CSV import is now a TypeError (it was ignored); for xlsx the error and its order are as before.

Tests (written first; red on 2.3: the worker OOM-aborted on the measured files)

  • test/read/max-columns.test.ts: default 16,384; XFD reads and refs beyond XFD still warn; the measured file built in the test (<700 KB): refused with maxColumns: 256 at IW1 (row 1, so no data row read), and under defaults refused by the dataset count at exactly row 611 with value ≤ max + one row (work asserted, not wall clock alone); 1-character titles; exactly-at-limit reads, one more refused; one sparse cell in XFD counted; cells without r; empty unstyled vs styled cells; hyperlink cells; per-sheet and only sheets read; rows past maxRows; lying <dimension>; validation and readSheetNames.
  • test/import/dataset-size.test.ts: the count stops after reading only 11 rows of 100,000 (proxy-counted row reads) and builds none; at-the-budget output equals the unbounded output; dense rows, empty rows, null cells, range; CSV wide header refused under defaults; limits.maxCells budget; preview.
  • test/csv/max-columns.test.ts: default, at/over the limit, trailing delimiter, quoted fields, readCsv, maxRows, validation, importDataset pass-through.
  • test/worker/worker.test.ts: maxColumns (read, parseCsv, importDataset xlsx and CSV) and the dataset maxCells cross the boundary.
  • scripts/consumer.mjs: a complete 2.3-style ReadLimits literal (without maxColumns) still compiles; DEFAULT_READ_LIMITS.maxColumns; CSV maxColumns. npm run consumer-ts: TypeScript 5.0–7.0 pass.
  • One existing fake context in test/read/sheet-list-linear.test.ts gained limits.maxColumns.

Differential fuzz against the released 2.3.0

Harness written for this ticket (kept outside the repo): the released tarball from npm pack [email protected] against this branch's build. Random cases: hand-built xlsx packages (stored zip; cells with and without r, refs beyond XFD, every cell type, styled and unstyled empty cells, shared formulas with SUBTOTAL, hyperlinks far to the right, lying <dimension>, merges, hidden first sheet, 1–3 sheets), workbooks written by 2.3.0 (header widths 3–16,384, sparse rows, dates, nulls), and CSV (4 delimiters, 3 newline styles, quotes, stray quotes, blank lines, ragged and up to 3,000-field records, BOM, trailing delimiter). Options randomised: limits.maxColumns, limits.maxCells, styles, maxRows, sheets, range, preview, sheet, header, CSV delimiter/inferTypes/skipEmptyLines/mode/maxRows/maxColumns. Each case runs read, readSheetNames, importDataset (xlsx or CSV) and parseCsv; results and errors (name, code, message, details) must be deep-equal, except a refusal by the new limits (maxColumns, or the dataset's maxCells count, recognised by details.rows) where 2.3.0 did not refuse.

Run Cases Comparisons Identical (of which both errored) New-limit refusals Mismatches
seeds 11, 22, 33, 44, 55, 66, 77, 88 × 13,000 104,000 270,197 249,250 (12,556) 20,947 (read 6,462; importDataset xlsx 7,212; parseCsv 3,143; importDataset CSV 4,130; readSheetNames 0) 0
seeds 101, 202 × 6,000, on the final head build 12,000 31,096 28,702 (1,453) 2,394 0

The 104,000-case run used a build from before the last change (the limits.maxCells check in importDataset moved after read, which affects only invalid option values, not generated here); the second run is on the head.

Docs

docs/api.md §5.2 (ReadLimits block, new maxColumns row, maxCells row, details.value note), §5.3 (CSV option), §5.6 (limits comment, "Dataset size" behaviour), §8 ("Wide sheets" row, server guidance maxColumns: 256); README limits line; changeset (minor).

Review fixes

  • M1: the dataset count treated any cell object as present, so cells holding only a hyperlink (charged to maxItems, not maxCells) built full-width rows for free. A cell with neither value nor formula (hyperlink-only or style-only) now counts as an empty field. Tests: 16,384 titles over 100 rows of hyperlinks with maxCells: 100000 (with and without a schema) refused at row 7 (value 114,681); a consumer's settings (maxColumns 256, maxCells 1,000,000), 256 titles over 20,000 rows of 1 cell and 255 hyperlinks, refused at row 3,922 (value 1,000,110); hyperlink cells that hold values are unchanged.
  • Low 1: a shared-string cell whose index does not exist now counts toward maxColumns on both paths (with ReadOptions.maxRows it was counted when stored; without, it was dropped first). Test pins both.
  • Low 2-5: changeset and §5.6/§8 now say: the count is checked while the dataset is built (so it can win over later 2.3 errors such as a types key naming no column, or maxRows); the ragged-CSV example (more than 500,000 rows of 10 fields under 30 columns are refused under the defaults); CSV imports validate limits.maxCells; spreading DEFAULT_READ_LIMITS into a CSV import gives maxColumns: 16384, a LimitError instead of 2.3's ValidationError; the public toDataset is not bounded by the count (set maxColumns on the read); the measured worst case under defaults (about 610 rows × 16,384 columns: 3.1 s, 1.13 GB).
  • Merged origin/main (JSE-69: 'day' takes the day as written for ISO text with an offset #137 JSE-69, JSE-62: bounded shared formulas (scan once, Excel's limit for masters, copies under maxCellTextLength), importDataset without shifting #138 JSE-62); the read call in pipeline.ts keeps JSE-62's internal sharedFormulaText option, and limits (incl. maxColumns) pass through as before; docs §8 and the README keep both sets of rows.
  • Fuzz on the merged build, generator extended with hyperlink-only cells across rows: seeds 301–306 × 11,000 = 66,000 cases, 171,789 comparisons, 157,424 identical (8,459 both errors), 14,365 new-limit refusals, 0 mismatches.

…axCells

- New optional ReadLimits.maxColumns (default 16,384, Excel's width): the
  highest column a kept cell uses on each sheet read, checked as each cell is
  kept (a styled empty cell and a hyperlink's cell count; <dimension> is not
  trusted). LimitError LIMIT_EXCEEDED, details { limit: 'maxColumns', value
  (the column number), max, part, ref }. Passed through read, importDataset
  and the worker client; readSheetNames accepts and validates it.
- CSV: new option maxColumns, fields in one record (default 1,048,576), with
  the same error; importDataset passes limits.maxColumns to CSV when given.
- importDataset counts the empty fields its dataset pads short rows with
  (null for a column a data row has no cell in) against limits.maxCells, for
  xlsx and CSV, before any row is built; details { limit: 'maxCells', value,
  max, rows, columns }. A title in every column to XFD over 10,000 three-cell
  rows is refused in about 0.6 s instead of exhausting a 4 GB heap.
- Docs: limits table, details.value, CSV options, import behaviour, security
  table and server guidance; README limits line.
…invalid shared-string cells count toward maxColumns on both paths
Keeps JSE-62's shared-formula rows in docs §8 and the README beside the
JSE-68 width rows, and documents the review's notes on the dataset count.
@securedeveloper
securedeveloper merged commit e526c0e into main Dec 1, 2026
9 checks passed
@securedeveloper
securedeveloper deleted the fix/jse-68-max-columns branch December 1, 2026 23:17
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