fix(core): avoid quadratic backtracking in path parameter substitution - #888
fix(core): avoid quadratic backtracking in path parameter substitution#888Dhirenderchoudhary wants to merge 2 commits into
Conversation
|
@Dhirenderchoudhary is attempting to deploy a commit to the corsair Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 10 included reviews per hour; 6 remain after this review. 📝 WalkthroughWalkthroughThe URL builder now matches brace-delimited placeholders without crossing braces. Tests cover substitution behavior, encoding, API-version replacement, malformed placeholders, and large unclosed-brace input. ChangesURL substitution
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to The PR changes path-parameter matching to avoid pathological backtracking while preserving substitution behavior, with focused regression coverage. No actionable merge-blocking risk remains beyond normal checks and review. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryThe PR replaces the path-parameter substitution regex with a brace-bounded matcher to avoid quadratic backtracking while retaining normal placeholder behavior.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains. Important Files Changed
Reviews (2): Last reviewed commit: "test(core): document substitution edge c..." | Re-trigger Greptile |
Description
getUrlsubstitutes path parameters with a lazy quantifier:.replace(/{(.*?)}/g, …).*? backtracks from every { in the string, so the scan is quadratic in the
number of unmatched opening braces. CodeQL flags it as a polynomial regular
expression on uncontrolled data.
This replaces the lazy quantifier with an explicit negated class:
.replace(/\{([^{}]*)\}/g, …)[^{}]* cannot cross a brace, so there is nothing to backtrack over and the
scan is linear. It matches exactly the same well-formed {placeholder} tokens.
Impact
Every plugin request goes through getUrl, and options.url is built from
endpoint paths that interpolate caller-supplied values. A value carrying many
unmatched { characters stalls the event loop before the request is even sent.
Tests
packages/corsair/tests/request-path-substitution.test.ts — 6 tests driving the
real request() with a stubbed fetch, asserting the resolved URL.
Five cover substitution behaviour so the change is provably semantics-preserving:
single placeholder, multiple placeholders, unmatched placeholder left untouched,
value encoding, and {api-version}.
The sixth is the regression guard: it builds the hostile input above and asserts
substitution completes in under 1s. With the lazy quantifier restored it fails
at 22,664 ms; the other five still pass, which is what confirms the fix changes
performance and not behaviour.
pnpm --filter corsair test -- tests/request-path-substitution.test.tsChecklist
Before submitting your PR, please verify the following:
pnpm lintand all checks passpnpm typecheckand there are no TypeScript errorspnpm buildand all packages build successfullypnpm testand all tests passScreenshots / Demos (if applicable)
Additional Notes
Summary by CodeRabbit
Bug Fixes
Tests