This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27160 in repository https://gitbox.apache.org/repos/asf/camel.git
commit fdedd03aed2b75b4992a314c87dcea74f111fb34 Author: Andrea Cosentino <[email protected]> AuthorDate: Thu Nov 19 09:07:39 2026 +0100 CAMEL-27160: literal option binding scope; keep #bean:/#type: references Address review on the dynamic-endpoint option binding: - Make literal option binding a creation scope (PropertyBindingSupport.setLiteralOptionBinding, opened in DefaultComponent.createEndpoint and read in DefaultEndpoint.setProperties), so components that bind their options inside their own createEndpoint (the two-argument setProperties path, used by log, aws2-s3, sql, jms and many others) also bind message-derived values as literal text. - Narrow literal mode to {{...}} placeholders, #class: and #property: references; plain registry references #bean: and #type: still resolve, so toD("...?dataSource=#ds") keeps working. - Warn when literal binding is requested for an endpoint that does not extend DefaultEndpoint. - Add self-binding component test cases plus a #bean:-resolves and a #property:-literal case, and escape the {{...}} samples in the upgrade guide. Co-Authored-By: Claude Opus 4.8 <[email protected]> Signed-off-by: Andrea Cosentino <[email protected]> --- .../DynamicEndpointMessageOptionLiteralTest.java | 60 ++++++++++++++++++- .../org/apache/camel/support/DefaultComponent.java | 23 ++++++++ .../org/apache/camel/support/DefaultEndpoint.java | 5 +- .../org/apache/camel/support/NormalizedUri.java | 8 ++- .../camel/support/PropertyBindingSupport.java | 68 ++++++++++++++++++---- .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 25 ++++---- 6 files changed, 160 insertions(+), 29 deletions(-) diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/DynamicEndpointMessageOptionLiteralTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/DynamicEndpointMessageOptionLiteralTest.java index 46aa5e5686e4..93a87f920d90 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/DynamicEndpointMessageOptionLiteralTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/DynamicEndpointMessageOptionLiteralTest.java @@ -63,6 +63,7 @@ class DynamicEndpointMessageOptionLiteralTest extends ContextTestSupport { prop.setProperty("secretTarget", "resolved"); context.getPropertiesComponent().setInitialProperties(prop); context.addComponent("sink", new SinkComponent()); + context.addComponent("selfsink", new SelfBindingSinkComponent()); context.getRegistry().bind("marker", new Marker()); return context; } @@ -95,13 +96,51 @@ class DynamicEndpointMessageOptionLiteralTest extends ContextTestSupport { } @Test - void toDOptionBeanReferenceTokenStaysLiteral() throws Exception { + void toDOptionBeanReferenceStillResolves() throws Exception { getMockEndpoint("mock:done").expectedMessageCount(1); template.sendBodyAndHeader("direct:tod", "Hello", "target", "sink:c?option=" + MARKER_BEAN); assertMockEndpointsSatisfied(); - assertThat(SinkEndpoint.lastOption).isEqualTo(MARKER_BEAN); + // a #bean: / #type: reference is a plain registry lookup of an already-registered bean, so it is still + // resolved: this keeps references a route author wrote in a dynamic uri (such as dataSource=#ds) working. + // Only property placeholders, #class: and #property: are kept literal. + assertThat(SinkEndpoint.lastOption).isSameAs(context.getRegistry().lookupByName("marker")); + assertThat(Marker.instances).isZero(); + } + + @Test + void toDOptionPropertyReferenceStaysLiteral() throws Exception { + getMockEndpoint("mock:done").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:tod", "Hello", "target", "sink:p?option=#property:secretTarget"); + + assertMockEndpointsSatisfied(); + assertThat(SinkEndpoint.lastOption).isEqualTo("#property:secretTarget"); + } + + @Test + void toDSelfBindingComponentClassReferenceStaysLiteral() throws Exception { + getMockEndpoint("mock:done").expectedMessageCount(1); + + // selfsink binds its options inside its own createEndpoint, like log/aws2-s3/sql/jms and ~329 other + // components; literal binding must be enforced there too (CAMEL-27160), so a #class: option value from the + // message is not instantiated + template.sendBodyAndHeader("direct:tod", "Hello", "target", "selfsink:a?option=" + MARKER_CLASS); + + assertMockEndpointsSatisfied(); + assertThat(SinkEndpoint.lastOption).isEqualTo(MARKER_CLASS); + assertThat(Marker.instances).isZero(); + } + + @Test + void toDSelfBindingComponentPlaceholderStaysLiteral() throws Exception { + getMockEndpoint("mock:done").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:tod", "Hello", "target", "selfsink:b?option={{secretTarget}}"); + + assertMockEndpointsSatisfied(); + assertThat(SinkEndpoint.lastOption).isEqualTo("{{secretTarget}}"); } @Test @@ -199,11 +238,26 @@ class DynamicEndpointMessageOptionLiteralTest extends ContextTestSupport { } } + /** + * Like {@link SinkComponent} but binds its options inside its own {@code createEndpoint} (through the two-argument + * {@code setProperties}), the pattern used by {@code log}, {@code aws2-s3}, {@code sql}, {@code jms} and ~329 other + * components. It proves that literal binding is also enforced for these components (CAMEL-27160). + */ + private static final class SelfBindingSinkComponent extends DefaultComponent { + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) + throws Exception { + SinkEndpoint endpoint = new SinkEndpoint(uri, this); + setProperties(endpoint, parameters); + return endpoint; + } + } + private static final class SinkEndpoint extends DefaultEndpoint { static volatile Object lastOption; private Object option; - SinkEndpoint(String uri, SinkComponent component) { + SinkEndpoint(String uri, DefaultComponent component) { super(uri, component); } diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java index 019dc85185e8..d6aa74a8bfd7 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java @@ -111,6 +111,21 @@ public abstract class DefaultComponent extends ServiceSupport implements Compone */ public Endpoint createEndpoint(String uri, Map<String, Object> properties, boolean literalOptionBinding) throws Exception { + if (!literalOptionBinding) { + return doCreateEndpoint(uri, properties, false); + } + // mark the whole endpoint creation as literal so that components that bind their options inside their own + // createEndpoint (through the two-argument setProperties) also bind the message-derived values literally + boolean prev = PropertyBindingSupport.setLiteralOptionBinding(true); + try { + return doCreateEndpoint(uri, properties, true); + } finally { + PropertyBindingSupport.setLiteralOptionBinding(prev); + } + } + + private Endpoint doCreateEndpoint(String uri, Map<String, Object> properties, boolean literalOptionBinding) + throws Exception { // need to encode before its safe to parse with java.net.Uri String encodedUri = UnsafeUriCharactersEncoder.encode(uri); URI u = new URI(encodedUri); @@ -442,6 +457,14 @@ public abstract class DefaultComponent extends ServiceSupport implements Compone if (literalOptionBinding && endpoint instanceof DefaultEndpoint de) { de.setProperties(endpoint, parameters, true); } else { + if (literalOptionBinding && LOG.isWarnEnabled()) { + // literal binding of message-derived option values is enforced through DefaultEndpoint; a custom + // endpoint that does not extend it falls back to the standard resolution path, which may resolve + // property placeholders or #class: / #property: references against the message-derived values + LOG.warn("Endpoint {} does not extend DefaultEndpoint; option values computed from the message cannot" + + " be bound as literal text and will use the standard property resolution", + endpoint.getEndpointUri()); + } endpoint.configureProperties(parameters); } } diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java index a3737c8116e7..4f1c0a795edf 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java @@ -403,7 +403,10 @@ public abstract class DefaultEndpoint extends ServiceSupport implements Endpoint * @param parameters properties to set */ public void setProperties(Object bean, Map<String, Object> parameters) { - setProperties(bean, parameters, false); + // honor a literal-option-binding scope opened while a dynamic endpoint is being created, so that options + // bound by a component inside its own createEndpoint are also kept literal (see + // PropertyBindingSupport#isLiteralOptionBinding and DefaultComponent#createEndpoint(String, Map, boolean)) + setProperties(bean, parameters, PropertyBindingSupport.isLiteralOptionBinding()); } /** diff --git a/core/camel-support/src/main/java/org/apache/camel/support/NormalizedUri.java b/core/camel-support/src/main/java/org/apache/camel/support/NormalizedUri.java index 9a19849b879e..522fd6f5e972 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/NormalizedUri.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/NormalizedUri.java @@ -35,9 +35,11 @@ public final class NormalizedUri extends ValueHolder<String> implements Normaliz // Whether the endpoint uri was computed from the current message at runtime (for example the per-message // recipient of toD, enrich, wireTap, pollEnrich, recipientList, routingSlip or dynamicRouter). When true the - // query option values are bound as literal text, i.e. without property-placeholder resolution and without - // bean/class reference (#class:, #bean:, #type:, ...) resolution, because those segments come from the message - // and not from the route. Not part of equals/hashCode: the endpoint registry key stays the uri alone. + // query option values are bound more strictly, to stop message content from being turned into routing + // configuration: a {{...}} property placeholder, a #class: reference or a #property: reference in such a value is + // kept as literal text (no placeholder resolution, no class instantiation, no property lookup), while plain + // registry references (#bean:, #type:) are still resolved. Not part of equals/hashCode: the endpoint registry key + // stays the uri alone. private final boolean literalOptionBinding; private NormalizedUri(String normalizedValue, String rawUri, boolean literalOptionBinding) { diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java index be660c596d37..2f281ac445d0 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java @@ -118,10 +118,48 @@ public final class PropertyBindingSupport { private PropertyBindingSupport() { } + // a thread-scoped signal that the endpoint currently being created was computed from the current message at + // runtime (a dynamic endpoint such as toD/enrich/wireTap/pollEnrich/recipientList/routingSlip/dynamicRouter), + // so its option values must be bound as literal text. It is set (in a try/finally) around the whole endpoint + // creation by DefaultComponent.createEndpoint(uri, properties, true) and read by DefaultComponent and + // DefaultEndpoint when they bind the options, so components that bind inside their own createEndpoint still honor + // literal binding. + private static final ThreadLocal<Boolean> LITERAL_OPTION_BINDING = new ThreadLocal<>(); + public static Builder build() { return new Builder(); } + /** + * Whether the endpoint currently being created on this thread must bind its query option values as literal text, + * because its uri was computed from the current message at runtime, see {@link Builder#withLiteral(boolean)}. + * + * @return true if literal option binding is active on the current thread + * @since 4.24 + */ + public static boolean isLiteralOptionBinding() { + return Boolean.TRUE.equals(LITERAL_OPTION_BINDING.get()); + } + + /** + * Marks (or clears) the current thread as creating an endpoint whose query option values must be bound as literal + * text. The caller MUST restore the returned previous value in a {@code finally} block, as + * {@link DefaultComponent#createEndpoint(String, java.util.Map, boolean)} does. + * + * @param literalOptionBinding whether literal option binding is active + * @return the previous value, to be restored by the caller + * @since 4.24 + */ + public static boolean setLiteralOptionBinding(boolean literalOptionBinding) { + boolean prev = isLiteralOptionBinding(); + if (literalOptionBinding) { + LITERAL_OPTION_BINDING.set(Boolean.TRUE); + } else { + LITERAL_OPTION_BINDING.remove(); + } + return prev; + } + /** * Binds the properties to the target object, and removes the property that was bound from properties. * <p> @@ -528,7 +566,16 @@ public final class PropertyBindingSupport { String key = name; Object text = value; - if (placeholder && !literal) { + // a value computed from the current message at runtime (literal mode) is kept verbatim only when resolving it + // would be unsafe: a {{...}} property placeholder (which reads env vars, system or application properties or + // vault secrets), a #class: reference (which instantiates a class and can invoke a public static or factory + // method), or a #property: lookup. Benign registry references (#bean:, #type:, ...) are still resolved, so a + // reference a route author wrote in the dynamic uri, such as dataSource=#ds, keeps working. + boolean literalValue = literal && text instanceof String lit + && (lit.contains(PropertiesComponent.PREFIX_TOKEN) + || lit.startsWith("#class:") || lit.startsWith("#property:")); + + if (placeholder && !literalValue) { // resolve property placeholders key = camelContext.resolvePropertyPlaceholders(key); if (text instanceof String s) { @@ -541,10 +588,9 @@ public final class PropertyBindingSupport { } } - // prepare the value before it is bound. In literal mode the value was computed from message content at - // runtime, so it is kept verbatim: neither property placeholders nor bean/class references (#class:, #bean:, - // #type:, ...) are resolved, because that resolution is reserved for values authored in the route. - if (literal) { + // prepare the value before it is bound + if (literalValue) { + // the message-derived value is kept verbatim: no property-placeholder, #class: or #property: resolution value = text; } else { try { @@ -566,11 +612,11 @@ public final class PropertyBindingSupport { } } - // in literal mode reference resolution must be disabled on every binding path, and a value that still looks - // like a reference (#...) must not be handed to the configurer either (the configurer resolves references for - // non-String options on its own), so it is bound via reflection as plain text instead - boolean effReference = reference && !literal; - boolean skipConfigurer = literal && value instanceof String refStr && isReferenceParameter(refStr); + // when the value was kept literal, reference resolution must be disabled on every binding path, and a value + // that still looks like a reference (#...) must not be handed to the configurer either (the configurer resolves + // references for non-String options on its own), so it is bound via reflection as plain text instead + boolean effReference = reference && !literalValue; + boolean skipConfigurer = literalValue && value instanceof String refStr && isReferenceParameter(refStr); // okay we are ready to set the value, but the property key // can still be complex such as a map/list/array so we need to handle them specially than a regular key @@ -594,7 +640,7 @@ public final class PropertyBindingSupport { if (!bound && reflection) { // fallback to reflection based bound = setSimplePropertyViaReflection(camelContext, target, key, value, fluentBuilder, allowPrivateSetter, - !literal && reflection, ignoreCase); + !literalValue && reflection, ignoreCase); } // if the target value is a map type, then we can skip reflection // and set the entry diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc index ae09883c037b..7b1c4c3ef518 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc @@ -137,17 +137,20 @@ includes the template of `sql-stored` and the bucket name of `aws2-s3`. The dynamic-endpoint EIPs -- `toD`, `enrich`, `wireTap`, `pollEnrich`, `recipientList`, `routingSlip` and `dynamicRouter` -- compute their target endpoint from the current message at runtime. The query option values of that -per-message endpoint are now bound as literal text: a property-placeholder token (`{{...}}`) or a bean or class -reference token (`#class:`, `#bean:`, `#type:`) that appears in a runtime-computed option value is kept verbatim, so it -is neither resolved as a property nor used to look up a bean or instantiate a class. This extends to the option binding -the treatment that `toD`, `enrich`, `wireTap` and `pollEnrich` already gave the endpoint path, where property -placeholders in the computed path are not resolved. `recipientList`, `routingSlip` and `dynamicRouter` still resolve -property placeholders in the computed uri string, only their option values change. - -Property placeholders written in the static route template are still resolved once when the route starts, so -`toD("mock:foo?delay={{myDelay}}")` is unchanged. A `#bean:`, `#class:` or `#type:` reference, however, was resolved -when the endpoint was created, so such a reference in the option of a dynamic endpoint is now kept literal as well: -configure that option on the component, or use a static `to(...)` endpoint, if it must be resolved. +per-message endpoint are now bound more strictly: a property-placeholder token (`+{{...}}+`), a `#class:` reference +(which would instantiate a class and can invoke a public static or factory method) or a `#property:` reference that +appears in a runtime-computed option value is kept verbatim, so it is neither resolved as a property nor used to +instantiate a class. This extends to the option binding the treatment that `toD`, `enrich`, `wireTap` and `pollEnrich` +already gave the endpoint path, where property placeholders in the computed path are not resolved. `recipientList`, +`routingSlip` and `dynamicRouter` still resolve property placeholders in the computed uri string; only this subset of +their option values changes. + +Plain registry references (`#bean:`, `#type:`) are still resolved, so a reference a route author wrote in a dynamic +uri, such as `+toD("sql:${header.query}?dataSource=#ds")+`, keeps working. Property placeholders written in the static +route template are also still resolved once when the route starts, so `+toD("mock:foo?delay={{myDelay}}")+` is +unchanged. Only a value that is itself computed from the message and is a `+{{...}}+` placeholder, a `#class:` +reference or a `#property:` reference is kept literal; configure such an option on the component, or use a static +`to(...)` endpoint, if it must be resolved. === camel-core - Rest DSL binding mode json fails at startup without a JSON data format
