fastify / fastify-multipart

Multipart support for Fastify
MIT License
487 stars 103 forks source link

Strange behaviour with request.file() when filename in form is not specified #504

Open giovanni-bertoncelli opened 10 months ago

giovanni-bertoncelli commented 10 months ago

Prerequisites

Fastify version

4.23.2

Plugin version

8.1.0

Node.js version

20.x

Operating system

macOS

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

Sonoma 14.2

Description

I encountered a strange behavior in the request.file() method when I send a multipart (using form-auto-content) without specifying the filename of the sent file. The method returns undefined. I do not know if it is a form-auto-content issue, if this is the expected behaviour or not. If it is expected I'll close this.

Steps to Reproduce

Reproduction example: https://codesandbox.io/p/devbox/fastify-multipart-filename-nxnqgd

Expected Behavior

I expect an error (if multipart with part missing the filename are considered malformed) or otherwise the parsed file.

gurgunday commented 9 months ago

For now, we definitely don’t throw in many cases where we maybe should, this could be an interesting change that I support

Silent errors should be opt-in

decipher-cs commented 1 week ago

Seconded. This should most definitely throw an error or warning of some kind. In the meantime, I highly recommend adding a note in the README that documents this behavior.

data will be undefined in this case unless I add the name attribute to the input in my client code

fastify.post("/upload", async function handler(request, reply) {
  const data = await request.file();
  log(data?.file); //undefined
  return "done";
});
<form method="post" action="upload" enctype="multipart/form-data">
  <input type="file" />
  <button type="submit">send</button>
</form>

the code will work.

<input 
        type="file" 
++      name="some-name"
    />