Fixing maxBodyLength enforcement#3786
Fixing maxBodyLength enforcement#3786jasonsaayman merged 3 commits intoaxios:masterfrom PauloRSF:max-body-length-fix
Conversation
* Removed due to the error being thrown by axios itself now, instead of follow-redirects
|
But what if the body comes from a stream and not a buffer like in this example? let { PassThrough } = require("stream");
let s = new PassThrough();
s.end(Buffer.alloc(1000));
require("axios")
.put("http://www.example.com", s, { maxRedirects: 0, maxBodyLength: 100 })
.catch(e => console.log("axios returned error:", e));This is how it is in follow-redirects: // Only write when we don't exceed the maximum body length
if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
this._requestBodyLength += data.length;
this._requestBodyBuffers.push({ data: data, encoding: encoding });
this._currentRequest.write(data, encoding, callback);
}
// Error when we exceed the maximum body length
else {
this.emit("error", new MaxBodyLengthExceededError());
this.abort();
} |
* Adding request body length validation on HTTP adapter * Removing error code assertion on HTTP's body length support test * Removed due to the error being thrown by axios itself now, instead of follow-redirects Co-authored-by: Jay <[email protected]>
|
Hi 👋 |
|
This "fix" is not complete. It will not work for streams as I wrote earlier ( |
|
I'm guessing this is related to my issue... I recently upgraded from 0.24 to 0.26 and my file upload via export async function importAsset(uri: string, mime: string) {
const formData = new FormData();
const isImage = mime.startsWith('image/');
formData.append('file', {
name: uri.split('/').pop(),
type: mime,
uri,
});
formData.append('upload_preset', 'assets');
return axios.post(
`https://api.cloudinary.com/v1_1/abc123/${isImage ? 'image' : 'video'}/upload`,
formData,
);
} |
|
Yeah, our file upload broke as well (on Android only). Would be nice to know what in our end might need to be changed in order to upgrade to this version. |
The
maxBodyLengthoption was only being enforced by follow-redirects, so, the "body length exceeded" error was only thrown whenmaxRedirectswas set to 0. I added a body length validation before creating the request.Closes #3769.