denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.28k stars 5.41k forks source link

`FormData` from `<input type=file>` with no file selected gives inconsistent results in browser vs Deno (empty file vs empty string) #24964

Open lionel-rowe opened 3 months ago

lionel-rowe commented 3 months ago

Version: Deno 1.45.5

Given the following HTML:

<form method=post enctype="multipart/form-data">
    <input type=file name=files multiple>
    <input type=submit>
</form>

Assuming no files are selected, calling FormData#get('files') on a FormData object constructed from this form on the client side (tested latest Chrome, FireFox) gives an empty File object. However, calling (await req.formData()).get('files') on the Request given by Deno.serve gives an empty string "" instead.

Notably, FormData#getAll('files') gives an array of length 1 in both Deno and browsers, which hopefully everyone can agree is a horrible way of representing zero files, but the content of that array's element differs (again, it's an empty file on client side vs an empty string on Deno).

Repro: https://dash.deno.com/projects/formdata-no-files-repro

lionel-rowe commented 3 months ago

Observed browser behavior looks to be correct per the spec:

  1. Otherwise, if the field element is an input element whose type attribute is in the File Upload state, then:
    1. If there are no selected files, then create an entry with name and a new File object with an empty name, application/octet-stream as type, and an empty body, and append it to entry list.

Meanwhile, the raw HTTP request body looks something like this:

------WebKitFormBoundary5jMC0I81oPK5c8nU
Content-Disposition: form-data; name="files"; filename=""
Content-Type: application/octet-stream

------WebKitFormBoundary5jMC0I81oPK5c8nU--