axios icon indicating copy to clipboard operation
axios copied to clipboard

`RangeError: Invalid array length` when `data` is set to a large `Buffer`

Open Cafe137 opened this issue 6 months ago • 3 comments

Describe the bug

Axios triggers a RangeError: Invalid array length when data is set to a large Buffer

Explanation

When axios merges configurations, it checks for empty objects:

function isEmptyObject(val) {
  return val && Object.keys(val).length === 0 && Object.getPrototypeOf(val) === Object.prototype;
}

Calling Object.keys on a Buffer instance will try to enumerate every byte as a key. Node.js decides to die when there are more than 100-200 million keys (100-200MB byte length).

Workaround

import axios from 'axios'

main()

async function main() {
    const buffer = Buffer.alloc(1024 * 1024 * 200) // 200 MB buffer
    const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)
    await axios({
        method: 'POST',
        url: 'http://localhost:8080',
        data: arrayBuffer
    })
}

Passing ArrayBuffer instead of Buffer solves the issue, but it is not ideal as it requires additional conversion.

To Reproduce

import axios from 'axios'

main()

async function main() {
    const buffer = Buffer.alloc(1024 * 1024 * 200) // 200 MB buffer
    await axios({
        method: 'POST',
        url: 'http://localhost:8080',
        data: buffer
    })
}

Output:

~/node_modules/axios/lib/utils.js:134
  return val && Object.keys(val).length === 0 && Object.getPrototypeOf(val) === Object.prototype;
                       ^

RangeError: Invalid array length
    at Function.keys (<anonymous>)
    at Object.isEmptyObject (~/node_modules/axios/lib/utils.js:134:24)
    at getMergedValue (~/node_modules/axios/lib/core/mergeConfig.js:21:22)
    at valueFromConfig2 (~/node_modules/axios/lib/core/mergeConfig.js:43:14)
    at computeConfigValue (~/node_modules/axios/lib/core/mergeConfig.js:98:23)
    at Object.forEach (~/node_modules/axios/lib/utils.js:281:10)
    at mergeConfig (~/node_modules/axios/lib/core/mergeConfig.js:96:9)
    at Axios.request (~/node_modules/axios/lib/core/Axios.js:41:12)
    at wrap (~/node_modules/axios/lib/helpers/bind.js:5:15)
    at main (~/src/axios.ts:7:11)

Node.js v20.18.3

Code snippet


Expected behavior

Since Buffer is common, e.g. the default return types of readFile or readFileSync are Buffers, naively trying to upload a large file in Node.js will trigger this error.

When typeof Buffer !== 'undefined', axios should detect and work with it properly during configuration merging.

Axios Version

0.30.0

Adapter Version

Node.js default

Browser

No response

Browser Version

No response

Node.js Version

v20.18.3

OS

MacOS Sonoma 14.6.1

Additional Library Versions


Additional context/Screenshots


Cafe137 avatar Jun 26 '25 00:06 Cafe137

Hello! I would like to work on this issue. Could you please assign it to me?

salonisaboo avatar Sep 26 '25 19:09 salonisaboo

@salonisaboo I believe it was fixed in https://github.com/axios/axios/pull/6961 but I am not sure if it landed in the 0.x versions.

Cafe137 avatar Sep 29 '25 19:09 Cafe137

i am interested this issue, please assign me

Dhanashripatil123 avatar Oct 09 '25 16:10 Dhanashripatil123