Repository navigation
fix(security): replace superlinear quote-strip regex in extractBodySummary (CD-01) - #4063
Merged
Merged
Conversation
…mmary (CD-01) The stripQuotes path of extractBodySummary used the legacy regex /(^(\n|\r|\s)*)>([\s\S]*?).*\s*/g whose alternation branches overlap (\s already matches \n and \r), so V8 backtracked superlinearly on leading whitespace runs that never reach `>`: 26 leading newlines took ~0.9 s, growing ~3x per extra newline, enabling server-side CPU exhaustion via reply bodies flowing into generateMetadata. Replace it with stripLeadingQuoteLine, a single linear-time scan that produces identical output (verified by characterization tests and a 60k-case differential fuzz against the old regex). Fixes finding CD-01 of the 2026-09-29 steem-audit condenser report.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Audit finding CD-01 (HIGH) from the 2026-09-29 steem-audit condenser report: the
stripQuotespath ofextractBodySummary(lib/extract-content.ts) used the legacy regexwhose leading group alternates
\n,\rand\s— but\salready contains\nand\r, so the branches overlap. When a leading whitespace run is followed by no>, V8 backtracks superlinearly through the redundant parses. Attacker-controlled reply/post bodies reach this code viabuildPostMetadata(lib/seo.ts) in the post pages'generateMetadata, causing server-side CPU exhaustion (ReDoS).Root cause
A run of
nnewlines admits2^nparses of(\n|\r|\s)*(each\n/\rmatches two branches), so a failed>match unwinds exponentially. Measured on this machine (Node/V8, standalone regex): 20/22/24/26/28/30 leading newlines take 24 / 94 / 310 / 898 / 3829 / 10702 ms — ~3x growth per 2 newlines; a 10k-char mixed-whitespace prefix was killed after 25 s without finishing. (A pure-space run happens to backtrack linearly since a space matches only one branch;\nand\rare the pathological characters.)Fix
Replace the regex with
stripLeadingQuoteLine, a single left-to-right scan that is behavior-identical and O(n):\sclass).>, return the input unchanged (the old regex was^-anchored withoutm, so it could only ever match once, at index 0 — a quote anywhere else was never stripped)..stopped at line terminators\n,\r,\u2028,\u2029only) and then skip all trailing whitespace — exactly one quote line is removed, matching the old single-replacement behavior.Behavior-parity strategy
stripQuotestrue/false, URL stripping, 140-char truncation). The same tests pass unchanged against the new implementation.stripQuotesin {true, false}) over 60,000 random strings built from an adversarial alphabet (\n \r \t > a . * # \u00a0 \u2028 \u2029 \f \u3000 - _ \你`) found 0 mismatches.Performance evidence (new implementation, full
extractBodySummaryincl. markdown parse, median of 5)\n \t\rx2500)>quoteTest plan
__tests__/lib/extract-content.test.ts— 13 characterization tests + 5 CD-01 regression tests asserting 10k-char adversarial prefixes (newlines / spaces / mixed), a 60-newline non-matching prefix, and a 10k-newline matching quote each complete in well under 100 ms with correct output.pnpm test: 86 files / 1081 tests pass.pnpm lint: no new warnings or errors (unchanged files report the same 7 pre-existing warnings; the 1 pre-existing error in__tests__/proxy.test.tsis from the baseline commit and unrelated to this change).pnpm typecheck: clean.Fixes audit finding CD-01. Does not merge automatically — review welcome.