diff --git a/bun.lock b/bun.lock index 52dbfeedb0a..b4421176458 100644 --- a/bun.lock +++ b/bun.lock @@ -810,6 +810,7 @@ "zod": "4.3.6", }, "devDependencies": { + "@sim/testing": "workspace:*", "@sim/tsconfig": "workspace:*", "typescript": "^7.0.2", "vitest": "^5.1.0", diff --git a/packages/table-cells/package.json b/packages/table-cells/package.json index 8dafd9b5dbb..b394ff50014 100644 --- a/packages/table-cells/package.json +++ b/packages/table-cells/package.json @@ -53,6 +53,7 @@ "zod": "4.3.6" }, "devDependencies": { + "@sim/testing": "workspace:*", "@sim/tsconfig": "workspace:*", "typescript": "^7.0.2", "vitest": "^5.1.0" diff --git a/packages/table-cells/src/currency.test.ts b/packages/table-cells/src/currency.test.ts index b07816d9d16..be72a3ee026 100644 --- a/packages/table-cells/src/currency.test.ts +++ b/packages/table-cells/src/currency.test.ts @@ -3,6 +3,7 @@ * filters and sorts cast a currency cell to `numeric`, so anything it lets * through unparsed would break every query against the column. */ +import { expectLinearGrowth } from '@sim/testing/helpers/scaling' import { describe, expect, it } from 'vitest' import { parseCurrencyInput } from './currency' @@ -246,4 +247,20 @@ describe('parseCurrencyInput', () => { expect(parseCurrencyInput('1,234,567.89')).toBe(1234567.89) expect(parseCurrencyInput('12,34,567')).toBe(1234567) }) + + it.each([ + ['whitespace where a leading marker would start', (n: number) => `(${' '.repeat(n)}#)`], + ['whitespace where a trailing marker would start', (n: number) => `a${'\t'.repeat(n)}!`], + ['digits before a stray character', (n: number) => `${'0'.repeat(n)}#`], + ['digits and separators before a stray character', (n: number) => `${'0.'.repeat(n)}!`], + ])('rejects a long run of %s in linear time', (_shape, make) => { + // Table imports and coercion parse every cell on the server, so a parser + // that backtracks quadratically lets one crafted cell stall the event loop. + expectLinearGrowth((n) => { + const input = make(n) + return () => { + expect(parseCurrencyInput(input)).toBeNull() + } + }, 2000) + }) }) diff --git a/packages/table-cells/src/currency.ts b/packages/table-cells/src/currency.ts index 395b62de603..37f2ac9329c 100644 --- a/packages/table-cells/src/currency.ts +++ b/packages/table-cells/src/currency.ts @@ -82,13 +82,20 @@ function isCurrencyMarkerLetters(letters: string): boolean { * A leading currency marker: up to three letters and/or a symbol, optionally * behind a sign. The sign is captured so `-$12.50` keeps it, and the letters * are captured so {@link isCurrencyMarkerLetters} can vet them. + * + * The sign and the whitespace after it form one optional group, so a + * whitespace run splits only one way and a failed match backtracks in linear + * time. `\s` already covers U+00A0 and U+202F. */ -const CURRENCY_MARKER_PREFIX = - /^[\s\u00a0\u202f]*([+-]?)[\s\u00a0\u202f]*(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)[\s\u00a0\u202f]*/u +const CURRENCY_MARKER_PREFIX = /^\s*(?:([+-])\s*)?(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)\s*/u -/** The same, trailing. */ -const CURRENCY_MARKER_SUFFIX = - /[\s\u00a0\u202f]*(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)\.?[\s\u00a0\u202f]*$/u +/** + * The same, trailing, matched against a right-trimmed string. It has no + * leading whitespace run, so each start position costs at most the marker's + * length; the whitespace before the marker survives the strip and is removed + * with every other space afterwards. + */ +const CURRENCY_MARKER_SUFFIX = /(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)\.?$/u /** * Whether a matched marker may be stripped. A marker carrying a currency @@ -100,8 +107,12 @@ function isStrippableMarker(letters: string | undefined, symbol: string | undefi return isCurrencyMarkerLetters(letters) } -/** Only digits and separators, with at least one digit. */ -const AMOUNT_SHAPE = /^[+-]?[\d.,]*\d[\d.,]*$/ +/** + * Only digits and separators, with at least one digit. The run before the + * first digit admits only separators, so no character can be claimed by both + * runs and a failed match backtracks in linear time. + */ +const AMOUNT_SHAPE = /^[+-]?[.,]*\d[\d.,]*$/ /** * 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 const withoutPrefix = body.replace(CURRENCY_MARKER_PREFIX, (match, sign, letters, symbol) => { if (!isStrippableMarker(letters, symbol)) return match hadMarker = true - return sign - }) - const withoutMarkers = withoutPrefix.replace(CURRENCY_MARKER_SUFFIX, (match, letters, symbol) => { - if (!isStrippableMarker(letters, symbol)) return match - hadMarker = true - return '' + return sign ?? '' }) - const cleaned = withoutMarkers.replace(/[\s\u00a0\u202f\u2019']/gu, '') + const withoutMarkers = withoutPrefix + .trimEnd() + .replace(CURRENCY_MARKER_SUFFIX, (match, letters, symbol) => { + if (!isStrippableMarker(letters, symbol)) return match + hadMarker = true + return '' + }) + const cleaned = withoutMarkers.replace(/[\s\u2019']/gu, '') // What remains must be ONLY digits and separators. Anything else — a // US-format date, leftover prose — is not an amount. if (!AMOUNT_SHAPE.test(cleaned)) return null