Skip to content

[Fix] parse: avoid quadratic copying when combining duplicate keys - #652

Merged
ljharb merged 2 commits into
ljharb:mainfrom
zul5050:parse-duplicates-linear
Nov 30, 2026
Merged

ljharb merged 2 commits into
ljharb:mainfrom
zul5050:parse-duplicates-linear

Conversation

@zul5050

@zul5050 zul5050 commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

parseValues folds each repeated key into utils.combine, which returns [].concat(existing, val), so the Nth duplicate of a key copies the N-1 values already collected. While that array stays within arrayLimit the work is quadratic in the number of duplicates, which makes it an algorithmic-complexity DoS on untrusted query strings and form bodies. With the default arrayLimit of 20 the array turns into an overflow object early and appends cheaply after that, but body-parser's urlencoded parser passes arrayLimit equal to the parameter count (at least 100 in extended mode), so any app that raises parameterLimit lets one request with a single key repeated N times cost O(N²) CPU in middleware that usually runs before authentication. With body-parser 2.4.0, urlencoded({ parameterLimit: 50000 }) and the default 100kb body limit, a 99,999-byte POST of a&a&a&… spends 3216 ms in the middleware on main and 10 ms with the fix. The array combine returns here is local to parseValues, so a later duplicate of the same key now appends to it in place while it stays within arrayLimit. The first duplicate, an overflow object, and an array returned by a custom decoder still go through utils.combine as before, so a decoder's arrays are never mutated.

qs.parse alone, with the options body-parser 2 passes, on node 22: 10,000 / 25,000 / 50,000 duplicates take 119 / 821 / 3357 ms on main and 1.4 / 2.2 / 3.5 ms with the fix, and repeated a[]=x or comma: true values (a=1,2 repeated) improve the same way. A differential fuzz against main (1.2M random query strings across arrayLimit, duplicates, comma, throwOnLimitExceeded, strictNullHandling, the charset options, and custom decoders returning shared, sparse, or undefined values) found no difference in the parsed structure, overflow markers, thrown errors, or mutation of decoder-returned arrays. The new utils.combine call-count test fails on main (99 calls instead of 1) and passes with the fix, and the decoder test pins that the decoder's array is left alone. npm run tests-only passes 1703 tests with lib/ at 100% coverage, and eslint reports the same 18 pre-existing warnings as main. Not run: the older-node CI matrix (the change uses nothing newer than the surrounding code), and eclint, which hangs on every file in this environment; the touched lines follow .editorconfig.

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3a58258) to head (e7b61e4).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #652   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines         3651      3698   +47     
  Branches       369       374    +5     
=========================================
+ Hits          3651      3698   +47     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

ljharb and others added 2 commits November 30, 2026 08:26
`utils.combine` copies its first argument,
so folding N duplicates of a key through it is O(N^2) while the result stays within `arrayLimit`.
(body-parser sets `arrayLimit` to the parameter count, so 50,000 duplicates took about 3 seconds)
The array `combine` returned for a key earlier in the same parse is referenced by nothing else,
so later duplicates now append to it in place,
keeping holes from a decoder's sparse arrays.
The first duplicate, an overflow object,
and a decoder's own arrays still go through `utils.combine`.

Co-authored-by: Zulfareen Shaikh <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
@ljharb
ljharb force-pushed the parse-duplicates-linear branch from fb3b49c to e7b61e4 Compare November 30, 2026 17:03

@ljharb ljharb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I rebased this onto master; no other changes. Confirmed it fails (without hanging) when 1eaaada4 is reverted.

@ljharb
ljharb merged commit e7b61e4 into ljharb:main Nov 30, 2026
450 checks passed
@ljharb ljharb mentioned this pull request Dec 1, 2026
@zul5050

zul5050 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

thanks for rebasing and landing this, glad the test is doing its job against the revert too

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.

2 participants