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

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

Open
2 tasks done
giovanni-bertoncelli opened this issue Jan 22, 2024 · 2 comments
Open
2 tasks done
Labels
discussion Issues or PRs with this label will never stale

Comments

@giovanni-bertoncelli
Copy link

giovanni-bertoncelli commented Jan 22, 2024

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.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

  • Setup a fastify server with the @fastify/multipart plugin with a route calling req.file()
  • Setup a simple client using form-auto-content and appending a file specifying the contentType but not the filename
  • Call the endpoint

Expected Behavior

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

@gurgunday
Copy link
Member

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

@gurgunday gurgunday added the discussion Issues or PRs with this label will never stale label Jan 31, 2024
@decipher-cs
Copy link

decipher-cs commented Nov 9, 2024

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"
    />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Issues or PRs with this label will never stale
Projects
None yet
Development

No branches or pull requests

3 participants