Repository navigation
Conversation
|
Review requested:
|
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. Caution AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context. |
parseParams searched for '=' from the start of every pair. In a run of
pairs with no '=' after them, each search scanned to the end of the
string, so new URLSearchParams('a&'.repeat(n)) took quadratic time:
about 1.6 s for 262,144 pairs against 7.5 ms for 'a=b&'. Request's
formData() reaches the same loop with an urlencoded body.
Keep the position of the next '=' and search again only once the
parser has passed it.
Fixes: nodejs#67652
Refs: nodejs#65363
Signed-off-by: Aayush <21987529+aayushbtw@users.noreply.github.com>
b77080c to
bf418cf
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #67653 +/- ##
==========================================
+ Coverage 90.71% 90.73% +0.01%
==========================================
Files 801 801
Lines 280906 280911 +5
Branches 54249 54249
==========================================
+ Hits 254832 254892 +60
+ Misses 16246 16203 -43
+ Partials 9828 9816 -12
🚀 New features to boost your workflow:
|
Renegade334
left a comment
There was a problem hiding this comment.
confidence improvement accuracy (*) (**) (***)
url/url-searchparams-parse-no-equals.js n=100 pairs=10 pair='a' * 9.75 % ±8.82% ±11.74% ±15.29%
url/url-searchparams-parse-no-equals.js n=100 pairs=10 pair='a=b' -2.58 % ±9.75% ±12.98% ±16.90%
url/url-searchparams-parse-no-equals.js n=100 pairs=100 pair='a' 4.43 % ±9.81% ±13.06% ±17.02%
url/url-searchparams-parse-no-equals.js n=100 pairs=100 pair='a=b' -0.13 % ±6.92% ±9.21% ±11.99%
url/url-searchparams-parse-no-equals.js n=100 pairs=1000 pair='a' *** 48.15 % ±9.25% ±12.34% ±16.13%
url/url-searchparams-parse-no-equals.js n=100 pairs=1000 pair='a=b' -5.37 % ±5.87% ±7.81% ±10.16%
parseParamssearched for=from the start of every pair. In a run of pairs with no=after them, each search scanned to the end of the string, sonew URLSearchParams('a&'.repeat(n))took quadratic time.Request#formData()reaches the same loop with an urlencoded body, so a client can block the event loop with one body: a 21 MB body ofa&held a server's CPU for over 4 minutes before it was stopped.This keeps the position of the next
=across pairs and searches again only once the parser has passed it.Timings of the loop copied into a script, median of 5, v26.13.1 arm64:
a&The copied loop with and without the change returned the same output for 300,000 random strings over
a b = & ? + %2 0. The new test covers pairs with no=before and after pairs that have one, empty keys and values, a leading?, and encoded input. The new test checks that output; it passes before and after the change, since it guards the parse result rather than its speed. The speed is in a new benchmark,benchmark/url/url-searchparams-parse-no-equals.js. On v26.13.1, without the change,pairs=1e5runs 4.6 ops/s foraagainst 306 ops/s fora=b.I have not built Node locally, so I haven't run the full suite or the benchmark against the change; Node's ESLint config (
tools/eslint) passes on the three files.Fixes: #67652
Refs: #65363
Disclosure: I found and fixed this with a coding agent, which also opened this PR and #67652 from my account. I checked the fix against the source, compared the old and new loop on 300,000 random inputs, ran Node's ESLint, and timed the new benchmark on v26.13.1. I'll answer review comments myself.