Skip to content

fix(xlsx): prevent malformed coordinates from hanging the grid - #25

Open
mkroemer wants to merge 1 commit into
p10node:mainfrom
mkroemer:fix/xlsx-coordinate-bounds
Open

mkroemer wants to merge 1 commit into
p10node:mainfrom
mkroemer:fix/xlsx-coordinate-bounds

Conversation

@mkroemer

Copy link
Copy Markdown
Contributor

Malformed drawing anchors or worksheet rows could expand the grid to billions of rows, where signed 32-bit midpoint arithmetic could make axis lookup loop indefinitely.

Validate OOXML coordinates and geometry, discard invalid records, and bound drawing-derived extents to worksheet limits. Axis searches now use arithmetic midpoints without imposing Excel's row limit on intentional large CSV imports.

Follows up on the non-blocking feedback in #11. Invalid OOXML rows/cells/columns/merges/anchors are skipped, and sizes, offsets, defaults and frozen-pane metadata are validated.

Validation with Bun 1.4.4: frozen dependencies, typecheck, all 552 unit tests and production build passed. Regressions cover corrupt records in a time-bounded process, valid worksheet boundaries, bounded large-axis searches and preservation of large loader counts. An independent review passed additional Axis-oracle, malformed-record, extreme-metadata and bounded-browser checks.

Limits: no physical 500-million-row CSV import was rerun. Frozen-pane rendering remains unvirtualized as on main; this PR does not claim a general rendering-resource cap.

@pierreneter pierreneter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this. The bounds reject more than they need to, and files that opened fine now lose data without a word, so I can't merge it as it is. Line numbers are at 3751c55.

Reproduced against this branch and its base:

  1. src/core/xlsx/workbook.ts:168: a row height above 409.5pt is dropped, not clamped. <row r="1" ht="488" customHeight="1"> gives no height at all (base: 651px). Our own converters write such heights (src/core/iwork/numbers.ts:312, src/core/legacy/xls/sheet.ts:458).
  2. workbook.ts:196: a <col> with width > 255 or max > 16384 is dropped whole, together with its hidden flag. <col min="2" max="2" width="300" hidden="1"/> becomes a visible column of default width. Base accepted the width and clamped max.
  3. workbook.ts:62: worksheetRef accepts uppercase only. <c r="a1"> and <mergeCell ref="a1:b1"/> load as nothing, so the workbook opens as an empty sheet with no error. colIndex folds case, and base read both.
  4. workbook.ts:133: a cell whose ref names another row than the row counter is dropped. <row><c r="A1"/></row><row><c r="A3"><v>3</v></c></row> shows only A1. Base kept the cell.
  5. workbook.ts:163: rows past 1,048,576 are now dropped at load. That bypasses the notice we already have for this ("over Excel's 1,048,576-row limit: save as CSV", XlsxView.tsx:236) and the save guard (editor.ts:910): the sheet shows 1,048,576 rows with no warning, and the text export is cut. The cutoff is right for a sparse multi-billion coordinate; dense over-limit data should keep the existing path.
  6. Same place: the loader skips an invalid row or cell without advancing its implicit counters, but RowPatcher still counts them on save (editor.ts:1297, :1345). <row r="5">, <row r="1048577">, then a <row> with a value: edit A6, save, reopen. The edit is in row 6 and the overwritten value is back in row 7.
  7. src/core/xlsx/limits.ts:13: Number() accepts hex, exponent and padded forms. r="0x10" loads as row 16, while RowPatcher's \d+ does not match it, which is the same loader / saver disagreement. /^\d+$/ for integer attributes closes it.

From reading:

  1. The hang in the title is still reachable. <pane state="frozen" ySplit="1048575"/> passes (workbook.ts:203), and the frozen region is not virtualised: XlsxView.tsx:1702 and :1708 render region('top', 0, fr - 1, ...) and the row headers for about a million rows, and the effect at :599 calls store.ensure(sheet, 0, frozen.rows - 1) on every render. Clamping the frozen region to the viewport in the view is the fix; d.frozen is also set from sheet ops without a check.
  2. src/core/xlsx/drawings.ts:62: an anchor with a missing or non-integer colOff / rowOff is now dropped. Base took the offset as 0 and still showed the picture. I'd reject on col / row only.

In short: clamp sizes rather than drop them, keep a cell wherever a sane reading exists, and leave dense over-limit rows to the existing notice.

Non-blocking:

  • Parsing 250k rows x 12 cells is about 10-25% slower here: a regex test, a replace and a tuple per cell in the SAX path. One char-code scan can parse and bound-check column and row together.
  • test/xlsx-bounds.test.ts:134 asserts stderr === '' and kills the child after 5 s. Any warning on stderr or a cold transpile on a loaded runner fails it. The exit code plus a stdout marker is sturdier.
  • MAX_ROWS / MAX_COLS stay duplicated in src/core/legacy/odf/ods.ts and src/core/legacy/xls/sheet.ts; the converters and the new validator can drift apart, which is how 1 comes about.

This branch has not been deployed

No deployments
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.

2 participants