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

markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new a2da7fbf8b Fix BZ 69214. CORS. POST with no content-type is not a 
reason to reject
a2da7fbf8b is described below

commit a2da7fbf8b119fc3b06b4b514483e3eae8d1fbb7
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Mon Jul 22 20:44:25 2024 +0100

    Fix BZ 69214. CORS. POST with no content-type is not a reason to reject
    
    https://bz.apache.org/bugzilla/show_bug.cgi?id=69214
---
 java/org/apache/catalina/filters/CorsFilter.java     |  4 +++-
 test/org/apache/catalina/filters/TestCorsFilter.java | 18 +++++++++++++++++-
 webapps/docs/changelog.xml                           |  6 ++++++
 3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/java/org/apache/catalina/filters/CorsFilter.java 
b/java/org/apache/catalina/filters/CorsFilter.java
index 6fb793ad3f..05dee30c9f 100644
--- a/java/org/apache/catalina/filters/CorsFilter.java
+++ b/java/org/apache/catalina/filters/CorsFilter.java
@@ -567,7 +567,9 @@ public class CorsFilter extends GenericFilter {
                         requestType = CORSRequestType.SIMPLE;
                     } else if ("POST".equals(method)) {
                         String mediaType = 
getMediaType(request.getContentType());
-                        if (mediaType != null) {
+                        if (mediaType == null) {
+                            requestType = CORSRequestType.SIMPLE;
+                        } else {
                             if 
(SIMPLE_HTTP_REQUEST_CONTENT_TYPE_VALUES.contains(mediaType)) {
                                 requestType = CORSRequestType.SIMPLE;
                             } else {
diff --git a/test/org/apache/catalina/filters/TestCorsFilter.java 
b/test/org/apache/catalina/filters/TestCorsFilter.java
index 7989a85145..d4b6694d35 100644
--- a/test/org/apache/catalina/filters/TestCorsFilter.java
+++ b/test/org/apache/catalina/filters/TestCorsFilter.java
@@ -703,7 +703,7 @@ public class TestCorsFilter {
      * @throws ServletException
      */
     @Test
-    public void testCheckSimpleRequestType() throws ServletException {
+    public void testCheckSimpleRequestTypeGet() throws ServletException {
         TesterHttpServletRequest request = new TesterHttpServletRequest();
         request.setHeader(CorsFilter.REQUEST_HEADER_ORIGIN, 
TesterFilterConfigs.HTTP_TOMCAT_APACHE_ORG);
         request.setMethod("GET");
@@ -713,6 +713,22 @@ public class TestCorsFilter {
         Assert.assertEquals(CorsFilter.CORSRequestType.SIMPLE, requestType);
     }
 
+    /*
+     * Happy path test, when a valid CORS Simple request arrives.
+     *
+     * @throws ServletException
+     */
+    @Test
+    public void testCheckSimpleRequestTypePost() throws ServletException {
+        TesterHttpServletRequest request = new TesterHttpServletRequest();
+        request.setHeader(CorsFilter.REQUEST_HEADER_ORIGIN, 
TesterFilterConfigs.HTTP_TOMCAT_APACHE_ORG);
+        request.setMethod("POST");
+        CorsFilter corsFilter = new CorsFilter();
+        corsFilter.init(TesterFilterConfigs.getDefaultFilterConfig());
+        CorsFilter.CORSRequestType requestType = 
corsFilter.checkRequestType(request);
+        Assert.assertEquals(CorsFilter.CORSRequestType.SIMPLE, requestType);
+    }
+
     /*
      * Happy path test, when a valid CORS Simple request arrives.
      *
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 9d098de379..fad32fca8c 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -115,6 +115,12 @@
         the Servlet API (removing the need for the cast) in Servlet 6.2 
onwards.
         (markt)
       </add>
+      <fix>
+        <bug>69214</bug>: Do not reject a CORS request that uses POST but does
+        not include a <code>content-type</code> header. Tomcat now correctly
+        processes this as a simple CORS request. Based on a patch suggested by
+        thebluemountain. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to