下面的代码很容易受到跨站点脚本攻击,该攻击已由veracode扫描报告:
public void doPost(HttpServletRequest request,HttpServletResponse response)
{
byte[] inputBytes = request.getParameter("input").getBytes();
String name = request.getParameter("filename") == null?"excelReport":request.getParameter("filename").toString();
response.setContentType("application/vnd.ms-excel");
response.setContentLength(inputBytes.length);
response.addHeader("content-disposition", "attachment; filename="+name+".xls");
try {
response.getOutputStream().write(inputBytes, 0, inputBytes.length);
} catch (IOException e) {
e.printStackTrace();
}
}
我了解代码行-response.getOutputStream().write(inputBytes, 0, inputBytes.length);
容易受到XSS攻击。
如何在此修复XSS(跨站点脚本)漏洞?是否可以在byte []数组上使用任何ESAPI库来修复XSS缺陷?
答案 0 :(得分:0)
对于防止XSS的良好的纵深防御方法,需要做两件事。
您的代码存在多个安全问题,而不仅仅是XSS。但是要结束您的主要问题,OWASP的ESAPI有一个编码器,还有ESAPI编码器项目。正确的方法是将编码器移交给解释器时尽量靠近编码器。由于使用的是JSP,因此需要对显示给用户的所有输出进行编码,以确保其安全。
您的其他问题:
public void doPost(HttpServletRequest request,HttpServletResponse response)
{
//Did we get the correct file?
byte[] inputBytes = request.getParameter("input").getBytes();
//Validation against legal characters in the filename?
String name = request.getParameter("filename") == null?"excelReport":request.getParameter("filename").toString();
response.setContentType("application/vnd.ms-excel");
response.setContentLength(inputBytes.length);
//This is the line that's immediately triggering your problem. The attacker controls that filename and you've done nothing to check it!
//If you encoded here you MIGHT be fine!
response.addHeader("content-disposition", "attachment; filename="+name+".xls");
try {
response.getOutputStream().write(inputBytes, 0, inputBytes.length);
} catch (IOException e) {
e.printStackTrace();
}
}
如果您想了解此漏洞,请使用OWASP的ZAP之类的工具,并将filename参数修改为<script>alert(1);</script>
之类的值,然后您就会发现该漏洞在起作用。
对于文件本身,ESAPI仅提供检查文件扩展名白名单的方法,虽然不是很好,但是实际上很难正确地进行文件验证。因此,没有,没有方法可以验证您的文件字节。