Skip to content

fix(security): replace superlinear quote-strip regex in extractBodySummary (CD-01) - #4063

Merged
ety001 merged 1 commit into
nextfrom
fix/cd-01-linear-quote-strip
Oct 19, 2026
Merged

ety001 merged 1 commit into
nextfrom
fix/cd-01-linear-quote-strip

Conversation

@ety001

@ety001 ety001 commented Oct 19, 2026

Copy link
Copy Markdown
Member

Problem

Audit finding CD-01 (HIGH) from the 2026-09-29 steem-audit condenser report: the stripQuotes path of extractBodySummary (lib/extract-content.ts) used the legacy regex

/(^(\n|\r|\s)*)>([\s\S]*?).*\s*/g

whose leading group alternates \n, \r and \s — but \s already contains \n and \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 via buildPostMetadata (lib/seo.ts) in the post pages' generateMetadata, causing server-side CPU exhaustion (ReDoS).

Root cause

A run of n newlines admits 2^n parses of (\n|\r|\s)* (each \n/\r matches 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; \n and \r are the pathological characters.)

Fix

Replace the regex with stripLeadingQuoteLine, a single left-to-right scan that is behavior-identical and O(n):

  1. Skip the leading whitespace run (same \s class).
  2. If the next char is not >, return the input unchanged (the old regex was ^-anchored without m, so it could only ever match once, at index 0 — a quote anywhere else was never stripped).
  3. Otherwise skip to the end of that line (. stopped at line terminators \n, \r, \u2028, \u2029 only) and then skip all trailing whitespace — exactly one quote line is removed, matching the old single-replacement behavior.

Behavior-parity strategy

  • 13 characterization tests were written first and run against the old regex implementation to lock in observed outputs (quote at start / mid-body, leading whitespace/newlines/tabs before the quote, multi-line blockquote strips only the first line, CRLF, unicode bodies, quote-only bodies, empty body, stripQuotes true/false, URL stripping, 140-char truncation). The same tests pass unchanged against the new implementation.
  • A differential fuzz (old regex applied inline, then shared pipeline vs. new implementation, stripQuotes in {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 extractBodySummary incl. markdown parse, median of 5)

Input (stripQuotes=true) Old regex New scan
20/22/24/26 leading newlines 24 / 94 / 310 / 898 ms < 0.1 ms each
30 leading newlines 10 702 ms < 0.1 ms
10 000 leading newlines does not terminate (killed) 5.2 ms
10 000 spaces 0.1 ms 2.4 ms
10 000 mixed whitespace (\n \t\r x2500) does not terminate (killed) 2.9 ms
10 000 newlines + matching > quote fast 0.7 ms

Test plan

  • New: __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.ts is from the baseline commit and unrelated to this change).
  • pnpm typecheck: clean.

Fixes audit finding CD-01. Does not merge automatically — review welcome.

…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.
@ety001
ety001 merged commit 390dce8 into next Oct 19, 2026
0 of 2 checks passed
@ety001
ety001 deleted the fix/cd-01-linear-quote-strip branch October 19, 2026 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant