2014-02-22 31 views
2
  • 問題:Spring的數據綁定autoGrowCollectionLimit不能正常工作

    Spring的數據綁定允許您設置最大尺寸()的自動創建List<>例如至3項。繞過這個限制是很容易的,只需通過修改發送給服務器的HTTP內容,Spring就可以創建一個包含3000多個項目的列表。

    換句話說:在測試我的web應用程序,我能夠通過創建惡意的HTTP請求強制Spring的數據綁定創建一個列表<>由4000項,雖然我已經將限制設置爲3項。這可能很容易導致任何應用程序服務器上的內存不足異常。

  • 問:

    我失去了一些東西明顯如何預防春季從這樣或者更確切地說,它是要報春的bug跟蹤系統中的錯誤?

  • 版使用:

    彈簧工具套件-3.3.0.RELEASE,
    d:\平方米\回購\有機\ springframework的\彈簧的web \ 3.2.4.RELEASE \彈簧幅-3.2.4.RELEASE.jar

  • 描述:

    我需要多個HTML元素<input />綁定到單個List<String>對象,像:

    <input type="text" name="phoneNumber[0]" /> 
    ... 
    <input type="text" name="phoneNumber[n]" /> 
    

    Spring默認使用org.springframework.beans.propertyeditors.CustomCollectionEditor執行此類轉換。下面是一個簡單的代碼片段,其中介紹了上述問題。

  • 代碼:

    • 支持bean:

    public class ContactDataEntity { 
        private List<String> phoneNumber; 
        // getters and setters 
    } 
    
    • 粘合劑在控制器:

    @RequestMapping(value = VIEW_PAGE_1, method = RequestMethod.POST) 
    public String xxx(HttpServletRequest request, Model model) { 
    
        // set and bind 
        ContactDataEntity contactData = new ContactDataEntity(); 
        ServletRequestDataBinder binder = new ServletRequestDataBinder(contactData); 
        binder.setAutoGrowCollectionLimit(3); // set limit to 3 items 
        binder.bind(request); 
    
        // test binding results 
        List<String> numbers = contactData.getPhoneNumber(); 
        if (numbers != null) { 
         System.out.print("numbers SIZE: " + numbers.size() + ", DATA: "); 
         for (String s : numbers) System.out.print(s + ", "); 
         System.out.print("\n"); 
        } 
    
        // validate and return view name... 
    
    } 
    
  • 結果正確的數據(< = 3名的項目,一切工作正常,我使用Firefox的活HTTP頭):

    (對不起,圖片鏈接,但「至少需要10聲譽發表圖片」和‘你至少需要10聲譽後2個以上環節。’)

    http://i.stack.imgur.com/AEjsA.jpg

  • 結果的項目太多(> 3項,一切工作正常,500內部服務器E發生了錯誤):

    i.stack.imgur.com/MfWYy。JPG

  • 簡單的一招(> 3項,沒有報告錯誤,對不起,我在 '覆蓋' 錯字):

    i.stack.imgur.com/FNlXE.jpg

  • 讓我們開拓以上:

    i.stack.imgur.com/XPIhc.jpg

所以,我的問題再次:我是缺少明顯的東西如何預防春季從這樣或拉特呃它應該被報告給Spring的bug跟蹤器?

//編輯:
我報告說,它作爲一個bug:https://jira.springsource.org/browse/SPR-11472

回答

2

好吧,直到在Spring 3.2.9和4.0.3官方補丁發佈,我已經覆蓋Spring的默認CustomCollectionEditor來修復這個bug暫時。

唯一的缺點是,你不能在你的HTML代碼/ HTTP申請使用:

&phoneNumber=0 
&phoneNumber=1 
&phoneNumber=2 
&phoneNumber=3 

,但你更需要指數的每個參數明確:

&phoneNumber[0]=0 
&phoneNumber[1]=1 
&phoneNumber[2]=2 
&phoneNumber[3]=3 

參數,而不現在簡單地忽略[]結尾,請參閱下面的代碼中的註釋。

package xxx; 

import java.util.List; 

import org.springframework.beans.propertyeditors.CustomCollectionEditor; 

/** 
* @see <a href="https://jira.springsource.org/browse/SPR-11472">https://jira.springsource.org/browse/SPR-11472</a> 
*/ 
public class CustomListEditorSPR11472 extends CustomCollectionEditor { 

    @SuppressWarnings("rawtypes") 
    public CustomListEditorSPR11472(Class<List> collectionType) { 
     super(collectionType); 
    } 

    @Override 
    public void setValue(Object value) { 
     /* 
     * Force Spring to ignore all HTTP request **MULTIPLE** parameters without "[]" on the end so that 
     * binder.setAutoGrowCollectionLimit() could work correctly. Example: 
     * 
     * phoneNumber[2]=2 
     * Above request is OK, a List containing: 'null, null, 2' is created. 
     * 
     * phoneNumber=2 
     * Above request is OK, **SINGLE** parameter without "[]", a List containing: '2' is created 
     * 
     * phoneNumber[0]=0&phoneNumber=1&phoneNumber=2 
     * **MULTIPLE** parameters without "[]" are ignored, a List containing: '0' is created. 
     */ 

     if ((value != null && value.getClass().isArray()) == false) { 
      super.setValue(value); 
     } 
    } 

} 

當然,你還需要在你的粘結劑註冊您CustomEditor:

binder.registerCustomEditor(List.class, new CustomListEditorSPR11472(List.class)); 

或更細粒度的版本單個屬性:

binder.registerCustomEditor(List.class, "phoneNumber", new CustomListEditorSPR11472(List.class));