Repository navigation
[Fix] parse: avoid quadratic copying when combining duplicate keys - #652
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
`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
force-pushed
the
parse-duplicates-linear
branch
from
November 30, 2026 17:03
fb3b49c to
e7b61e4
Compare
ljharb
approved these changes
Nov 30, 2026
ljharb
left a comment
Owner
There was a problem hiding this comment.
Thanks! I rebased this onto master; no other changes. Confirmed it fails (without hanging) when 1eaaada4 is reverted.
Merged
Contributor
Author
|
thanks for rebasing and landing this, glad the test is doing its job against the revert too |
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.
parseValuesfolds each repeated key intoutils.combine, which returns[].concat(existing, val), so the Nth duplicate of a key copies the N-1 values already collected. While that array stays withinarrayLimitthe work is quadratic in the number of duplicates, which makes it an algorithmic-complexity DoS on untrusted query strings and form bodies. With the defaultarrayLimitof 20 the array turns into an overflow object early and appends cheaply after that, but body-parser's urlencoded parser passesarrayLimitequal to the parameter count (at least 100 in extended mode), so any app that raisesparameterLimitlets 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 ofa&a&a&…spends 3216 ms in the middleware onmainand 10 ms with the fix. The arraycombinereturns here is local toparseValues, so a later duplicate of the same key now appends to it in place while it stays withinarrayLimit. The first duplicate, an overflow object, and an array returned by a customdecoderstill go throughutils.combineas before, so a decoder's arrays are never mutated.qs.parsealone, with the options body-parser 2 passes, on node 22: 10,000 / 25,000 / 50,000 duplicates take 119 / 821 / 3357 ms onmainand 1.4 / 2.2 / 3.5 ms with the fix, and repeateda[]=xorcomma: truevalues (a=1,2repeated) improve the same way. A differential fuzz againstmain(1.2M random query strings acrossarrayLimit,duplicates,comma,throwOnLimitExceeded,strictNullHandling, the charset options, and custom decoders returning shared, sparse, orundefinedvalues) found no difference in the parsed structure, overflow markers, thrown errors, or mutation of decoder-returned arrays. The newutils.combinecall-count test fails onmain(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-onlypasses 1703 tests withlib/at 100% coverage, andeslintreports the same 18 pre-existing warnings asmain. Not run: the older-node CI matrix (the change uses nothing newer than the surrounding code), andeclint, which hangs on every file in this environment; the touched lines follow.editorconfig.