GHSA-5478-66c3-rhxr: Pretext: Algorithmic Complexity (DoS) in the text analysis phase
`isRepeatedSingleCharRun()` in `src/analysis.ts` (line 285) re-scans the entire accumulated segment on every merge iteration during text analysis, producing O(n²) total work for input consisting of repeated identical punctuation characters. An attacker who controls text passed to `prepare()` can block the main thread for ~20 seconds with 80KB of input (e.g., `"(".repeat(80_000)`).Tested against commit 9364741d3562fcc65aacc50953e867a5cb9fdb23 (v0.0.4) on Node.js v24.12.0, Windows x64.A standalone PoC and detailed write-up are attached below.Root CauseThe `buildMergedSegmentation()` function (line 795) processes text segments produced by `Intl.Segmenter`. When consecutive non-word-like segments consist of the same single character (e.g., `(`, `[`, `!`, `#`), the code merges them into one growing segment (line 859):Before each merge, it calls `isRepeatedSingleCharRun()` (line 857) to verify that ALL characters in the accumulated segment match the new character:`Intl.Segmenter` with `granularity: 'word'` produces individual non-word segments for each punctuation character. For a string of N identical punctuation characters, the merge check is called N times. On the k-th call, the accumulated segment is k characters long, so `isRepeatedSingleCharRun` performs k comparisons.Total work: `1 + 2 + 3 + ... + N = N(N+1)/2 = O(n^2)`Call chainProof of ConceptThe simplest payload is a string of repeated `(` characters:Any single character that meets these criteria works:Classified as `'text'` by `classifySegmentBreakChar` (analysis.ts:321) - i.e., not a space, NBSP, ZWSP, soft-hyphen, tab, or newlineProduced as individual non-word segments by `Intl.Segmenter` (word granularity)Not `-` or em-dash (explicitly excluded at lines 855-856)Working payload characters include: `(`, `[`, `{`, `#`, `@`, `!`, `%`, `^`, `~`, `<`, `>`, etc.Impact**Chat/messaging applications:** User sends an 80KB message of `(` characters;
the receiving client's UI thread freezes for ~20 seconds while rendering.**Comment/form systems:** User-supplied text in any text field that uses
`pretext` for layout measurement blocks the main thread.**Server-side rendering:** If `prepare()` is called server-side (Node.js/Bun),
a single request can consume 20+ seconds of CPU time per 80KB of payload.The attack requires no authentication, special characters, or encoding tricks -
just repeated ASCII punctuation. 80KB is well within typical text input limits.As an application-level mitigation, callers can cap the length of text passed to
`prepare()` before a library-level fix is available.Suggested FixReplace the O(n) full-scan verification with O(1) constant-time checks. 
Since the merge only ever appends the same character to an existing repeated-char run, the invariant is maintained structurally:**Option A - Check only endpoints (O(1)):**This works for the current code because this branch only fires after earlier merge branches (CJK, Myanmar, Arabic) have been skipped, and those branches produce segments that would not start and end with the same ASCII punctuation character. However, the safety relies on an emergent property of the branch ordering and the other merge branches. Future refactors that add new merge branches or reorder the existing ones could silently break the invariant.**Option B - Track with metadata**
Add a boolean `lastMergeWasSingleCharRun` alongside the accumulator arrays. Set it to `true` when a single-char merge succeeds, `false` when any other merge branch is taken. Check the flag instead of re-scanning the string.
CVSS v4.0: 8.7