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

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


The following commit(s) were added to refs/heads/main by this push:
     new 36a45972b8e8 CAMEL-28008: camel-rest - Rest DSL CORS does not allow 
credentials for the wildcard origin (#31212)
36a45972b8e8 is described below

commit 36a45972b8e80863273c1d61515bf6af2a63ad29
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Dec 2 07:13:55 2026 +0100

    CAMEL-28008: camel-rest - Rest DSL CORS does not allow credentials for the 
wildcard origin (#31212)
    
    With Access-Control-Allow-Credentials=true and the default 
Access-Control-Allow-Origin *,
    RestBindingAdvice answered with the origin of the request together with
    Access-Control-Allow-Credentials=true. Credentials are now only allowed for 
a configured
    origin: with * the header is left out, and a WARN is logged once per 
CamelContext. This
    matches platform-http-vertx, camel-jetty, camel-cometd and camel-a2a.
    
    The upgrade guides have entries for 4.25, 4.22.4 and 4.18.7.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../rest/FromRestGetCorsAllowCredentialsTest.java  | 10 +++--
 ...RestGetCorsAllowCredentialsWithOriginTest.java} | 46 ++++------------------
 .../camel/support/processor/RestBindingAdvice.java | 32 ++++++++++++---
 .../ROOT/pages/camel-4x-upgrade-guide-4_18.adoc    |  7 ++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_22.adoc    |  7 ++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc    |  8 ++++
 .../modules/ROOT/pages/rest-dsl-binding.adoc       |  5 +++
 7 files changed, 68 insertions(+), 47 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsTest.java
index 0c685941441e..98bee6b34f8c 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsTest.java
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
 
 public class FromRestGetCorsAllowCredentialsTest extends ContextTestSupport {
 
@@ -57,7 +58,8 @@ public class FromRestGetCorsAllowCredentialsTest extends 
ContextTestSupport {
         assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS,
                 out.getMessage().getHeader("Access-Control-Allow-Headers"));
         assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE, 
out.getMessage().getHeader("Access-Control-Max-Age"));
-        assertEquals("true", 
out.getMessage().getHeader("Access-Control-Allow-Credentials"));
+        // credentials are not allowed with the origin *
+        
assertNull(out.getMessage().getHeader("Access-Control-Allow-Credentials"));
 
         assertMockEndpointsSatisfied();
     }
@@ -82,8 +84,10 @@ public class FromRestGetCorsAllowCredentialsTest extends 
ContextTestSupport {
         assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS,
                 out.getMessage().getHeader("Access-Control-Allow-Headers"));
         assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE, 
out.getMessage().getHeader("Access-Control-Max-Age"));
-        assertEquals("true", 
out.getMessage().getHeader("Access-Control-Allow-Credentials"));
-        assertEquals("mydomain", 
out.getMessage().getHeader("Access-Control-Allow-Origin"));
+        // the origin of the request is not sent back: no origin is 
configured, so it stays * without credentials
+        assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN,
+                out.getMessage().getHeader("Access-Control-Allow-Origin"));
+        
assertNull(out.getMessage().getHeader("Access-Control-Allow-Credentials"));
 
         assertMockEndpointsSatisfied();
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsWithOriginTest.java
similarity index 53%
copy from 
core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsTest.java
copy to 
core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsWithOriginTest.java
index 0c685941441e..49c24cd56073 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetCorsAllowCredentialsWithOriginTest.java
@@ -21,13 +21,12 @@ import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.spi.Registry;
-import org.apache.camel.spi.RestConfiguration;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
-public class FromRestGetCorsAllowCredentialsTest extends ContextTestSupport {
+public class FromRestGetCorsAllowCredentialsWithOriginTest extends 
ContextTestSupport {
 
     @Override
     protected Registry createCamelRegistry() throws Exception {
@@ -37,63 +36,32 @@ public class FromRestGetCorsAllowCredentialsTest extends 
ContextTestSupport {
     }
 
     @Test
-    public void testCorsWithoutOrigin() throws Exception {
-        // the rest becomes routes and the input is a seda endpoint created by
-        // the DummyRestConsumerFactory
+    public void testCorsWithConfiguredOrigin() throws Exception {
         getMockEndpoint("mock:update").expectedMessageCount(1);
 
         Exchange out = template.request("seda:post-say-bye", new Processor() {
             @Override
             public void process(Exchange exchange) {
+                exchange.getIn().setHeader("Origin", 
"https://other.example.com";);
                 exchange.getIn().setBody("I was here");
             }
         });
         assertNotNull(out);
 
-        assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN,
-                out.getMessage().getHeader("Access-Control-Allow-Origin"));
-        assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS,
-                out.getMessage().getHeader("Access-Control-Allow-Methods"));
-        assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS,
-                out.getMessage().getHeader("Access-Control-Allow-Headers"));
-        assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE, 
out.getMessage().getHeader("Access-Control-Max-Age"));
+        // credentials are allowed for the configured origin, and the origin 
of the request is not sent back
+        assertEquals("https://app.example.com";, 
out.getMessage().getHeader("Access-Control-Allow-Origin"));
         assertEquals("true", 
out.getMessage().getHeader("Access-Control-Allow-Credentials"));
 
         assertMockEndpointsSatisfied();
     }
 
