Skip to content

Off-by-one in 1.x urlencoded parameterCount breaks parsing arrays #715

Closed

Description

@krzysdz

Discovered in expressjs/express#7135.

parameterCount has an off-by-one bug (returns 0 for "a=x", 1 for "a=x&b=y", ...). This in turn causes an array like "a[0]=1&a[1]=2&...&a[199]=200" to be parsed by qs with arrayLimit: 199.
This used to work, because arrayLimit: 199 would allow the index to be at most 199. [email protected] changed arrayLimit behaviour to instead restrict the array length (ljharb/qs#540 (comment), ljharb/qs@cfc108f, 6.14.2 changelog).

2.x uses a slightly different function and returns the real number of parameters.

More context on the differences: #652 (comment)

Environment information

Version: 1.20.4

Platform: Microsoft Windows NT 10.0.19045.0 x64

Node.js version: 25.8.2

Any other relevant information: When using public API, the problem can be observed only with qs >=6.14.2

What steps will reproduce the bug?

  1. Try parsing a large (>100 elements) array using extended urlencoded parser

Alternatively:

  1. Run npm test, the following test will fail:
    it('should parse array index notation with large array', function (done) {
    var str = 'f[0]=0'
    for (var i = 1; i < 500; i++) {
    str += '&f[' + i + ']=' + i.toString(16)
    }
    request(this.server)
    .post('/')
    .set('Content-Type', 'application/x-www-form-urlencoded')
    .send(str)
    .expect(function (res) {
    var obj = JSON.parse(res.text)
    assert.strictEqual(Object.keys(obj).length, 1)
    assert.strictEqual(Array.isArray(obj.f), true)
    assert.strictEqual(obj.f.length, 500)
    })
    .expect(200, done)
    })

Activity

  1. added theissue type on Mar 30, 2026
  2. changed the title [-]Off-by-one in 1.x urlencoded parameterCount[/-] [+]Off-by-one in 1.x urlencoded `parameterCount`[/+] on Mar 30, 2026
  3. changed the title [-]Off-by-one in 1.x urlencoded `parameterCount`[/-] [+]Off-by-one in 1.x urlencoded `parameterCount` breaks parsing arrays[/+] on Mar 30, 2026
  4. added a commit that references this issue on Mar 30, 2026
    75351de
  5. abhu85 commented on Mar 30, 2026

    @abhu85

    Submitted fix in #716. The function was returning the count of & characters instead of the actual parameter count (which is &count + 1). Added a test case to verify arrays with 110 elements are parsed correctly.

  6. added a commit that references this issue on Apr 23, 2026
    6f24d7e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions