oscerd commented on code in PR #25315:
URL: https://github.com/apache/camel/pull/25315#discussion_r3710073830


##########
core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java:
##########
@@ -494,4 +507,24 @@ public boolean isAutoStartupComponents() {
     public void setAutoStartupComponents(boolean autoStartupComponents) {
         this.autoStartupComponents = autoStartupComponents;
     }
+
+    public String getAllowedSchemes() {
+        return allowedSchemes;
+    }
+
+    public void setAllowedSchemes(String allowedSchemes) {
+        this.allowedSchemes = allowedSchemes;
+    }
+
+    private boolean isSchemeAllowed(String scheme) {
+        if (allowedSchemes == null) {
+            return true;
+        }
+        for (String allowed : allowedSchemes.split(",")) {
+            if (allowed.trim().equals(scheme)) {

Review Comment:
   Done in 92389a8 — `setAllowedSchemes` now pre-parses into a `HashSet` once, 
and the per-exchange check is a single `Set.contains(scheme)` with no 
allocation on the hot path. Thanks!
   
   _Claude Code on behalf of Andrea Cosentino_



##########
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:
   Added `multipleAllowedSchemesHonored` (92389a8): it configures 
`allowedSchemes("mock,seda")` and verifies both are honored, while a scheme 
outside the list (`log:`) is rejected.
   
   _Claude Code on behalf of Andrea Cosentino_



##########
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:
   Good catch — added `disallowedSchemeRejectedEvenWithIgnoreInvalidEndpoint` 
(92389a8): a route with `ignoreInvalidEndpoint(true)` still hard-fails on a 
disallowed scheme, since the allow-list check sets the exception directly (and 
returns) instead of throwing, so it bypasses the `ignoreInvalidEndpoint` 
handling.
   
   _Claude Code on behalf of Andrea Cosentino_



##########
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:
   Added a YAML DSL example alongside the Java and XML ones (92389a8).
   
   _Claude Code on behalf of Andrea Cosentino_



-- 
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]

Reply via email to