-    @Test
-    public void testCorsWithOrigin() throws Exception {
-        // the rest becomes routes and the input is a seda endpoint created by
-        // the DummyRestConsumerFactory
-        getMockEndpoint("mock:update").expectedMessageCount(1);
-
-        Exchange out = template.request("seda:post-say-bye", new Processor() {
-            @Override
-            public void process(Exchange exchange) {
-                exchange.getIn().setHeader("Origin", "mydomain");
-                exchange.getIn().setBody("I was here");
-            }
-        });
-        assertNotNull(out);
-
-        assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS,
-                out.getMessage().getHeader("Access-Control-Allow-Methods"));
-        assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS,
-                out.getMessage().getHeader("Access-Control-Allow-Headers"));
-        assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE, 
out.getMessage().getHeader("Access-Control-Max-Age"));
-        assertEquals("true", 
out.getMessage().getHeader("Access-Control-Allow-Credentials"));
-        assertEquals("mydomain", 
out.getMessage().getHeader("Access-Control-Allow-Origin"));
-
-        assertMockEndpointsSatisfied();
-    }
-
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             @Override
             public void configure() {
-                
restConfiguration().host("localhost").enableCORS(true).corsAllowCredentials(true);
+                
restConfiguration().host("localhost").enableCORS(true).corsAllowCredentials(true)
+                        .corsHeaderProperty("Access-Control-Allow-Origin", 
"https://app.example.com";);
 
                 rest("/say/hello").get().to("direct:hello");
 
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
index 4654f0a15661..e18461d9b298 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
@@ -19,11 +19,13 @@ package org.apache.camel.support.processor;
 import java.io.InputStream;
 import java.io.Reader;
 import java.nio.ByteBuffer;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
+import java.util.WeakHashMap;
 
 import org.apache.camel.AsyncProcessor;
 import org.apache.camel.CamelContext;
@@ -60,6 +62,11 @@ public class RestBindingAdvice extends ServiceSupport 
implements CamelInternalPr
 
     private static final Logger LOG = 
LoggerFactory.getLogger(RestBindingAdvice.class);
 
+    // the CamelContexts that have logged the WARN of credentials with the 
origin *, so it is logged once per context
+    // and not once per rest operation
+    private static final Set<CamelContext> CORS_CREDENTIALS_WARNED = 
Collections.newSetFromMap(
+            Collections.synchronizedMap(new WeakHashMap<>()));
+
     private static final String STATE_KEY_DO_MARSHAL = "doMarshal";
     private static final String STATE_KEY_ACCEPT = "accept";
     private static final String STATE_KEY_REQUEST_CONTENT_TYPE = 
"requestContentType";
@@ -154,6 +161,10 @@ public class RestBindingAdvice extends ServiceSupport 
implements CamelInternalPr
         this.clientResponseValidation = clientResponseValidation;
         this.enableCORS = enableCORS;
         this.corsHeaders = corsHeaders;
+        if (enableCORS && isCredentialsWithAnyOrigin(corsHeaders) && 
CORS_CREDENTIALS_WARNED.add(camelContext)) {
+            LOG.warn("CORS Access-Control-Allow-Credentials=true is ignored 
because Access-Control-Allow-Origin is *."
+                     + " Configure the allowed origin in the rest 
configuration to allow credentials.");
+        }
         this.queryDefaultValues = queryDefaultValues;
         this.queryAllowedValues = queryAllowedValues;
         this.requiredBody = requiredBody;
@@ -586,6 +597,17 @@ public class RestBindingAdvice extends ServiceSupport 
implements CamelInternalPr
         return first;
     }
 
+    /**
+     * Whether the CORS headers allow credentials with the origin * (the 
default origin when none is configured).
+     */
+    static boolean isCredentialsWithAnyOrigin(Map<String, String> corsHeaders) 
{
+        if (corsHeaders == null || 
!"true".equalsIgnoreCase(corsHeaders.get("Access-Control-Allow-Credentials"))) {
+            return false;
+        }
+        String allowOrigin = corsHeaders.get("Access-Control-Allow-Origin");
+        return allowOrigin == null || "*".equals(allowOrigin);
+    }
+
     private void setCORSHeaders(Exchange exchange) {
         // add the CORS headers after routing, but before the consumer writes 
the response
         Message msg = exchange.getMessage();
@@ -609,11 +631,11 @@ public class RestBindingAdvice extends ServiceSupport 
implements CamelInternalPr
         }
         String allowCredentials = corsHeaders != null ? 
corsHeaders.get("Access-Control-Allow-Credentials") : null;
 
-        // Restrict the origin if credentials are allowed.
-        // https://www.w3.org/TR/cors/ - section 6.1, point 3
-        String origin = exchange.getIn().getHeader("Origin", String.class);
-        if ("true".equalsIgnoreCase(allowCredentials) && 
"*".equals(allowOrigin) && origin != null) {
-            allowOrigin = origin;
+        // credentials are only allowed for the origins that are configured: 
the fetch spec does not allow them with
+        // the origin *, and the origin of the request is not sent back in its 
place (as platform-http-vertx and
+        // camel-a2a do), so with * the Access-Control-Allow-Credentials 
header is left out
+        if ("true".equalsIgnoreCase(allowCredentials) && 
"*".equals(allowOrigin)) {
+            allowCredentials = null;
         }
 
         msg.setHeader("Access-Control-Allow-Origin", allowOrigin);
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_18.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_18.adoc
index 7df95117a74d..d1f8e7bcea4e 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_18.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_18.adoc
@@ -13,6 +13,13 @@ See the xref:camel-upgrade-recipes-tool.adoc[documentation] 
page for details.
 
 == Upgrading from 4.18.6 to 4.18.7
 
+=== Rest DSL - CORS credentials require a configured origin
+
+When CORS is enabled with `Access-Control-Allow-Credentials=true` 
(`corsAllowCredentials(true)`) and
+`Access-Control-Allow-Origin` is left at its default `*`, the response no 
longer echoes the `Origin` of the request
+and leaves out `Access-Control-Allow-Credentials`. Camel logs a WARN when the 
routes start. To allow credentials,
+configure the allowed origin, for example 
`corsHeaderProperty("Access-Control-Allow-Origin", "https://app.example.com";)`.
+
 === camel-ftp, camel-mina-sftp, camel-jsch, camel-azure-files - the starting 
directory check resolves a name as a server does
 
 The check that a file is in the starting directory 
(`jailStartingDirectory=true`, the default, for a producer, and the
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
index 2efb6b6b52af..fa85700efe6e 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
@@ -13,6 +13,13 @@ See the xref:camel-upgrade-recipes-tool.adoc[documentation] 
page for details.
 
 == Upgrading from 4.22.3 to 4.22.4
 
+=== Rest DSL - CORS credentials require a configured origin
+
+When CORS is enabled with `Access-Control-Allow-Credentials=true` 
(`corsAllowCredentials(true)`) and
+`Access-Control-Allow-Origin` is left at its default `*`, the response no 
longer echoes the `Origin` of the request
+and leaves out `Access-Control-Allow-Credentials`. Camel logs a WARN when the 
routes start. To allow credentials,
+configure the allowed origin, for example 
`corsHeaderProperty("Access-Control-Allow-Origin", "https://app.example.com";)`.
+
 === camel-openai, camel-spring-ai-chat - the path of the mcpServer url is the 
endpoint of the MCP server
 
 With `transportType=streamableHttp` (camel-openai) or `transportType=sse` 
(camel-openai and camel-spring-ai-chat), the
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
index df2c96a64ddc..d8e420ab8402 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
@@ -13,6 +13,14 @@ See the xref:camel-upgrade-recipes-tool.adoc[documentation] 
page for details.
 
 == Upgrading Camel 4.24 to 4.25
 
+=== Rest DSL - CORS credentials require a configured origin
+
+When CORS is enabled with `Access-Control-Allow-Credentials=true` 
(`corsAllowCredentials(true)`) and
+`Access-Control-Allow-Origin` is left at its default `*`, the response no 
longer echoes the `Origin` of the request
+and leaves out `Access-Control-Allow-Credentials`. Camel logs a WARN when the 
routes start. To allow credentials,
+configure the allowed origin, for example 
`corsHeaderProperty("Access-Control-Allow-Origin", "https://app.example.com";)`.
+This matches what `platform-http-vertx` and `camel-a2a` already do.
+
 === camel-pqc - default-material accessors and provider resolution
 
 The `PQCDefault*Material` helper classes now initialize their default 
cryptographic material lazily. Their backing
diff --git a/docs/user-manual/modules/ROOT/pages/rest-dsl-binding.adoc 
b/docs/user-manual/modules/ROOT/pages/rest-dsl-binding.adoc
index b63679917897..ae9814bb2b92 100644
--- a/docs/user-manual/modules/ROOT/pages/rest-dsl-binding.adoc
+++ b/docs/user-manual/modules/ROOT/pages/rest-dsl-binding.adoc
@@ -657,3 +657,8 @@ value.
 |`Access-Control-Allow-Headers` |Origin, Accept, X-Requested-With, 
Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers
 |`Access-Control-Max-Age` |3600
 |===
+
+`Access-Control-Allow-Credentials` is not set by default. You can set it with 
`corsAllowCredentials(true)`, but
+credentials are only allowed for an origin you configure in 
`Access-Control-Allow-Origin`, such as
+`https://app.example.com`. With the default origin `*` the 
`Access-Control-Allow-Credentials` header is left out of the
+response, and Camel logs a WARN when the routes start.

Reply via email to