servletoutputstream.write的xss漏洞

时间:2012-06-19 07:12:59

标签: java xss

    String id = request.getParameter("id") != null ? request.getParameter("id") : "0";
            aaaa doc = bbb.getdetailsById(id);    
            byte b[] = doc.getUploaded();        
            try {
                response.setContentType("APPLICATION/OCTET-STREAM");
                String disHeader = "Attachment;Filename=" + doc.getName();
                response.setHeader("Content-Disposition", disHeader);
                servletoutputstream = response.getOutputStream();
                servletoutputstream.write(b, 0, b.length);
}

我有这段代码。代码审计工具说servletoutputstream.write(b,0,b.length);是xss易受攻击的。但我不知道它是如何报告相同的。以及如何解决它。我正在使用ESAPI来验证输入并在其他xss易受攻击的报告问题中转义输出。我是否也需要对这些做同样的事情?请提出建议或解决方案。 在做了一些研究工作之后,我发现使用ESAPI需要为htmlESCAPE或xmlESCAPE转义字节b []。它会解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

如果getUploaded()返回由黑客<script>alert('hi')</script>上传的某些javascript代码,则可能会产生问题。

您可以尝试以下解决方案来格式化Spring框架附带的字符串。

HtmlUtils.htmlEscape("<script> alter(''hi)</script>")

<强>输出:

&lt;script&gt; alter(''hi)&lt;/script&gt

您也可以使用JSTL库来格式化包含javascript

的字符串
public static byte[] getFormatedString(byte[] string){

    String str=new String(string);
    str=HtmlUtils.htmlEscape(str);
    return str.getBytes();

}

您的密码:

String id = request.getParameter("id") != null ? request.getParameter("id") : "0";
    aaaa doc = bbb.getdetailsById(id);    
    byte b[] = doc.getUploaded();        
    try {
        response.setContentType("APPLICATION/OCTET-STREAM");
        String disHeader = "Attachment;Filename=" + doc.getName();
        response.setHeader("Content-Disposition", disHeader);
        servletoutputstream = response.getOutputStream();
        servletoutputstream.write(getFormatedString(b), 0, b.length);

答案 1 :(得分:1)

例如,使用ESAPI验证输入'id'。 使用ESAPI验证FILE DOWNLOAD INJECTION的fileName。还使用ESapi使用getVAlidatedFileContent()验证字节b []。

这是STORED XSS VULNERABILITY ISSUE的案例。

答案 2 :(得分:0)

如果您使用Spring MVC,则有一个功能,启用如下:

<context-param>
    <param-name>defaultHtmlEscape</param-name>
    <param-value>true</param-value>
</context-param>

here is the clue

2nd clue

相关问题