Skip to content

Commit de5fe80

Browse files
committed
fix(table-cells): parse currency input in linear time
Three regexes in parseCurrencyInput backtracked quadratically on crafted cells, and table imports and coercion run it per cell on the server. - CURRENCY_MARKER_PREFIX: whitespace runs on both sides of an optional sign split ambiguously; the sign and its trailing whitespace are now one optional group. - CURRENCY_MARKER_SUFFIX: unanchored with a leading whitespace run, so replace rescanned the run from every start; it now matches the marker against the right-trimmed string. - AMOUNT_SHAPE: overlapping [\d.,]*\d[\d.,]*; the run before the first digit now admits only separators.
1 parent 507953c commit de5fe80

4 files changed

Lines changed: 46 additions & 14 deletions

File tree

‎bun.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/table-cells/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"zod": "4.3.6"
5454
},
5555
"devDependencies": {
56+
"@sim/testing": "workspace:*",
5657
"@sim/tsconfig": "workspace:*",
5758
"typescript": "^7.0.2",
5859
"vitest": "^5.1.0"

‎packages/table-cells/src/currency.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* filters and sorts cast a currency cell to `numeric`, so anything it lets
44
* through unparsed would break every query against the column.
55
*/
6+
import { expectLinearGrowth } from '@sim/testing/helpers/scaling'
67
import { describe, expect, it } from 'vitest'
78
import { parseCurrencyInput } from './currency'
89

@@ -246,4 +247,20 @@ describe('parseCurrencyInput', () => {
246247
expect(parseCurrencyInput('1,234,567.89')).toBe(1234567.89)
247248
expect(parseCurrencyInput('12,34,567')).toBe(1234567)
248249
})
250+
251+
it.each([
252+
['whitespace where a leading marker would start', (n: number) => `(${' '.repeat(n)}#)`],
253+
['whitespace where a trailing marker would start', (n: number) => `a${'\t'.repeat(n)}!`],
254+
['digits before a stray character', (n: number) => `${'0'.repeat(n)}#`],
255+
['digits and separators before a stray character', (n: number) => `${'0.'.repeat(n)}!`],
256+
])('rejects a long run of %s in linear time', (_shape, make) => {
257+
// Table imports and coercion parse every cell on the server, so a parser
258+
// that backtracks quadratically lets one crafted cell stall the event loop.
259+
expectLinearGrowth((n) => {
260+
const input = make(n)
261+
return () => {
262+
expect(parseCurrencyInput(input)).toBeNull()
263+
}
264+
}, 2000)
265+
})
249266
})

‎packages/table-cells/src/currency.ts‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,20 @@ function isCurrencyMarkerLetters(letters: string): boolean {
8282
* A leading currency marker: up to three letters and/or a symbol, optionally
8383
* behind a sign. The sign is captured so `-$12.50` keeps it, and the letters
8484
* are captured so {@link isCurrencyMarkerLetters} can vet them.
85+
*
86+
* The sign and the whitespace after it form one optional group, so a
87+
* whitespace run splits only one way and a failed match backtracks in linear
88+
* time. `\s` already covers U+00A0 and U+202F.
8589
*/
86-
const CURRENCY_MARKER_PREFIX =
87-
/^[\s\u00a0\u202f]*([+-]?)[\s\u00a0\u202f]*(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)[\s\u00a0\u202f]*/u
90+
const CURRENCY_MARKER_PREFIX = /^\s*(?:([+-])\s*)?(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)\s*/u
8891

89-
/** The same, trailing. */
90-
const CURRENCY_MARKER_SUFFIX =
91-
/[\s\u00a0\u202f]*(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)\.?[\s\u00a0\u202f]*$/u
92+
/**
93+
* The same, trailing, matched against a right-trimmed string. It has no
94+
* leading whitespace run, so each start position costs at most the marker's
95+
* length; the whitespace before the marker survives the strip and is removed
96+
* with every other space afterwards.
97+
*/
98+
const CURRENCY_MARKER_SUFFIX = /(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)\.?$/u
9299

93100
/**
94101
* Whether a matched marker may be stripped. A marker carrying a currency
@@ -100,8 +107,12 @@ function isStrippableMarker(letters: string | undefined, symbol: string | undefi
100107
return isCurrencyMarkerLetters(letters)
101108
}
102109

103-
/** Only digits and separators, with at least one digit. */
104-
const AMOUNT_SHAPE = /^[+-]?[\d.,]*\d[\d.,]*$/
110+
/**
111+
* Only digits and separators, with at least one digit. The run before the
112+
* first digit admits only separators, so no character can be claimed by both
113+
* runs and a failed match backtracks in linear time.
114+
*/
115+
const AMOUNT_SHAPE = /^[+-]?[.,]*\d[\d.,]*$/
105116

106117
/**
107118
* Whether `text` is validly grouped by `separator`: a first group of 1-3 digits
@@ -251,14 +262,16 @@ export function parseCurrencyInput(raw: unknown, currencyCode?: string): number
251262
const withoutPrefix = body.replace(CURRENCY_MARKER_PREFIX, (match, sign, letters, symbol) => {
252263
if (!isStrippableMarker(letters, symbol)) return match
253264
hadMarker = true
254-
return sign
255-
})
256-
const withoutMarkers = withoutPrefix.replace(CURRENCY_MARKER_SUFFIX, (match, letters, symbol) => {
257-
if (!isStrippableMarker(letters, symbol)) return match
258-
hadMarker = true
259-
return ''
265+
return sign ?? ''
260266
})
261-
const cleaned = withoutMarkers.replace(/[\s\u00a0\u202f\u2019']/gu, '')
267+
const withoutMarkers = withoutPrefix
268+
.trimEnd()
269+
.replace(CURRENCY_MARKER_SUFFIX, (match, letters, symbol) => {
270+
if (!isStrippableMarker(letters, symbol)) return match
271+
hadMarker = true
272+
return ''
273+
})
274+
const cleaned = withoutMarkers.replace(/[\s\u2019']/gu, '')
262275
// What remains must be ONLY digits and separators. Anything else — a
263276
// US-format date, leftover prose — is not an amount.
264277
if (!AMOUNT_SHAPE.test(cleaned)) return null

0 commit comments

Comments
 (0)