Skip to content

fix: harden three regexes flagged by CodeQL polynomial-redos#55

Merged
nahiyankhan merged 1 commit intomainfrom
fix/redos-regexes
Apr 24, 2026
Merged

fix: harden three regexes flagged by CodeQL polynomial-redos#55
nahiyankhan merged 1 commit intomainfrom
fix/redos-regexes

Conversation

@nahiyankhan
Copy link
Copy Markdown
Collaborator

Summary

Three regexes in ghost-drift tripped CodeQL's js/polynomial-redos check when the compiled dist/ is scanned downstream (surfaced via squareup/ghost-fleet#17). All three were flagged for ambiguous quantifier overlap, not actual ReDoS exposure — inputs are short and controlled — but tightening them is cheap and clears the scanner.

  • FONT_SUFFIXES (embedding/compare.ts) — dropped the leading \s* that created an ambiguous split with the word alternation. Added a \s+ → " " squash before .trim() so any leftover whitespace is still collapsed.
  • scanHeadings (fingerprint/layout.ts) — /^(#{1,6})\s+(.*?)\s*$//^(#{1,6})\s(.*)$/ + .trim(). \s and . both match space characters; the original had overlapping quantifiers.
  • slug (fingerprint/layout.ts) — rewrote as a single imperative O(n) pass. The regex chain kept being flagged even after splitting /^-+|-+$/g in two.

Test plan

  • pnpm test — all 182 tests pass (incl. layout.test.ts, compare.test.ts, fragments.test.ts)
  • pnpm check — biome + typecheck + file-sizes + docs + cli-manifest all green
  • pnpm build — clean
  • slug() cross-verified against the old regex chain on: leading/trailing/interior dash runs, mixed case, empty string, all-dashes, unicode dropouts

🤖 Generated with Claude Code

- FONT_SUFFIXES: dropped the leading \s* that created an ambiguous split
  with the word alternation; added a \s+ squash before .trim() so any
  leftover whitespace is still collapsed.
- scanHeadings: /^(#{1,6})\s+(.*?)\s*$/ → /^(#{1,6})\s(.*)$/ with .trim()
  on the captured text. \s and . both match space characters, so the
  original had overlapping quantifiers.
- slug: rewrote as a single imperative O(n) pass. The regex chain kept
  getting flagged even after splitting /^-+|-+$/g in two.

Semantics preserved — verified against the existing layout tests and a
broader slug input set (leading/trailing/interior dash runs, mixed case,
unicode dropouts).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@nahiyankhan nahiyankhan merged commit cd72f12 into main Apr 24, 2026
6 checks passed
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