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機制,或使用攔截器技術驗證請求的合法性。

也或者爲每一個表單生成一個隨機數,將該隨機數放置到表單中,並在收到表單後即驗證該隨機數。

相關文章
相關標籤/搜索