fix(security): avoid polynomial-time ReDoS in trimTrailingSlash - #10
Merged
Merged
Conversation
CodeQL js/polynomial-redos (High, CWE-1333/400) flagged the `/\/+$/` regex in trimTrailingSlash. Because baseUrl reaches this regex from the SDK's public API, an input with many '/' characters (e.g. "/".repeat(n) + "x") forces O(n^2) backtracking, enabling a denial-of-service. Strip trailing slashes with a linear character scan instead. Behavior is identical to the previous regex on all inputs; the leading-slash strip on the next line (`/^\/+/`) is anchored and linear, so it is left unchanged.
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.
Summary
Fixes code scanning alert #2 — Polynomial regular expression used on uncontrolled data (
js/polynomial-redos, High, CWE‑1333/400/730) atsrc/index.ts:273.The
trimTrailingSlashhelper used the regex/\/+$/:Because
baseUrlreaches this regex through the SDK's public API (createMissionControlPluginClient({ baseUrl })→normalizeBaseUrl→trimTrailingSlash, and also viajoinURL), the input is uncontrolled. On a string with many/characters that isn't a match — e.g."/".repeat(n) + "x"— the engine matches\/+greedily, fails the$anchor, backtracks one slash, fails again, and so on: O(n²) work, i.e. a denial‑of‑service vector.Fix
Strip trailing slashes with a linear character scan instead of a backtracking regex:
The leading‑slash strip on the next line (
path.replace(/^\/+/, "")injoinURL) is anchored at^with nothing following it, so it is linear and was correctly not flagged — left unchanged.Verification
"","/","//","////","https://mc.example.com/","/api/mission-control"," https://x/y// ","a/b/c")."/".repeat(100000) + "x"payload now completes in ~0.07 ms (linear).pnpm check(tsc),pnpm build, andpnpm test(9/9) all pass.🤖 Generated with Claude Code
https://claude.ai/code/session_01Cz4tJqTqv4TdxxAKdRMdDH
Generated by Claude Code