Play Framework 2.5.9 Scala multipart/form-data file field allways Some

626 views
Skip to first unread message

Sergey Dashko

unread,
Nov 8, 2016, 10:31:22 PM11/8/16
to Play Framework
Hello.
 

I'm following documentation from here https://www.playframework.com/documentation/2.5.x/ScalaFileUpload to handle file upload in form. My code is here:

  def add(method: String) = Action(parse.multipartFormData) { implicit request =>
    if (method == "GET") {
      Ok(views.html.add(uploadForm))
    } else {
      uploadForm.bindFromRequest().fold(
        hasErrors => BadRequest(views.html.add(hasErrors)),
        form => {
          Logger.debug(request.body.file("myfilefield").toString)
          Ok(views.html.add(uploadForm))
        }
      )
    }
  }

Whenever I submit this form, even if I didn't select file in the form I'm getting something like this in console:

[debug] application - Some(FilePart(myfilefield,,Some(application/octet-stream),TemporaryFile(/var/folders/n3/8nrp7hw94bqbsjbcz00n5hk40000gn/T/playtemp4371116121043398878/multipartBody1548294152084205009asTemporaryFile)))

I can't understand why is this so. I think there are should be None in request.body.file("myfilefield") if I didn't select file before submitting form. How can I find if file was submitted?

Actually I was tried to downgrade Play to 2.5.4 and got the same result.


Sergey Dashko

unread,
Nov 9, 2016, 1:53:43 AM11/9/16
to Play Framework
Ok. Now I just copied code from documentation and got the same result :( 
Here is my controller action:
def upload = Action(parse.multipartFormData) { request =>
request.body.file("picture").map { picture =>
import java.io.File
val filename = picture.filename
val contentType = picture.contentType
picture.ref.moveTo(new File(s"/tmp/picture/$filename"))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.HomeController.uploadtest()).flashing(
"error" -> "Missing file")
}
}

and here is my view:

@()

@helper.form(action = routes.HomeController.upload, 'enctype -> "multipart/form-data") {

<input type="file" name="picture">

<p>
<input type="submit">
</p>

}

Whenever I submit this form I got "File uploaded" message.

Regards,
Sergey Dashko

среда, 9 ноября 2016 г., 5:31:22 UTC+2 пользователь Sergey Dashko написал:

Christian Schmitt

unread,
Nov 9, 2016, 3:09:15 AM11/9/16
to Play Framework

Sergey Dashko

unread,
Nov 9, 2016, 3:21:43 AM11/9/16
to Play Framework
Oh, I see. And this issue is opened since may :(
Thank you Christian for info.
Have you managed to work around this issue?

Regards,
Sergey Dashko

среда, 9 ноября 2016 г., 10:09:15 UTC+2 пользователь Christian Schmitt написал:

Christian Schmitt

unread,
Nov 9, 2016, 4:23:40 AM11/9/16
to Play Framework
well probably you **should** work around that.
Basically you should check if the file is valid, always, that's why nobody actually fixed this Bug. Most people will actually check if the file is a valid file has a correct mime type, etc. So in this case you can just check if the file length is zero and discard it, if it is the case. However you can also read owasp: https://www.owasp.org/index.php/Unrestricted_File_Upload which has some good stuff on checking file uploads for validity.
Or maybe you can find a library that checks the validity for you, but I'm not sure if such a thing exists.

Sergey Dashko

unread,
Nov 9, 2016, 9:00:21 AM11/9/16
to Play Framework
Thanks a lot Christian. I've got your point.

Regards,
Sergey Dashko

среда, 9 ноября 2016 г., 11:23:40 UTC+2 пользователь Christian Schmitt написал:
Reply all
Reply to author
Forward
0 new messages