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:
Actual behavior:
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.
馃憤
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
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