Directory Traversal
1. Введение:
2. Типичный уязвимый код:
private static final String BASE_PATH = "/storage/items/images";
private void getProfileImage(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String folderName = request.getParameter("folder");
String fileName = request.getParameter("file");
String path = BASE_PATH + folderName + fileName;
File file = new File(path);
buildResponse(response, file);
}
private void constructResponse(HttpServletResponse response,
File file) throws IOException {
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
// Notice there is no extra validation on the path of the file, it is read
// straight away.
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int read;
while ((read = bis.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
bis.close();
os.flush();
os.close();
}3. Смягчение последствий:
3.1. Проверка абсолютного пути:
3.2. Использование службы хостинга файлов:
File Upload3.3. Косвенные ссылки на файлы:
4. Выводы:
Last updated