Axios: Download continues after maxContentLength exceeded

Created on 22 Sep 2017  路  7Comments  路  Source: axios/axios

聽Summary

The following code demonstrates the issue:

require('axios').get(
  'https://upload.wikimedia.org/wikipedia/commons/f/fe/A_Different_Slant_on_Carina.jpg',
  { maxContentLength: 2000 }
)
  .then(d => console.log('done'))
  .catch(e => console.log(e.toString()))

Expected behavior:

  • The script exits immediately after printing "Error: maxContentLength size of 2000 exceeded"
  • Not much more than 2KB was downloaded from the server

Actual behavior:

  • The script prints "Error: maxContentLength size of 2000 exceeded", then continues to download the remaining 52MB of data from the server. It takes about two minutes before it exits on my connection.

It's possible to work around this by adding a cancellation token and manually canceling the request when an error is encountered. However, since the request never actually fires a "complete" event, it's surprising that the download continues, only to have the data be thrown into the void.

聽Context

  • axios version: v0.16.2
  • Environment: node v8.4.0, macOS Sierra 10.12.6

Most helpful comment

Fix is released as part of 0.19.0; per @emilyemorehouse's comment there should be a more targeted 0.18.1 release made available as well shortly.

Let's all thank the maintainers and contributors for their generous work <3

All 7 comments

馃憤

Same problem here. I looked into the code of lib/adapters/http.js, and I made some tests.
Maybe we could just close the response stream when the maxContentLenght is reached ?

lib/adapters/http.js:173

if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
  + stream.destroy();
  reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
              config, null, lastRequest));
}

This simple modification works in my tests, the rest of the reponse body is not fully loaded into memory anymore.

Another way to obtain this behavior is to destroy the response stream in the catch handler in the client code:

axios({
  url: ...,
  maxContentLength: 2000,
}).catch((error) => {
  error.request.res.destroy();
});

This is an acceptable solution for me, don't know if you want to make this behavior the default ? (I think it would me more sensible).

At least we could add this method in the documentation ?

Thanks in advance.

What's the likelihood of this PR getting approved / merged?

Sourceclear is raising this as a vulnerability, with this PR being the fix:
https://www.sourceclear.com/vulnerability-database/security/denial-of-service-dos-/javascript/sid-6130

It is getting a high severity security hole now from GitHub, what is going on?
dependanbot shows continually there isn't a single fix for that.

I'm pretty sure the maintainers are AWOL.

Does this impact Axios in browser or just server?

As mentioned on the Snyk blog

_If you are requesting resources from untrusted sources, or via insecure mediums, then you are potentially vulnerable to Denial of Service where malicious users can control the remote resource._

_When using axios in a Node.js server this can be disastrous due to the single threaded nature of the runtime. A spike in resources such as I/O and CPU negatively affect all users connected to that server. In browser environments, the Denial of Service negatively affects end-users with varying severity, depending on how the resource fetching with axios is used in the application._

Fix is released as part of 0.19.0; per @emilyemorehouse's comment there should be a more targeted 0.18.1 release made available as well shortly.

Let's all thank the maintainers and contributors for their generous work <3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

achingbrain picture achingbrain  路  3Comments

c0debreaker picture c0debreaker  路  3Comments

tbaustin picture tbaustin  路  3Comments

altruisticsoftware picture altruisticsoftware  路  3Comments

jdpagley picture jdpagley  路  3Comments