Repository navigation
feat!: enforce default fieldArrayIndexLimit of 1000 - #1477
official-burak wants to merge 2 commits into
Conversation
kilisamemarisaaa
left a comment
There was a problem hiding this comment.
I found a false positive in exceedsArrayIndexLimit for a fallback key containing a nested [.
append-field@2.0.0 parses items[[1001] as a literal object key (the bracket group is [1001), not an array path: its numeric step matcher requires [ to be followed immediately by digits. However, this PR's global /\\[(\\d+)\\]/g scan finds the inner [1001] and rejects the field with the default limit:
items[[1001] rejected LIMIT_FIELD_ARRAY_INDEX items[[1001]
items[1001]tail accepted {"items[1001]tail":"value"}
I reproduced this on d6d25b4b in multer-pr1477; the existing 71-test suite and lint still pass. This contradicts the stated “literal fallback keys … left alone” behavior and breaks a previously accepted field name under the new default. Please add a regression test and make the scan follow append-field's actual path grammar (so digits inside an object-key bracket are not treated as an array step).
A bracket group is an array index only when it holds nothing but digits, and a name that does not parse completely stays a literal key. Digits inside an object-key group such as the [1001 key in items[[1001] are therefore no longer counted as an array step.
|
Good catch, thanks for the detailed report and the reproduction. The check now walks the name using the same grammar append-field does: a bracket group only counts as an array index when it holds nothing but digits, and a name that does not parse completely stays a literal key. So Both cases are covered as regression tests in ae4f9ff. |
yxshee
left a comment
There was a problem hiding this comment.
I checked out ae4f9ff on top of v3 and ran it on Node 22.
- The new file has 10 tests and all pass. On
v3without thelibandindex.jschanges, 2 of them fail ("reject an index above the default limit" and "oversized index nested behind object keys", bothMissing expected rejection). The other 8 pass there too, which is expected since they only check names that should be accepted. - The full suite gives 68 passing, 5 failing. The same 5 fail on plain
v3on my machine (aborted requests x2, Express integration, orphan cleanup x2), and v3's own CI has been red since August, so they aren't from this PR.standardis clean. - I compared
exceedsArrayIndexLimitwith what append-field 2.0.0 actually builds. I generated 300,000 random names froma,[,],[],[6],[[,]], digits and so on, calledappendFieldon each one, and checked whether the result contained an array longer thanlimit + 1. With limit 5, the function agreed with append-field on every name. So the grammar walk in the second commit does match the parser, includingitems[[1001]anda[6]suffix.
One thing to decide before merge: fieldArrayIndexLimit goes through parseLimit, so { fieldArrayIndexLimit: Infinity } now throws Invalid limit "fieldArrayIndexLimit" given: Infinity. On 2.x, Infinity is the documented default. Someone who set it explicitly to keep large indices will get a constructor error after upgrading, and fieldNestingDepth on v3 still accepts Infinity. If that's intended (the other parseLimit limits behave the same way), a line in the migration notes saying to use Number.MAX_SAFE_INTEGER would help. parseLimit also passes the value through bytes, so '1KB' is taken as 1024. That's harmless, just a bit odd for an index.
Related, but outside this PR: the 2.x check from #1438 on main still has the false positive this PR fixes. With fieldArrayIndexLimit: 1000, multer 2.5.0 rejects items[[1001] even though append-field 1.0.0 stores it as the object key '[1001'. The walk here could be backported.
The change looks correct to me as written.
Ulises asked for this on v3 in #1461, matching the fieldNestingDepth default in #1433. A name like
items[1000000]currently builds a huge sparse array even when only one value is sent, so this ports the index check from #1438 and defaults it to 1000.items[1000]still works,items[1001]is rejected withLIMIT_FIELD_ARRAY_INDEX, and you can raise the limit if you need larger indices. Literal keys such asitems[1000]tailare left alone.