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

Limit and File Handling on addToBody Option #435

Closed
2 tasks done
maxkre96 opened this issue Apr 30, 2023 · 3 comments · Fixed by #481
Closed
2 tasks done

Limit and File Handling on addToBody Option #435

maxkre96 opened this issue Apr 30, 2023 · 3 comments · Fixed by #481
Labels
bug Confirmed bug

Comments

@maxkre96
Copy link

maxkre96 commented Apr 30, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.15.0

Plugin version

7.6.0

Node.js version

16.13

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

13.3.1

Description

When using @fastify/multipart empty files are returned when the file size limit is exceeded (default: 1mb).

If I send files larger than the limit to the API and access them body, I receive empty files when the file size exceeds the limit.
e.g:

File with 1.1mb
{ data: undefined, filename: 'DJI_0366.JPG', encoding: '7bit', mimetype: 'image/jpeg', limit: true }

If the file is smaller then the limit everything works correctly and the data field is defined.

File with 0.8mb:

{ data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e2 02 28 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 02 18 00 00 00 00 04 30 00 00 ... 212574 more bytes>, filename: 'DJI_0365.JPG', encoding: '7bit', mimetype: 'image/jpeg', limit: false }

Reason

file.on('limit', () => {
    lastFile.limit = true
  })
file.on('end', () => {
    if (!lastFile.limit) {
      lastFile.data = Buffer.concat(fileData)
    } else {
      lastFile.data = undefined
    }
})

Steps to Reproduce

Register module with the addToBody Option

app.register(fastifyMultipart, { addToBody: true });

And access the files e.g. using nestjs
@Post() post(@Body() body: { files: Buffer[] }): void { return this.appService.createFiles(body.files); }

Expected Behavior

Either ban this option or return a 413 error message if the file exceeds the size limit.
This is a serious issue that can result in data loss

@maxkre96 maxkre96 changed the title Limit and File Handlin on addToBody Option Limit and File Handling on addToBody Option Apr 30, 2023
@mcollina
Copy link
Member

mcollina commented May 2, 2023

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@mcollina mcollina added the bug Confirmed bug label May 2, 2023
@Uzlopak
Copy link
Contributor

Uzlopak commented May 25, 2023

Our busboy implementation will give the feedback that the file is to big and cut off the file stream. Thats why your big file is smaller. What is missing is Handling the feedback and error with file too big.

@gurgunday
Copy link
Member

addToBody is not documented in the README anymore and I believe is deprecated. If you use attachFieldsToBody: true instead, it does throw when the limit is exceeded

Other than that, I think removing these undocumented options is the best way forward

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