Skip to content

fix: remove polynomial backtracking flagged by code scanning - #469

Open
wjsoj wants to merge 2 commits into
hypit-ai:mainfrom
wjsoj:fix/codeql-redos
Open

wjsoj wants to merge 2 commits into
hypit-ai:mainfrom
wjsoj:fix/codeql-redos

Conversation

@wjsoj

@wjsoj wjsoj commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Closes #481

This PR works through the 11 open code-scanning alerts. I checked each one against the latest main and timed a worst-case input with Node 24, 50k-character payload unless noted. The open regex PRs (#439, #445, #446, #458) were checked first so this PR doesn't repeat their work.

Alert Rule Location Classification Reasoning
#38 js/polynomial-redos packages/visual-track/src/author.ts (shadows) fixed here The SVS shadows recipe property is authored input. The final \s+(.+)$ let .+ compete with \s+ for whitespace, so a long whitespace run followed by a line break took ~23 s.
#37 js/polynomial-redos packages/text-fine/src/surface.ts (dedent) covered by #445 Real (same /\n\s*$/ as #5). Note on #445: its backward scan only drops the trailing run when the run begins with \n. /\n\s*$/ drops from the first newline inside the trailing whitespace run, so "abc \n " used to become "abc " and with #445 stays "abc \n ". The behaviour-preserving form is text.indexOf("\n", end) (used here for #5).
#36 js/polynomial-redos packages/runtime-local/src/node.ts (executableExists) fixed here (low impact) Windows-only check on a configured executable path. "."*n + "/" took ~6 s. The input is local runtime config, so the impact is low, but the fix is trivial.
#35 js/polynomial-redos packages/result/src/writer.ts (mediaExtension) false positive .replace(/[^a-z0-9]+/gu, "-") runs first and collapses every run of non-alphanumerics, - included, into a single -. So /^-+|-+$/ never sees more than one consecutive dash. Measured 0 ms.
#34 js/polynomial-redos packages/generation/src/model.ts (pascal) false positive (not reachable) The regex is quadratic on long separator runs (~7 s for "/"*n), but its only input is a generation port name. sealGenerationPortTable enforces ^[a-z][A-Za-z0-9]*$ on port names, so they contain no separators. Port tables are also declared by model-module authors, not by end users. Left unchanged.
#16 js/incomplete-url-substring-sanitization packages/provider-hypihub/test/upload.test.ts test-only, fixed here A fake-fetch route in a test, not a sanitizer. Adding the trailing slash ("https://s3.test/") matches the other routes in the same fixture.
#15 js/identity-replacement packages/ranking/test/ranking.test.ts test-only, fixed here type.name.replace(/Style$/u, "Style") was a no-op. It is now type.name.
#9 js/polynomial-redos packages/provider-hypihub/src/upload.ts (apiBaseUrl) fixed here (low impact) /\/+$/ is not anchored at the start, so it is quadratic on many slashes followed by another character (~5 s). The input is the configured base URL. #458 does not touch this code.
#8 js/polynomial-redos packages/provider-hypihub/src/provider.ts (apiBaseUrl) fixed here (low impact) Same as #9.
#5 js/polynomial-redos packages/comment-sticker/src/surface.ts (dedent) fixed here Authored Sticker body text. /\n\s*$/ restarts at every newline, so many newlines followed by text took ~5 s. Not covered by #445, which only changes text-fine.
#2 js/redos packages/markup/src/syntax.ts (REFERENCE) covered by #439 Real and exponential: "a" + ".a"*24 + "!" took ~4 s. Note on #439: it makes references stricter, not just faster. The current first segment's class already includes ., so a., a..b and a.1 are accepted today and are rejected under #439. If only the performance fix is wanted, ^[A-Za-z_][A-Za-z0-9_.:-]*$ accepts exactly the same language. Whether the stricter grammar is intended is up to the maintainers of that PR.

What changed

  • visual-track shadows: the color group is (\S.*) instead of (.+). The preceding \s+ is greedy and the entry is trimmed, so the two forms match the same entries with the same groups.
  • comment-sticker dedent: .replace(/^\n/u, "") becomes a startsWith check, and .replace(/\n\s*$/u, "") becomes a backward scan that finds the trailing whitespace run and cuts at its first \n.
  • runtime-local: /\.[^\\/]+$/ becomes hasExtension(), which looks at the last / or \ path segment and checks for a . that is not its last character.
  • provider-hypihub: one exported internal helper, trimTrailingSlashes() in upload.ts, replaces /\/+$/ in both apiBaseUrl functions.
  • Tests: removed the identity replace (Draw a caption's shadows behind its outline #15) and added the trailing slash on the S3 test route (Stop the environment-gated test scripts depending on sh #16).

To check that the results are unchanged, I compared each old regex with its replacement on 200k random strings over an adversarial alphabet (newlines, \r, U+2028, spaces, tabs, dots, slashes, backslashes, digits). There were 0 mismatches.

Tests added

  • packages/visual-track/test/visual-track.test.ts: decodeVisualFrameTreatment shadows. Covers multi-entry parsing, tab and line-break separators, the rejection cases (missing color, color spanning lines), and the 50k-run pathological entry. It finishes in about 8 ms.
  • packages/comment-sticker/test/comment-sticker.test.ts: body-text Sticker through decodeCommentStickerTrackSurface. Covers dedent of indented text, trailing spaces before the last newline, and 50k newlines followed by text. The exact result is asserted, and it finishes in about 10 ms.
  • packages/provider-hypihub/test/upload.test.ts: trimTrailingSlashes cases, including 100k slashes followed by a character, plus a check that a padded https://hub.test/v1// base URL still posts to https://hub.test/v1/files/uploads.
  • runtime-local has no new test. The changed check only runs on win32.

Commands run

  • pnpm install --frozen-lockfile: ok
  • pnpm check: ok (no errors)
  • pnpm test: passes in CI (Linux and Windows); all checks on this PR are green.
  • Affected test files run directly (node --import tsx --test …): visual-track, comment-sticker, provider-hypihub upload, all passing.

wjsoj added 2 commits November 5, 2026 08:03
Rewrite the regexes behind code-scanning alerts hypit-ai#38, hypit-ai#5, hypit-ai#36, hypit-ai#9 and hypit-ai#8
so they cannot backtrack super-linearly, keeping results identical:

- visual-track shadows: the color group now starts at a non-space
  character, so it no longer competes with the preceding \s+.
- comment-sticker dedent: trailing blank lines are trimmed by a
  backward scan instead of /\n\s*$/.
- runtime-local: the Windows extension check inspects the last path
  segment instead of /\.[^\\/]+$/.
- provider-hypihub: trailing slashes of the base URL are trimmed by a
  backward scan instead of /\/+$/.

Also clean up the two test-only alerts: drop an identity replace in the
ranking test (hypit-ai#15) and match the S3 test host with a trailing slash in
the upload test (hypit-ai#16).

Tests pin the previous results and run pathological inputs.
SanHsien added a commit to SanHsien/hypit that referenced this pull request Nov 7, 2026
…i#460-hypit-ai#474; advance watermarks

No PR was merged upstream, so nothing was cherry-picked: 11 PRs = 5 defer
(hypit-ai#464 hypit-ai#466 hypit-ai#467 hypit-ai#469 hypit-ai#471, pinned in monitored) + 6 not applicable; 5 issues =
1 defer (hypit-ai#473) + 4 not applicable. hypit-ai#464 npm pin is moot because the fork gates
publish jobs to upstream (b641cce). ROADMAP row updated with the deferred items.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
SanHsien added a commit to SanHsien/hypit that referenced this pull request Nov 7, 2026
…e URLs (port of upstream PR hypit-ai#469)

Ported before upstream merge, only the two spots that exist in this fork:
comment-sticker dedent (/\n\s*$/ retried from every newline; 200k newlines
took 21.8 s) and the HypiHub apiBaseUrl trailing-slash trim. Same results,
linear scan. Replace with upstream's version once hypit-ai#469 is merged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

This branch has not been deployed

No deployments
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.

Open CodeQL code-scanning alerts: triage and fixes

1 participant