Added Filepath.getSanitizedFilename, deprecated getFilename #12183
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist
Helpful things
Fixes
Fixes #8008
Purpose
Provides a new way of getting sanitized filepaths from a
FilePart
, via thegetSanitizedFilename
method, as discussed in #8007 and #8008. Also deprecates the oldgetFilename
method that returns the raw path.Background Context
In #8000, it was found that Play used a raw filepath in an insecure way. This particular case was fixed in #8007, but @gmethvin suggested a reusable method should be added for doing this, instead of just once-off fixes.
This change uses the builtin Java functionality in
Path.normalize
to do most of the processing, as suggested by @wsargent. It then usesgetFileName
to return the last part of the path.Also checks for empty filepaths and unresolvable
..
paths and throws a runtime error - there is no good way to sanitize these into a valid filepath.Tests have also been written in
SanitizedFilenameTest.java
to check that it works correctly.References
#8007 and #8008