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

Fix example to only use last part of filename #8007

Merged
merged 1 commit into from
Nov 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ package scalaguide.upload.fileupload {
//#upload-file-action
def upload = Action(parse.multipartFormData) { request =>
request.body.file("picture").map { picture =>
val filename = picture.filename
val contentType = picture.contentType

// only get the last part of the filename
// otherwise someone can send a path like ../../home/foo/bar.txt to write to other files on the system
val filename = Paths.get(picture.filename).getFileName
Copy link
Contributor

@NathanC NathanC Nov 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about making it so that request.body.file("foo") does this by default internally? Are there any valid use-cases for file uploads containing actual path fragments? Path traversal vulnerabilities can be pretty messy, and a secure-by-default solution seems preferable.

(If that's not feasible because of backwards compatibility, what about adding a sanitizedFileName method to the FilePart case class, that automatically does this?)

Copy link
Member Author

@gmethvin gmethvin Nov 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PainterAndHacker I was thinking that as well. I wanted to get this documentation fix in first so we can apply it to older branches of Play, but I do think we should have a way to access a sanitized version in Play's API. I opened issue #8008 for this.

Copy link
Contributor

@shkhln shkhln Nov 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any valid use-cases for file uploads containing actual path fragments?

There is https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory.


picture.ref.moveTo(Paths.get(s"/tmp/picture/$filename"), replace = true)
Ok("File uploaded")
}.getOrElse {
Expand Down Expand Up @@ -146,4 +149,4 @@ package scalaguide.upload.fileupload {
}
}
}