Uses checker instead set of patterns to check if param is excluded
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/5ec47b1e Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/5ec47b1e Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/5ec47b1e Branch: refs/heads/feature/exclude-object-class Commit: 5ec47b1e6df6c59ff3fa466d20f28fda46b60254 Parents: 3d77c34 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Wed May 14 08:25:50 2014 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Wed May 14 08:25:50 2014 +0200 ---------------------------------------------------------------------- .../interceptor/ParametersInterceptor.java | 43 +++++++------------- .../interceptor/ParametersInterceptorTest.java | 4 +- 2 files changed, 16 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/5ec47b1e/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java ---------------------------------------------------------------------- diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java b/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java index 6de6aad..460aae2 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java @@ -17,6 +17,7 @@ package com.opensymphony.xwork2.interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.ExcludedPatternsChecker; import com.opensymphony.xwork2.ValidationAware; import com.opensymphony.xwork2.XWorkConstants; import com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler; @@ -143,12 +144,13 @@ public class ParametersInterceptor extends MethodFilterInterceptor { protected static final int PARAM_NAME_MAX_LENGTH = 100; + private ExcludedPatternsChecker excludedPatterns; + private int paramNameMaxLength = PARAM_NAME_MAX_LENGTH; private boolean devMode = false; protected boolean ordered = false; - protected Set<Pattern> excludeParams = Collections.emptySet(); protected Set<Pattern> acceptParams = Collections.emptySet(); private ValueStackFactory valueStackFactory; @@ -163,7 +165,12 @@ public class ParametersInterceptor extends MethodFilterInterceptor { devMode = "true".equalsIgnoreCase(mode); } - /** + @Inject + public void setExcludedPatterns(ExcludedPatternsChecker excludedPatterns) { + this.excludedPatterns = excludedPatterns; + } + + /** * Sets a comma-delimited list of regular expressions to match * parameters that are allowed in the parameter map (aka whitelist). * <p/> @@ -306,7 +313,7 @@ public class ParametersInterceptor extends MethodFilterInterceptor { //see WW-2761 for more details MemberAccessValueStack accessValueStack = (MemberAccessValueStack) newStack; accessValueStack.setAcceptProperties(acceptParams); - accessValueStack.setExcludeProperties(excludeParams); + accessValueStack.setExcludeProperties(excludedPatterns.getExcludedPatterns()); } for (Map.Entry<String, Object> entry : acceptableParameters.entrySet()) { @@ -426,14 +433,10 @@ public class ParametersInterceptor extends MethodFilterInterceptor { } protected boolean isExcluded(String paramName) { - if (!this.excludeParams.isEmpty()) { - for (Pattern pattern : excludeParams) { - Matcher matcher = pattern.matcher(paramName); - if (matcher.matches()) { - notifyDeveloper("Parameter [#0] is on the excludeParams list of patterns!", paramName); - return true; - } - } + ExcludedPatternsChecker.IsExcluded result = excludedPatterns.isExcluded(paramName); + if (result.isExcluded()) { + notifyDeveloper("Parameter [#0] is on the excludeParams list of patterns!", paramName); + return true; } return false; } @@ -467,29 +470,13 @@ public class ParametersInterceptor extends MethodFilterInterceptor { } /** - * Gets a set of regular expressions of parameters to remove - * from the parameter map - * - * @return A set of compiled regular expression patterns - */ - protected Set getExcludeParamsSet() { - return excludeParams; - } - - /** * Sets a comma-delimited list of regular expressions to match * parameters that should be removed from the parameter map. * * @param commaDelim A comma-delimited list of regular expressions */ public void setExcludeParams(String commaDelim) { - Collection<String> excludePatterns = ArrayUtils.asCollection(commaDelim); - if (excludePatterns != null) { - excludeParams = new HashSet<Pattern>(); - for (String pattern : excludePatterns) { - excludeParams.add(Pattern.compile(pattern, Pattern.CASE_INSENSITIVE)); - } - } + excludedPatterns.addExcludedPatterns(commaDelim); } } http://git-wip-us.apache.org/repos/asf/struts/blob/5ec47b1e/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java ---------------------------------------------------------------------- diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java b/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java index a2aa92b..156c012 100644 --- a/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java +++ b/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java @@ -145,7 +145,6 @@ public class ParametersInterceptorTest extends XWorkTestCase { }; - pi.setExcludeParams("(.*\\.|^)class\\..*"); container.inject(pi); ValueStack vs = ActionContext.getContext().getValueStack(); @@ -165,7 +164,7 @@ public class ParametersInterceptorTest extends XWorkTestCase { final String pollution2 = "model.class.classLoader.jarPath"; final String pollution3 = "class.classLoader.defaultAssertionStatus"; - loadConfigurationProviders(new XWorkConfigurationProvider(), new XmlConfigurationProvider("xwork-param-test.xml")); + loadConfigurationProviders(new XWorkConfigurationProvider(), new XmlConfigurationProvider("xwork-class-param-test.xml")); final Map<String, Object> params = new HashMap<String, Object>() { { put(pollution1, "bad"); @@ -308,7 +307,6 @@ public class ParametersInterceptorTest extends XWorkTestCase { }; - pi.setExcludeParams("(.*\\.|^|.*|\\[('|\"))class(\\.|('|\")]|\\[).*"); container.inject(pi); ValueStack vs = ActionContext.getContext().getValueStack();