I am doing a REST API in Java (although this question may apply to other languages) where I create several local files and directories, and they can be deleted based on input. I manage my files in a certain directory, then append the request subpath.
So, if user sends a request DELETE /data { path: "user23/mobile" }, then I would delete files under "/opt/my-app/user-data/user23/mobile/". However, It is easy to see that if the input was something like ".." I would start deleting files from other users, or even system files.
Is there a way to securize the path construction, and make sure the given String does not have access previous folders?
Thanks in advance.
getCanonicalPath()path is only useful for the old File API. For detecting path-traversal attacks in new nio file API you should usenormalize()see e.g. heise.de/en/background/…