Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes form submissions with empty file fields throwing exceptions (#150)
Given a form with a file filed i.e. `<input type="file"/>` if the user does not include a file to upload when the form is submitted the browser will send a request with the following multipart body: ``` -----------------------------101075975012956453003083734809 Content-Disposition: form-data; name="some-field" -----------------------------101075975012956453003083734809 Content-Disposition: form-data; name="a-file"; filename="" Content-Type: application/octet-stream -----------------------------101075975012956453003083734809-- ``` Since there is no file but there is a file field, the browser will send a part with empty filename and body and octet-stream as the content type to signify a submission without a file to upload. In cask to handle such form submissions you create an endpoint like the following: ``` @cask.postForm("/post") def post(image: cask.FormFile) = s"Image filename: ${image.fileName}" ``` The `postForm` decorator upon receiving the submission it will create an Undertow `MultiPartParserDefinition` and try to parse the request. After parsing the boundary and the headers of the part Undertow will then try to parse the body which is empty and because there is nothing to parse it will never create a file for the `FormValue` representing the empty file field (see `MultiPartParserDefinition` line 329): ![image](https://github.com/user-attachments/assets/cbfa167f-a870-413a-9f48-9890595ca042) This results to `FormData` with a `FormValue` where the `fileitem` field is `null`. ![image](https://github.com/user-attachments/assets/6fc890ba-6ae9-40be-9ab3-a5c7fd7e74fc) So when cask tries to convert the Undertow `FormValue`s to cask `FormEntry` by calling `FormEntry.fromUndertow` on each FormValue the `FormValue.isFile` condition returns false and cask tries to convert the Undertow `FormValue` to a cask `FormValue` and an exception is thrown when calling `getValue()` on a binary `FormValue`. ![image](https://github.com/user-attachments/assets/0f7715a4-c166-4dd2-b8f1-a2c3efb0bd10) ![image](https://github.com/user-attachments/assets/c69f804a-e54e-49d8-adf8-ce550a2f96e0) ![image](https://github.com/user-attachments/assets/e00b0328-c1b9-4037-98d5-2747c1ae2211) In essence if your endpoint is using the `postForm` decorator and is expecting a `FormField` cask will throw an exception if the user doesn't submit a file and there's no chance for the developer to do any validation. This PR is preventing the exception from being thrown by detecting the empty binary field and replicating the Undertow behaviour of passing a `FormValue` with null values (it's converted to Option[Path] in cask land) so that the code has a chance to perform validation and accept or reject the form submission. ``` def fromUndertow(from: io.undertow.server.handlers.form.FormData.FormValue): FormEntry = { val isOctetStream = Option(from.getHeaders) .flatMap(headers => Option(headers.get(HttpString.tryFromString("Content-Type")))) .exists(h => h.asScala.exists(v => v == "application/octet-stream")) // browsers will set empty file fields to content type: octet-stream if (isOctetStream || from.isFileItem) FormFile(from.getFileName, Try(from.getFileItem.getFile).toOption, from.getHeaders) else FormValue(from.getValue, from.getHeaders) } ``` Closes #149 --------- Co-authored-by: nk <nk>
- Loading branch information