davsclaus commented on code in PR #25315:
URL: https://github.com/apache/camel/pull/25315#discussion_r3706435887
##########
core/camel-core-engine/src/main/docs/modules/eips/pages/toD-eip.adoc:
##########
@@ -417,3 +417,30 @@ during startup if `toD` was optimized, or if there was a
failure loading the opt
----
Detected SendDynamicAware component: http optimising toD:
http:myloginserver:8080/login?userid=${header.userName}
----
+
+== Restricting the allowed component schemes
+
+Because `toD` computes its endpoint uri at runtime, a route that interpolates
message content into the uri
+(for example `toD("${header.target}")`) can end up resolving to any component
on the classpath. In low-code or
+Kamelet-style deployments you may want to constrain this to a fixed set of
components. The optional `allowedSchemes`
+option takes a comma-separated allow-list of component schemes; a resolved
recipient whose scheme is not in the list
+is rejected (independently of `ignoreInvalidEndpoint`). By default the option
is unset and any scheme is allowed.
+
+[source,java]
+----
+from("direct:start")
+ // only http and https recipients are permitted
+ .toD().allowedSchemes("http,https").uri("${header.target}");
+----
+
+And in XML:
+
+[source,xml]
+----
+<route>
+ <from uri="direct:start"/>
+ <toD uri="${header.target}" allowedSchemes="http,https"/>
+</route>
+----
Review Comment:
**Consider adding a YAML DSL example.**
The section targets "low-code / Kamelet-style deployments" which
predominantly use YAML DSL, but only Java and XML examples are shown. A YAML
example would serve the primary target audience:
```yaml
- route:
from:
uri: direct:start
steps:
- toD:
uri: "${header.target}"
allowedSchemes: "http,https"
```
Non-blocking.
##########
core/camel-core/src/test/java/org/apache/camel/processor/ToDynamicAllowedSchemesTest.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * The optional {@code allowedSchemes} allow-list on {@code toD} restricts
which component schemes a dynamic recipient
+ * may resolve to. A recipient whose scheme is not in the list is rejected,
independently of
+ * {@code ignoreInvalidEndpoint}. See CAMEL-24298.
+ */
+class ToDynamicAllowedSchemesTest extends ContextTestSupport {
+
+ @Test
+ void allowedSchemeIsSent() throws Exception {
+ getMockEndpoint("mock:allowed").expectedMessageCount(1);
+
+ template.sendBodyAndHeader("direct:start", "Hello", "target",
"mock:allowed");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ void disallowedSchemeIsRejected() {
+ assertThatThrownBy(() -> template.sendBodyAndHeader("direct:start",
"Hello", "target", "seda:blocked"))
+ .isInstanceOf(CamelExecutionException.class)
+ .cause()
+ .isInstanceOf(ResolveEndpointFailedException.class)
+ .hasMessageContaining("not in the allowed schemes");
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+
from("direct:start").toD().allowedSchemes("mock").uri("${header.target}");
+ }
+ };
Review Comment:
**Missing test: `ignoreInvalidEndpoint` independence.**
The PR description, the code comment in `SendDynamicProcessor`, and the
documentation all explicitly state that a disallowed scheme is rejected
*independently* of `ignoreInvalidEndpoint`. This is a security-relevant
contract worth locking down with a test.
Consider adding a third test method with a separate route that sets both
options:
```java
@Test
void disallowedSchemeIsRejectedEvenWhenIgnoreInvalidEndpoint() {
assertThatThrownBy(() -> template.sendBodyAndHeader("direct:lenient",
"Hello", "target", "seda:blocked"))
.isInstanceOf(CamelExecutionException.class)
.cause()
.isInstanceOf(ResolveEndpointFailedException.class)
.hasMessageContaining("not in the allowed schemes");
}
```
with the route:
```java
from("direct:lenient").toD().allowedSchemes("mock").ignoreInvalidEndpoint("true").uri("${header.target}");
```
Non-blocking — can be a follow-up.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]