This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.18.x by this push:
new e9257672fb39 [backport camel-4.18.x] CAMEL-28008: camel-rest - Rest
DSL CORS does not allow credentials for the wildcard origin (#31229)
e9257672fb39 is described below
commit e9257672fb3902ba9ec6bb8df570f67f07f02271
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Dec 2 08:54:23 2026 +0100
[backport camel-4.18.x] CAMEL-28008: camel-rest - Rest DSL CORS does not
allow credentials for the wildcard origin (#31229)
Backport of #31212
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 | 61 ++++++++++++++++++++++
4 files changed, 102 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 596f9db9df38..947ac6d4b99a 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
@@ -16,10 +16,12 @@
*/
package org.apache.camel.support.processor;
+import java.util.Collections;
import java.util.HashMap;
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;
@@ -55,6 +57,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_JSON = "json";
@@ -147,6 +154,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;
@@ -529,6 +540,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();
@@ -552,11 +574,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 51dc3f37a519..9c6279297e6b 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
@@ -11,6 +11,67 @@ Note that manual migration is still required.
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
+same check of a consumer) now resolves the `.` and `..` segments of a name the
way a server does, and refuses a name
+with a `..` segment that goes above the start of the name, also when the
endpoint has no directory or its directory is
+the root (`/`). A name that starts like a scheme or a drive, such as `http:`,
`file:` or `C:`, is resolved like any
+other name. For example, a producer without a directory now refuses
`/../report.txt`, which a server wrote as
+`/report.txt`. The exchange fails with an `IllegalArgumentException` stating
that the filename is jailed to the starting
+directory. Remove the `..` segments from such a name.
+
+=== camel-ftp, camel-mina-sftp, camel-jsch, camel-azure-files - a file name
with both a backslash and a `..` segment is refused in some configurations
+
+With `jailStartingDirectory=true` (the default), a producer now refuses a file
name that has both a backslash and a
+`..` segment, such as `sub\..\report.txt`, when the `separator` option is
`Windows` or `Auto`, when the producer
+writes a temp file (`tempPrefix` or `tempFileName`) or a done file
(`doneFileName`), or when `checksumFileAlgorithm` is
+set (with `checksumWriteFile` at its default, `true`). Where such a name goes
depends on the server and on the platform
+that Camel runs on. The exchange fails with an `IllegalArgumentException`
stating that the filename is jailed to the
+starting directory. With the default options, the producer replaces each
backslash by `/` and writes such a name as
+before. Remove the backslashes or the `..` segments from the name.
+
+=== camel-ftp, camel-mina-sftp - a stepwise recursive poll skips a listed
directory whose name is not one name
+
+A recursive consumer with `stepwise=true` (the default) changes into each sub
directory by its name, and back to its
+parent afterwards. It now skips a directory from the listing of the server
whose name is blank, `.` or `..`, or has a
+`/` or a `\` (other than a trailing `/`), and logs it at DEBUG level, as such
a name would leave the poll in another
+directory than the one it lists. With
`handleDirectoryParserAbsoluteResult=true` (ftp and ftps), the last name of the
+path that the server lists is checked. A backslash is a valid character in the
name of a directory on a server on
+Linux: to poll such a directory, set `stepwise=false`.
+
+=== camel-fop - the CamelFop.Render and CamelFop.Encrypt headers are applied
as given
+
+The `fop` producer now applies only the `CamelFop.Render.*` and
`CamelFop.Encrypt.*` headers that the
+xref:components::fop-component.adoc[FOP] component page lists, and uses their
values as given, converted to the type
+of the option. Before, these headers were bound onto the FOP user agent and
the PDF encryption parameters like
+endpoint options, so a value with `{{` `}}` or a value that starts with `#`
was resolved as a property placeholder or
+a bean reference, and any property of these objects could be set. A route that
needs a property placeholder or a bean
+in such a header resolves it before the `fop` endpoint, for example with the
Simple language. Other names are ignored,
+and a WARN is logged. Set other FOP user agent settings in the FOP
configuration file (`userConfigURL`) or on a
+custom `FopFactory`.
+
+=== camel-azure-eventgrid, camel-azure-servicebus, camel-azure-eventhubs - a
DefaultAzureCredential set as tokenCredential is used
+
+With `credentialType` `AZURE_IDENTITY`, which these components also choose
when the `tokenCredential` option is a
+`DefaultAzureCredential`, the clients now authenticate with the configured
`tokenCredential` instance. Before, they
+built a new `DefaultAzureCredential` with the default settings, so a
credential built with
+`managedIdentityClientId(...)`, `tenantId(...)` or other settings was not
used. Without `tokenCredential`,
+`AZURE_IDENTITY` still builds a new `DefaultAzureCredential`. In
camel-azure-eventgrid, an explicit `credentialType`
+`TOKEN_CREDENTIAL` or `AZURE_IDENTITY` is no longer replaced when
`tokenCredential` is set. In camel-azure-servicebus
+and camel-azure-eventhubs on 4.18.x, the components still choose the
`credentialType` from `tokenCredential` when it
+is set, also over an explicit `credentialType`: `AZURE_IDENTITY` for a
`DefaultAzureCredential` and
+`TOKEN_CREDENTIAL` for any other credential. Both use the `tokenCredential`
instance.
+
== Upgrading from 4.18.5 to 4.18.6
=== camel-jpa - query parameters from CamelJpaParameters header are bound
literally