Java编程安全漏洞之:不信任用户可控数据

Trust_Boundary_Violation

违反信任边界,用户可控的数据被缓存到可信容器中。java

修复建议

若无必要或可下降可信度存放,最好不要把非可信数据缓存到可信容器中。在存放以前对数据合法性进行校验。浏览器

只有HttpSession和HttpServletContext被认为是可信容器。缓存

Unchecked_Input_for_Loop_Condition

可输入的循环条件未做检查,用户可控的数据被做为循环次数的判断依据。服务器

修复建议

不使用用户可控的数据做为循环判断的依据或给定最大循环次数。dom

修复示例

如:jsp

       public void Unchecked_Input_for_Loop_Condition(HttpServletRequest request){oop

              String text = request.getParameter("text");性能

              List list = service.getList(text);编码

              int len = list.size();url

              for(int i = 0; i < len; i++){

                     System.out.println(i);

              }

       }

修复为:修复后暂时会被报出

       public void Unchecked_Input_for_Loop_Condition_Fix(HttpServletRequest request){

              String text = request.getParameter("text");

              List list = service.getList(text);

              int len = list.size();

              int maxTimes = 2000;

              for(int i = 0; len < maxTimes && i < len; i++){

                     System.out.println(i);

              }

       }

Unnormalize_Input_String

未标准化的输入字符串,指外来的字符串型数据未作标准化处理。

此标准化处理是将字符串的实际存储转换为Unicode编码,主要做用是提升字符串查询搜索的性能。

修复建议

进行字符串标准化处理。

修复示例

如:

       public void Unnormalize_Input_String(HttpServletRequest request){

              String text = request.getParameter("text");

              File z = new File(text);

       }

修复为:

       public void Unnormalize_Input_String_Fix(HttpServletRequest request){

              String text = request.getParameter("text");

              //jdk1.6

              text = java.text.Normalizer.normalize(text, java.text.Normalizer.Form.NFKD);

              //java.text.Normalizer.Form.NFD,NFC,NFKD,NFKC

              //

              File z = new File(text);

       }

Cross_Site_History_Manipulation

跨站历史操纵,跳转时使用了固定的url,可能致使浏览器会加载部分可能已经被篡改的本地数据。相同url浏览器会先加载本地缓存页面。

修复建议

跳转时,在url中追加随机数。

修复示例

如:

       public void Cross_Site_History_Manipulation(HttpServletRequest request, HttpServletResponse response){

              String text = request.getParameter("text");

              if("t".equals(text)){

                     response.sendRedirect(request.getContextPath() + "/page.jsp");

              }else{

                     response.sendRedirect(request.getContextPath() + "/error.jsp");

              }

             

       }

修复为:

       public void Cross_Site_History_Manipulation_Fix(HttpServletRequest request, HttpServletResponse response){

             

              String text = request.getParameter("text");

              if("t".equals(text)){

                     response.sendRedirect(request.getContextPath() + "/page.jsp" + "?"

 + Math.random());

              }else{

                     response.sendRedirect(request.getContextPath() + "/error.jsp" + "?"

 + Math.random());

              }

       }

HTTP_Response_Splitting

HTTP响应折断,在操做响应报头时加入\r或\n能够伪造新的HTTP响应报头,从而覆盖原有的响应。原有响应会被解释为两个响应返回给客户端,攻击者有可能会获取第二个响应的彻底控制权。

正常响应:

HTTP/1.1 200 OK

...

Set-Cookie: author=Jane Smith

用户提交了恶意脚本:author = Wiley Hacker\r\nHTTP/1.1 200 OK\r\n

异常响应:

HTTP/1.1 200 OK

...

Set-Cookie: author=Wiley Hacker HTTP/1.1 200 OK

修复建议

在操做HTTP报头(即Head部分)时,全部写入该区域的值必须去除\r和\n字符。

修复示例

如:

       public void HTTP_Response_Splitting(HttpServletRequest request, HttpServletResponse response){

              String text = request.getParameter("text");

              response.setHeader("text", text);

       }

修复为:

       public void HTTP_Response_Splitting_Fix(HttpServletRequest request, HttpServletResponse response){

              String text = request.getParameter("text");

              text = text.replace("\n", "");

              text = text.replace("\r", "");

              response.setHeader("text", text);

       }

Absolute_Path_Traversal

绝对路径遍历,指使用用户输入的数据做为全路径,直接应用于文件操做。

修复建议

将用户输入的数据只做为文件操做的部分路径,根路径由程序控制。

修复示例

如:

       public void Absolute_Path_Traversal(HttpServletRequest request){

              String text = request.getParameter("text");

              File z = new File(text);

       }

修复为:

       public void Absolute_Path_Traversal_Fix(HttpServletRequest request, String basePath){

              String text = request.getParameter("text");

              File z = new File(basePath + text);

       }

XSRF

跨站请求伪造,指在使用GET方式处理请求时,未做请求合法性校验。

修复建议

使用Token技术或在应用服务器中禁用GET请求,Struts自己有一套完善的防止表单重复提交的token机制,或使用拦截器技术验证请求的合法性。

也或者为每一个表单生成一个随机数,将该随机数放置到表单中,并在收到表单后即验证该随机数。

相关文章
相关标签/搜索