This is an automated email from the ASF dual-hosted git repository.

lukaszlenart pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/struts.git


The following commit(s) were added to refs/heads/main by this push:
     new a643cc523 WW-5821 Handle an unknown HTTP method instead of raising 
IllegalArgumentException (#2043)
a643cc523 is described below

commit a643cc52323f5ffe350ed517e6a4367f138a3846
Author: Mundur <[email protected]>
AuthorDate: Sun Nov 22 16:59:32 2026 +0800

    WW-5821 Handle an unknown HTTP method instead of raising 
IllegalArgumentException (#2043)
    
    * WW-5821 Handle an unknown HTTP method instead of raising 
IllegalArgumentException
    
    HttpMethodInterceptor calls HttpMethod.parse() with the request method for
    any action implementing HttpMethodAware. HttpMethod is an enum whose parse()
    is valueOf(method.toUpperCase()) with no fallback, and the interceptor has 
no
    try/catch, so any method outside its nine constants - PROPFIND, MKCOL,
    REPORT, or any invented token - raises IllegalArgumentException before the
    allow-checks run and reaches the client as a 500.
    
    Add HttpMethod.parseOrNull(), a total function returning null for an
    unrecognised method, and use it in the interceptor, passing null to 
HttpMethodAware
    so the request falls through to the existing isAllowed() check. With
    http-method configured that answers 405 with an Allow header, which is the
    response the code already produces for a disallowed method.
    
    Both parse() and parseOrNull() also use toUpperCase(Locale.ROOT); the bare
    call is locale-sensitive, and XmlDocConfigurationProvider already uses
    Locale.ROOT for the same lookup.
    
    Tests added to HttpMethodTest.
    
    * WW-5821 Address review: pass null to HttpMethodAware, add interceptor 
test, unify parsing
---
 .../providers/XmlDocConfigurationProvider.java     |  3 +-
 .../struts2/interceptor/httpmethod/HttpMethod.java | 15 ++++++-
 .../interceptor/httpmethod/HttpMethodAware.java    |  3 +-
 .../httpmethod/HttpMethodInterceptor.java          |  7 ++--
 .../httpmethod/HttpMethodInterceptorTest.java      | 14 +++++++
 .../interceptor/httpmethod/HttpMethodTest.java     | 48 ++++++++++++++++++++++
 6 files changed, 83 insertions(+), 7 deletions(-)

diff --git 
a/core/src/main/java/org/apache/struts2/config/providers/XmlDocConfigurationProvider.java
 
b/core/src/main/java/org/apache/struts2/config/providers/XmlDocConfigurationProvider.java
index c07dedc8e..d4ddd6a40 100644
--- 
a/core/src/main/java/org/apache/struts2/config/providers/XmlDocConfigurationProvider.java
+++ 
b/core/src/main/java/org/apache/struts2/config/providers/XmlDocConfigurationProvider.java
@@ -65,7 +65,6 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.function.Consumer;
@@ -527,7 +526,7 @@ public abstract class XmlDocConfigurationProvider 
implements ConfigurationProvid
         Set<HttpMethod> httpMethods = EnumSet.noneOf(HttpMethod.class);
         for (String value : 
commaDelimitedStringToSet(actionElement.getAttribute("http-method"))) {
             try {
-                
httpMethods.add(HttpMethod.valueOf(value.toUpperCase(Locale.ROOT)));
+                httpMethods.add(HttpMethod.parse(value));
             } catch (IllegalArgumentException e) {
                 throw new ConfigurationException(format("Invalid http-method 
[%s] on action [%s], allowed values: %s",
                         value, actionElement.getAttribute("name"), 
Arrays.toString(HttpMethod.values())), e, location);
diff --git 
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java 
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
index 8e9b85099..9cc018d3a 100644
--- 
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
+++ 
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
@@ -18,6 +18,9 @@
  */
 package org.apache.struts2.interceptor.httpmethod;
 
+import java.util.Arrays;
+import java.util.Locale;
+
 /**
  * Enum represents possible http request types
  *
@@ -36,8 +39,18 @@ public enum HttpMethod {
     CONNECT,
     PATCH;
 
+    /**
+     * @return the matching method, or {@code null} when the request method is 
not one of the constants
+     */
+    public static HttpMethod parseOrNull(String httpRequestMethod) {
+        return Arrays.stream(values())
+            .filter(method -> 
method.name().equalsIgnoreCase(httpRequestMethod))
+            .findFirst()
+            .orElse(null);
+    }
+
     public static HttpMethod parse(String httpRequestMethod) {
-        return valueOf(httpRequestMethod.toUpperCase());
+        return valueOf(httpRequestMethod.toUpperCase(Locale.ROOT));
     }
 
 }
diff --git 
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
 
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
index 642f40267..89df3937f 100644
--- 
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
+++ 
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
@@ -33,7 +33,8 @@ public interface HttpMethodAware {
     /**
      * Notifies action about http method used to perform request
      *
-     * @param httpMethod {@link 
jakarta.servlet.http.HttpServletRequest#getMethod()} translated to enum
+     * @param httpMethod {@link 
jakarta.servlet.http.HttpServletRequest#getMethod()} translated to enum, or 
{@code null} when the request method
+     *                   is not one of the {@link HttpMethod} constants
      */
     void setMethod(HttpMethod httpMethod);
 
diff --git 
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
 
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
index 54b2988e3..5498e6729 100644
--- 
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
+++ 
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
@@ -100,9 +100,10 @@ public class HttpMethodInterceptor extends 
AbstractInterceptor {
         HttpServletRequest request = 
invocation.getInvocationContext().getServletRequest();
 
         if (action instanceof HttpMethodAware) {
-            LOG.debug("Action: {} implements: {}, setting request method: {}",
-                action, HttpMethodAware.class.getSimpleName(), 
request.getMethod());
-            ((HttpMethodAware) 
(action)).setMethod(HttpMethod.parse(request.getMethod()));
+            HttpMethod requestedHttpMethod = 
HttpMethod.parseOrNull(request.getMethod());
+            LOG.debug("Action: {} implements: {}, setting request method: {} 
(parsed as: {})",
+                action, HttpMethodAware.class.getSimpleName(), 
request.getMethod(), requestedHttpMethod);
+            ((HttpMethodAware) (action)).setMethod(requestedHttpMethod);
         }
 
         Method method = resolveInvokedMethod(invocation);
diff --git 
a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
 
b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
index f22651116..7cea5e708 100644
--- 
a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
+++ 
b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
@@ -39,6 +39,7 @@ import org.springframework.mock.web.MockHttpServletResponse;
 import java.util.List;
 import java.util.Map;
 
+import static org.junit.Assert.assertNull;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
@@ -471,6 +472,19 @@ public class HttpMethodInterceptorTest extends 
StrutsInternalTestCase {
         assertEquals("GET", response.getHeader("Allow"));
     }
 
+    public void testHttpMethodAwareActionWithUnknownRequestMethod() throws 
Exception {
+        HttpMethodsTestAction action = new HttpMethodsTestAction();
+        prepareActionInvocation(action, actionConfig());
+        MockHttpServletResponse response = prepareRequest("PROPFIND");
+
+        String resultName = interceptor.intercept(invocation);
+
+        assertEquals(Action.NONE, resultName);
+        assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, 
response.getStatus());
+        assertEquals("POST", response.getHeader("Allow"));
+        assertNull(action.getHttpMethod());
+    }
+
     public void testHttpMethodAttributeRejectsWith405AndAllowHeader() throws 
Exception {
         MockHttpServletResponse response = new MockHttpServletResponse();
         ActionProxy proxy = proxyFor("", "PostOnly", "GET", response);
diff --git 
a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java
 
b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java
index c56ef5f7a..6593523dc 100644
--- 
a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java
+++ 
b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java
@@ -22,6 +22,8 @@ import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 
 public class HttpMethodTest {
 
@@ -55,4 +57,50 @@ public class HttpMethodTest {
         assertEquals(expected.getClass(), IllegalArgumentException.class);
     }
 
+
+    @Test
+    public void shouldParseUnknownRequestMethod_asNull_notRaise() {
+        // given : a request method this enum does not know about, e.g. a 
WebDAV verb
+        String httpRequestMethod = "PROPFIND";
+
+        // when
+        HttpMethod httpMethod = HttpMethod.parseOrNull(httpRequestMethod);
+
+        // then
+        assertNull(httpMethod);
+    }
+
+    @Test
+    public void shouldParseKnownRequestMethod_regardlessOfCase() {
+        // given
+        String httpRequestMethod = "get";
+
+        // when
+        HttpMethod httpMethod = HttpMethod.parseOrNull(httpRequestMethod);
+
+        // then
+        assertEquals(HttpMethod.GET, httpMethod);
+    }
+
+    @Test
+    public void shouldParseNullRequestMethod_asNull() {
+        // given
+        String httpRequestMethod = null;
+
+        // when
+        HttpMethod httpMethod = HttpMethod.parseOrNull(httpRequestMethod);
+
+        // then
+        assertNull(httpMethod);
+    }
+
+    @Test
+    public void shouldStillRejectUnknownMethodInConfiguration() {
+        // given : configuration is trusted input, an unknown name there is a 
real error
+        String configuredMethod = "PROPFIND";
+
+        // when / then
+        assertThrows(IllegalArgumentException.class, () -> 
HttpMethod.parse(configuredMethod));
+    }
+
 }

Reply via email to