gpt4 book ai didi

java - response.getOutputStream().write(buffer) 的 Servlet 类中的 XSS 漏洞问题

转载 作者:行者123 更新时间:2023-11-30 09:36:01 25 4
gpt4 key购买 nike

我正在针对我的模块的代码审计报告修复问题。问题是 XSS 漏洞。它报告了语法问题response.getOutputStream().write(缓冲区);如何解决?功课做足了,发现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();

最佳答案

这是一个误报。此 servlet 返回一个由 Apache POI 创建的 XLS 文件,而不是 HTML 文档。不可能有 XSS 攻击的手段。

然而,这段代码相当笨拙且效率低下。它在扩展的 WAR 文件夹中创建一个文件(当重新部署 WAR 时该文件无论如何都会丢失),然后将其全部内容完全复制到服务器的内存中,而不是直接写入响应。可能这种笨拙的方法混淆了审计工具。您应该只是将 HttpServletResponse#getOutputStream() 传递给 Workbook#write()

这是基于目前发布的代码的完整重写:

HSSFWorkbook wb = new HSSFWorkbook();
AAAA aaa = new AAAA();
wb = aaa.getExportXLS(request, response, fileName, wb);
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);
wb.write(response.getOutputStream());

关于java - response.getOutputStream().write(buffer) 的 Servlet 类中的 XSS 漏洞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10997331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com