mirror of
https://github.com/apache/struts.git
synced 2026-08-07 15:46:57 +00:00
Adds better way to handle JavaScript injection into request parameters
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1302803 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -27,6 +27,7 @@ import com.opensymphony.xwork2.util.TextParseUtil;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.logging.Logger;
|
||||
import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
@@ -176,8 +177,8 @@ public class UrlHelper {
|
||||
|
||||
String result = link.toString();
|
||||
|
||||
while (result.indexOf("<script>") > 0){
|
||||
result = result.replaceAll("<script>", "script");
|
||||
if (StringUtils.containsIgnoreCase(result, "<script")){
|
||||
result = StringEscapeUtils.escapeEcmaScript(result);
|
||||
}
|
||||
try {
|
||||
result = encodeResult ? response.encodeURL(result) : result;
|
||||
|
||||
@@ -21,26 +21,21 @@
|
||||
|
||||
package org.apache.struts2.views.util;
|
||||
|
||||
import com.mockobjects.dynamic.C;
|
||||
import com.mockobjects.dynamic.Mock;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Scope.Strategy;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.StrutsTestCase;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.StrutsTestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Test case for UrlHelper.
|
||||
@@ -116,6 +111,22 @@ public class UrlHelperTest extends StrutsTestCase {
|
||||
expectedUrl, url.toString());
|
||||
}
|
||||
|
||||
public void testBuildParametersStringWithJavaScriptInjected() throws Exception {
|
||||
String expectedUrl = "http://localhost:8080/myContext/myPage.jsp?initParam=initValue&param1=value1&param2=value2&param3%22%3Cscript+type%3D%22text%2Fjavascript%22%3Ealert%281%29%3B%3C%2Fscript%3E=value3";
|
||||
|
||||
Map parameters = new LinkedHashMap();
|
||||
parameters.put("param1", "value1");
|
||||
parameters.put("param2", "value2");
|
||||
parameters.put("param3\"<script type=\"text/javascript\">alert(1);</script>","value3");
|
||||
|
||||
StringBuilder url = new StringBuilder("http://localhost:8080/myContext/myPage.jsp?initParam=initValue");
|
||||
|
||||
UrlHelper.buildParametersString(parameters, url);
|
||||
|
||||
assertEquals(
|
||||
expectedUrl, url.toString());
|
||||
}
|
||||
|
||||
public void testForceAddNullSchemeHostAndPort() throws Exception {
|
||||
String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user