fix(table-cells): parse currency input in linear time

Merged
WA
11d ago
+46-14

Summary

  • parseCurrencyInput backtracked quadratically on crafted cells (CodeQL js/polynomial-redos); table imports and coercion run it per cell on the server, so one long cell could stall the event loop for seconds
  • CURRENCY_MARKER_PREFIX: whitespace runs on both sides of an optional sign split ambiguously → the sign and its trailing whitespace are one optional group (^\s*(?:([+-])\s*)?…), callback returns sign ?? ''
  • CURRENCY_MARKER_SUFFIX: unanchored with a leading whitespace run, so replace rescanned the run from every start → drop the whitespace runs and match against trimEnd(); the whitespace left before the marker is stripped with every other space right after
  • AMOUNT_SHAPE: overlapping [\d.,]*\d[\d.,]* → [.,]*\d[\d.,]* (same language, disjoint runs)
  • [\s  ] → \s (\s and trimEnd already cover U+00A0/U+202F); no length cap, no behaviour change
  • Other regexes in packages/table-cells/src checked and measured: all linear (ttl.ts's /0+$/ runs on at most 6 chars)

Type of Change

  • Bug fix

Testing

  • Equivalence (one-off script, old vs new): 0 differences over 550k inputs (test corpus, Intl output across 40 locales × 21 currencies × 4 displays × 2 signs, random and structured combos of digits, .,'’, whitespace incl. U+00A0/U+202F, signs incl. U+2212, parens, symbols, ISO codes, kr/zł/R$, letters, e/E) × 8 currency codes on both Bun and Node, plus every string up to length 6 over a 14-char alphabet × 3 codes (24.3M comparisons)
  • Linearity: new expectLinearGrowth cases in currency.test.ts fail on the old code (growth ratios 55–91 vs the 20 limit) and pass on the new; reverting each regex alone turns its own case red. At 40k chars the old code took 1–7s per cell, the new under 1ms
  • table-cells, workspace-code kinds, and apps/sim table import/coercion/column-type tests; lint, type-check, check:audits, check:test-types, block-registry check, root bun run test (one unrelated workflow-code scaling flake at 20.5, green on rerun)

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)