2017-07-02 59 views
0

我必須實施一個過濾器來阻止我的Liferay Portal中的XSS攻擊。我已經閱讀了很多關於它的答案,所以我使用HttpServletRequestWrapper爲我的請求添加了消毒參數。我的過濾器工作正常:調試代碼我意識到過濾器採用參數並對其進行了消毒。 我的問題是,在一個portlet的processAction中,我無法使用request.getParameter()檢索清理參數,但我總是得到舊的未清理參數。從processAction獲取消毒參數 - Liferay

例如,假設我有一個portlet用一個簡單的形式是這樣的:

enter image description here

正如你可以在輸入框中看到有一個b標籤進行消毒。當表單被提交時,我的過濾器被調用並且它拋出doFilter()方法。 我的doFilter方法遍歷所有執行衛生的參數。然後,我將它們添加在我WrappedRequest:

/* 
    * Did it make any difference? 
    */ 
    if (!Arrays.equals(processedParams, params)) { 
     logger.info("Parameter: " + params[0] + " sanitized with: " + processedParams[0]); 
     /* 
     * If so, wrap up the request with a new version that will return the trimmed version of the param 
     */ 
     HashMap<String, String[]> map = new HashMap<>(); 
     map.put(name, processedParams); 
     final HttpServletRequestWrapper newRequest = new ExtendedRequestWrapper(httpServletRequest,map);      


     /* 
     * Return the wrapped request and forward the processing instruction from 
     * the validation rule 
     */ 
     return newRequest; 

我班ExtendedRequestWrapper實現的getParameter方法:

public class ExtendedRequestWrapper extends HttpServletRequestWrapper { 

    private final Map<String, String[]> modifiableParameters; 
    private Map<String, String[]> allParameters = null; 


    public ExtendedRequestWrapper(final HttpServletRequest request, 
      final Map<String, String[]> additionalParams) 
    { 
     super(request); 
     this.modifiableParameters = new TreeMap<String, String[]>(); 
     this.modifiableParameters.putAll(additionalParams); 
    } 

    @Override 
    public String getParameter(final String name) 
    { 
     String[] strings = getParameterMap().get(name); 
     if (strings != null) 
     { 
      return strings[0]; 
     } 
     return super.getParameter(name); 
    } 

    @Override 
    public Map<String, String[]> getParameterMap() 
    { 
     if (this.allParameters == null) 
     { 
      this.allParameters = new TreeMap<String, String[]>(); 
      this.allParameters.putAll(super.getParameterMap()); 
      this.allParameters.putAll(modifiableParameters); 

     } 
     //Return an unmodifiable collection because we need to uphold the interface contract. 
     return Collections.unmodifiableMap(allParameters); 
    } 

    @Override 
    public Enumeration<String> getParameterNames() 
    { 
     return Collections.enumeration(getParameterMap().keySet()); 
    } 

    @Override 
    public String[] getParameterValues(final String name) 
    { 
     return getParameterMap().get(name); 
    } 
} 

現在,當我嘗試進入消毒PARAMS在我的processAction()我得到的舊值,一個不消毒:

@Override 
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException { 
    String azione = request.getParameter("MyXSSaction"); 
    if(azione.equals("XSSAttack")) {   
    String descr = req.getParameter("mydescr"); 
    } 
} 

我該如何解決?

回答

0

你不應該在你的輸入處理中做到這一點。首先,<b>中沒有XSS,因爲XSS中的第二個S用於'腳本' - 並且<b>不包含任何腳本。其次,這種過濾器的一般和徹底的應用將有效地防止添加適當的網頁內容,博客文章和其他內容,格式合法地完成。

三 - 假設你有一個隨機的圖書管理系統:爲什麼不應該的人能夠進入Let x < 5作爲書名,甚至<script>alert('XSS')</script> and what to do against it - 不,他們是正確的書名?事實上,他們會是正確的數據,並且你想在顯示時逃避它們。

如果要將它們顯示爲HTML,那麼可能會有一些消毒(如Liferay的AntiSamy插件所做的)某些元素。但是其他任何東西只需在輸出過程中妥善轉義即可。

另一種說法是:如果在HTML頁面中顯示時錯誤地忽略了這些參數,那麼這些參數就是危險的 - 但是如果將它們嵌入到文本/純文本電子郵件正文中,則它們完全無害。