GetMapping("/download")
public String download(String filename, HttpServletRequest request, HttpServletResponse response) {
String filePath = System.getProperty("user.dir") + "/logs/" + filename;
try {
File file = new File(filePath);
InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[is.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
return "下载文件成功:" + filePath;
public static boolean check_traversal(String content) {
return content.contains("..") || content.contains("/");
}
-
对文件名做映射生成id值,通过参数化下载文件可以有效防止遍历问题 -
需要注意:控制用户权限,避免通过遍历下载文件(越权)
-
文件目录避免外部参数拼接。 -
保存文件目录建议后台写死并对文件名进行校验(字符类型、长度)。 -
建议文件保存时,将文件名替换为随机字符串。 -
如因业务需要不能满足1.2.3的要求,文件路径、文件命中拼接了不可行数据,需判断请求文件名和文件路径参数中是否存在../或..(windows), 如存在应判定路径非法并拒绝请求。
https://github.com/j3ers3/Hello-Java-Sec
原文始发于微信公众号(Reset安全):Java代码审计:路径穿越
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论