fastify-multipart: fileSize limit on fastify-multipart did not throw error

I don’t know if I use it correctly or not but it is just pass through if I passing file with size higher than limit.

/*
 * other code
 */

fastify.post('/upload', async function (req, reply) {
    const data = await req.file({
      limits: {
        fileSize: 1 * 1024 * 1024,  // 1mb limit
      }
    })

    await pump(data.file, fs.createWriteStream(data.filename))

    reply.send()
})

It write file to my directory, when I expect it to throw error

  • node version: 14.15.3
  • fastify version: 3.9.2
  • fastify multipart version: 3.3.1
  • os: WSL

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (14 by maintainers)

Most upvoted comments

Yes, busboy marks a file with truncated when the file limit was reached. In my opinion, an error should be thrown but I can’t remember why we didn’t implement it. According to busboy the library will only emit a limit event on the file object.

I guess we should error this.

I think the current behavior is correct because we can only guarantee error handling for a stream when we iterate over all parts until onEnd is called. In all other cases, the user can check for file.truncated after the stream was consumed. The req.file API is error-prone because it can’t report the final error.

hi all, I just try the new version 4.0.0 but unfortunately the problem still exist when using req.file, req.files and req.parts but works when using req.saveRequestFiles.

I couldn’t reproduce it. It is only valid for req.file. Please provide a reproducible example.

I think I found the issue. await file() returns the file immediately but the error is pushed asynchronously to the queue and it will never be consumed. We have to ensure internally that all events are consumed.

It works for req.saveRequestFiles due to that exact reason because all files are consumed and it is guaranteed that the error is consumed as well.

For now, throwing error is the simplest solution. We can introduce an option throwFileLimit to enable throwing behavior and which the default is false. If we manage to bump an major, change it to default true.