struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [struts] 01/01: WW-5115 Reduces logging for ignored DMI related params when DMI is disabled
Date Sat, 03 Apr 2021 15:32:53 GMT
This is an automated email from the ASF dual-hosted git repository.

lukaszlenart pushed a commit to branch WW-5115-reduce-logging
in repository https://gitbox.apache.org/repos/asf/struts.git

commit f2dcda06b481f5b544b98a194896dc48bb43b357
Author: Lukasz Lenart <lukaszlenart@apache.org>
AuthorDate: Sat Apr 3 17:32:38 2021 +0200

    WW-5115 Reduces logging for ignored DMI related params when DMI is disabled
---
 .../xwork2/interceptor/ParametersInterceptor.java  | 21 +++++++++++++++++
 .../interceptor/ParametersInterceptorTest.java     | 27 ++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java
b/core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java
index 549574f..550cce6 100644
--- a/core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java
+++ b/core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java
@@ -33,6 +33,7 @@ import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
 import org.apache.commons.lang3.BooleanUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
+import org.apache.struts2.StrutsConstants;
 import org.apache.struts2.dispatcher.HttpParameters;
 import org.apache.struts2.dispatcher.Parameter;
 
@@ -40,6 +41,7 @@ import java.util.Collection;
 import java.util.Comparator;
 import java.util.Map;
 import java.util.TreeMap;
+import java.util.regex.Pattern;
 
 /**
  * This interceptor sets all parameters on the value stack.
@@ -49,9 +51,11 @@ public class ParametersInterceptor extends MethodFilterInterceptor {
     private static final Logger LOG = LogManager.getLogger(ParametersInterceptor.class);
 
     protected static final int PARAM_NAME_MAX_LENGTH = 100;
+    private static final Pattern DMI_IGNORED_PATTERN = Pattern.compile("^(action|method):.*",
Pattern.CASE_INSENSITIVE);
 
     private int paramNameMaxLength = PARAM_NAME_MAX_LENGTH;
     private boolean devMode = false;
+    private boolean dmiEnabled = false;
 
     protected boolean ordered = false;
 
@@ -79,6 +83,11 @@ public class ParametersInterceptor extends MethodFilterInterceptor {
         this.acceptedPatterns = acceptedPatterns;
     }
 
+    @Inject(value = StrutsConstants.STRUTS_ENABLE_DYNAMIC_METHOD_INVOCATION, required = false)
+    public void setDmiEnabled(String dmiEnabled) {
+        this.dmiEnabled = Boolean.parseBoolean(dmiEnabled);
+    }
+
     /**
      * If the param name exceeds the configured maximum length it will not be
      * accepted.
@@ -285,6 +294,10 @@ public class ParametersInterceptor extends MethodFilterInterceptor {
     }
 
     protected boolean acceptableName(String name) {
+        if (isIgnoredDMI(name)) {
+            LOG.trace("DMI is enabled, ignoring DMI method: {}", name);
+            return false;
+        }
         boolean accepted = isWithinLengthLimit(name) && !isExcluded(name) &&
isAccepted(name);
         if (devMode && accepted) { // notify only when in devMode
             LOG.debug("Parameter [{}] was accepted and will be appended to action!", name);
@@ -292,6 +305,14 @@ public class ParametersInterceptor extends MethodFilterInterceptor {
         return accepted;
     }
 
+    private boolean isIgnoredDMI(String name) {
+        if (dmiEnabled) {
+            return false;
+        } else {
+            return DMI_IGNORED_PATTERN.matcher(name).matches();
+        }
+    }
+
     protected boolean isWithinLengthLimit(String name) {
         boolean matchLength = name.length() <= paramNameMaxLength;
         if (!matchLength) {
diff --git a/core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java
b/core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java
index 584563d..61cebb9 100644
--- a/core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java
@@ -715,6 +715,33 @@ public class ParametersInterceptorTest extends XWorkTestCase {
         assertEquals(expected, actual);
     }
 
+    public void testDMIMethodsAreIgnored() throws Exception {
+        // given
+        ParametersInterceptor interceptor = createParametersInterceptor();
+        final Map<String, Object> actual = injectValueStackFactory(interceptor);
+        ValueStack stack = injectValueStack(actual);
+
+        final Map<String, Object> expected = new HashMap<String, Object>() {
+            {
+                put("ordinary.bean", "value");
+            }
+        };
+
+        Map<String, Object> parameters = new HashMap<String, Object>() {
+            {
+                put("ordinary.bean", "value");
+                put("action:", "myAction");
+                put("method:", "doExecute");
+            }
+        };
+
+        // when
+        interceptor.setParameters(new NoParametersAction(), stack, HttpParameters.create(parameters).build());
+
+        // then
+        assertEquals(expected, actual);
+    }
+
     public void testBeanListSingleValue() throws Exception {
         Map<String, Object> params = new HashMap<>();
         params.put("beanList.name", new String[] { "Superman" });

Mime
View raw message