Some refactoring of canonicalize feature, also change ServiceEventHandler to use UtilHttp stuff for parameters so this and other things take effect there

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@741478 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David E. Jones
2009-02-06 09:05:50 +00:00
parent 90741427de
commit 57dcf34bcd
2 changed files with 37 additions and 30 deletions
@@ -51,12 +51,18 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.errors.EncodingException;
import javolution.util.FastList;
import javolution.util.FastMap;
import org.owasp.esapi.Encoder;
import org.owasp.esapi.codecs.CSSCodec;
import org.owasp.esapi.codecs.Codec;
import org.owasp.esapi.codecs.HTMLEntityCodec;
import org.owasp.esapi.codecs.JavaScriptCodec;
import org.owasp.esapi.codecs.PercentCodec;
import org.owasp.esapi.errors.EncodingException;
import org.owasp.esapi.reference.DefaultEncoder;
/**
* HttpUtil - Misc HTTP Utility Functions
*/
@@ -66,7 +72,13 @@ public class UtilHttp {
/** OWASP ESAPI canonicalize strict flag; setting false so we only get warnings about double encoding, etc; can be set to true for exceptions and more security */
public static final boolean esapiCanonicalizeStrict = false;
public static final Encoder defaultWebEncoder;
static {
// possible codecs: CSSCodec, HTMLEntityCodec, JavaScriptCodec, MySQLCodec, OracleCodec, PercentCodec, UnixCodec, VBScriptCodec, WindowsCodec
List<Codec> codecList = Arrays.asList(new CSSCodec(), new HTMLEntityCodec(), new JavaScriptCodec(), new PercentCodec());
defaultWebEncoder = new DefaultEncoder( codecList );
}
public static final String MULTI_ROW_DELIMITER = "_o_";
public static final String ROW_SUBMIT_PREFIX = "_rowSubmit_o_";
public static final String COMPOSITE_DELIMITER = "_c_";
@@ -237,29 +249,28 @@ public class UtilHttp {
public static Map<String, Object> canonicalizeParameterMap(Map<String, Object> paramMap) {
for (Map.Entry<String, Object> paramEntry: paramMap.entrySet()) {
if (paramEntry.getValue() instanceof String) {
try {
String cannedStr = ESAPI.encoder().canonicalize((String) paramEntry.getValue(), esapiCanonicalizeStrict);
paramEntry.setValue(cannedStr);
} catch (EncodingException e) {
Debug.logError(e, "Error in canonicalize parameter with name [" + paramEntry.getKey() + "], value [" + paramEntry.getValue() + "]: " + e.toString(), module);
}
paramEntry.setValue(canonicalizeParameter((String) paramEntry.getValue()));
} else if (paramEntry.getValue() instanceof Collection) {
List<String> newList = FastList.newInstance();
for (String listEntry: ((Collection<String>) paramEntry.getValue())) {
try {
String cannedStr = ESAPI.encoder().canonicalize(listEntry, esapiCanonicalizeStrict);
newList.add(cannedStr);
} catch (EncodingException e) {
// add the original value and log the error, a soft fail for now
newList.add(listEntry);
Debug.logError(e, "Error in canonicalize parameter with name [" + paramEntry.getKey() + "], value [" + listEntry + "]: " + e.toString(), module);
}
newList.add(canonicalizeParameter((String) paramEntry.getValue()));
}
paramEntry.setValue(newList);
}
}
return paramMap;
}
public static String canonicalizeParameter(String paramValue) {
try {
String cannedStr = defaultWebEncoder.canonicalize(paramValue, esapiCanonicalizeStrict);
if (Debug.verboseOn()) Debug.logVerbose("Canonicalized parameter with " + (cannedStr.equals(paramValue) ? "no " : "") + "change: original [" + paramValue + "] canned [" + cannedStr + "]", module);
return cannedStr;
} catch (EncodingException e) {
Debug.logError(e, "Error in canonicalize parameter value [" + paramValue + "]: " + e.toString(), module);
return paramValue;
}
}
/**
* Create a map from a HttpRequest (attributes) object
@@ -18,15 +18,17 @@
*******************************************************************************/
package org.ofbiz.webapp.event;
import static org.ofbiz.base.util.UtilGenerics.checkList;
import java.io.File;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -39,9 +41,7 @@ import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.ofbiz.base.util.Debug;
import static org.ofbiz.base.util.UtilGenerics.checkList;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
@@ -226,6 +226,8 @@ public class ServiceEventHandler implements EventHandler {
// store the multi-part map as an attribute so we can access the parameters
request.setAttribute("multiPartMap", multiPartMap);
Map<String, Object> rawParametersMap = UtilHttp.getParameterMap(request, null, null);
// we have a service and the model; build the context
Map<String, Object> serviceContext = FastMap.newInstance();
for (ModelParam modelParam: model.getInModelParamList()) {
@@ -260,15 +262,9 @@ public class ServiceEventHandler implements EventHandler {
// check the request parameters
if (UtilValidate.isEmpty(value)) {
// normal parameter data, which can either be a single value or an array of values
String[] paramArr = request.getParameterValues(name);
if (paramArr != null) {
if (paramArr.length > 1) {
value = Arrays.asList(paramArr);
} else {
value = paramArr[0];
}
}
// use the rawParametersMap from UtilHttp in order to also get pathInfo parameters, do canonicalization, etc
value = rawParametersMap.get(name);
// make any composite parameter data (e.g., from a set of parameters {name_c_date, name_c_hour, name_c_minutes})
if (value == null) {
value = UtilHttp.makeParamValueFromComposite(request, name, locale);