I am looking to provide users of my website access to some static resources which are provided within a JAR file.
I am unable to provide a list of all files which the user should be able to access, but I can ensure that they are all contained within a subdirectory of the JAR, e.g:
public-access/file-1
public-access/file-2
public-access/sub-dir/file-3
The files will then be accessed via:
this.getClass().getResource("/public-access/" + requestedFile);
Is there a recommended way to prevent path traversal attacks? This is to prevent a malicious user from requesting a file called e.g. ../secret
, or sub-dir/../../secret