我必須實施一個過濾器來阻止我的Liferay Portal中的XSS攻擊。我已經閱讀了很多關於它的答案,所以我使用HttpServletRequestWrapper爲我的請求添加了消毒參數。我的過濾器工作正常:調試代碼我意識到過濾器採用參數並對其進行了消毒。 我的問題是,在一個portlet的processAction中,我無法使用request.getParameter()檢索清理參數,但我總是得到舊的未清理參數。從processAction獲取消毒參數 - Liferay
例如,假設我有一個portlet用一個簡單的形式是這樣的:
正如你可以在輸入框中看到有一個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");
}
}
我該如何解決?