Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to limit file size - multipart/form-data #140

Closed
alexaleluia12 opened this issue Mar 8, 2019 · 4 comments
Closed

How to limit file size - multipart/form-data #140

alexaleluia12 opened this issue Mar 8, 2019 · 4 comments

Comments

@alexaleluia12
Copy link

alexaleluia12 commented Mar 8, 2019

Problem/Feature Request Summary

Limit file size using multipart/form-data
Related issue:
#66
#23

Environment Information

  • koa-body: 4.1.0
  • Koa: 2.7.0
  • Node.js: 10.15.0

Current Behavior

Allow send file bigger than formLimit option.
Is this a bug ? If i can send a body bigger than defined value. Is this propertie affect the body only in certain conditions ?

Steps to Reproduce

git clone https://github.com/alexaleluia12/koa-upload.git
npm i
npm run start

send a txt file bigger than 1k, using multipart/form-data, pass the file through file field

Expected Behavior

Throw an error

Context (Environment)

I want to send a file to server.

@katanacrimson
Copy link
Contributor

Looks like it's only for urlencoded forms.

Perhaps something for 5.0.0 (ding #139) where we hook up formLimit up to formidible's maxFieldsSize property and then better document how one can limit the actual file upload size limit (which is actually formidible's maxFileSize option.

Would def be a BC breaker though.

@MarkHerhold
Copy link
Contributor

koa-body users formidable to perform file parsing so the option needs to be passed to formidable. I think this behavior is understandably confusing and should probably be addressed in a later release (ref #139).

A few other notes about your sample code:

  • I recommend removing the error handler
  • Math.random() isn't cryptographically secure, just FYI

I would change the options in your code to:

app.use(koaBody({
    multipart: true,
    json: true,
    formLimit: MAX_BODY,
    textLimit: MAX_BODY,
    formidable: {
        maxFileSize: MAX_BODY
    }
}));

@MarkHerhold
Copy link
Contributor

@damianb ha! you beat me by seconds 😆

@katanacrimson
Copy link
Contributor

Hah.

Anyways, I do think we ought to directly expose our own upload limit property (proxied to formidible's maxFileSize) for something a little easier to work with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants