我正在针对我的模块的代码审计报告修复问题。问题是 XSS 漏洞。它报告了语法 response.getOutputStream().write(buffer); 的问题。如何解决?我做了足够的功课,发现 OWASP 推荐的 ESAPI 可以帮助我修复它,但是如何实现呢?问题出在 servlet 类中?或任何其他api或其他任何东西可以帮助我修复它?请分享您的相关经验。
FileOutputStream fos = null;
FileInputStream fileInuptStream =null;
BufferedInputStream bufferedInputStream = null;
ByteArrayOutputStream byteArrayOutputStream =null;
try{
ServletContext servletContext = request.getSession().getServletContext();
File attachmentDir = new File(servletContext.getRealPath("")+File.separator+"Reports" );
String uploadDir=attachmentDir.getPath();
if (!attachmentDir.exists()) {
attachmentDir.mkdirs();
}
HSSFWorkbook wb= new HSSFWorkbook();
AAAA aaa=new AAAA();
wb=aaa.getExportXLS(request, response, fileName, wb);
if(request.getSession().getAttribute("SESSION_AAAAA")!=null){
request.getSession().removeAttribute("SESSION_AAAAA");
}
fos=new FileOutputStream(uploadDir+File.separator+fileName);
wb.write(fos);
File fileXls=new File(uploadDir+File.separator+fileName);
fileInuptStream = new FileInputStream(fileXls);
bufferedInputStream = new BufferedInputStream(fileInuptStream);
byteArrayOutputStream = new ByteArrayOutputStream();
int start = INT_ZERO;
int length = ONE_ZERO_TWO_FOUR;
int offset = MINUS_ONE;
byte [] buffer = new byte [length];
while ((offset = bufferedInputStream.read(buffer, start, length)) != -1)
byteArrayOutputStream.write(buffer, start, offset);
buffer = byteArrayOutputStream.toByteArray();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/xls");
response.setHeader("Content-disposition","attachment; filename="+fileName );
response.setContentLength((int ) fileXls.length());
response.getOutputStream().write(buffer); --- REPORTED AT THIS LINE
response.getOutputStream().flush();