So I have been given the task of fixing a path traversal problem in a basic Java web app, but I am quite stuck. We are meant to essentially make sure the code is secure, while maintaining functionality (which is the part i am struggling with)
So far I have looked online on how to fix the problems i am receiving, and i managed to fix them, but the bot that tests the code returns with a message saying the application no longer has functionality, but is secure.
The 2 errors I receive are the following:
1) PATH_TRAVERSAL_IN in FileDownload. java Source File FileDownload. java Class Name chatapp. FileDownload Method Name doGet Source Line 31
2) PT_RELATIVE_PATH_TRAVERSAL in FileDownload. java Source File FileDownload. java Class Name chatapp. FileDownload Method Name doGet Source Line 28
For reference this code is the original where it functions but it is not secure.
private String DOWNLOAD_PATH = new File(".").getCanonicalPath() +
"/webapps/webapp/app/download";
public FileDownload() throws IOException {
}
public void init() throws ServletException {
//To Do
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
!!!String file = request.getParameter("file");
String downloadPath = DOWNLOAD_PATH + "/" + file;
!!!File downloadFile = new File(FilenameUtils.getName(downloadPath));
if (downloadFile.exists()) {
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename="+ downloadFile.getName());
FileInputStream fis = new FileInputStream(downloadFile);
byte[] data = new byte[(int) downloadFile.length()];
fis.read(data);
fis.close();
OutputStream out = response.getOutputStream();
out.write(data);
out.flush();
}
else
response.sendError(404);
}
Does anyone have experience in fixing these sorts of problems? I am sort of confused