This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27695-telegram-token-tostring in repository https://gitbox.apache.org/repos/asf/camel.git
commit 931a68c6897dae2080da52e10522413e0347f83b Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 27 17:09:13 2026 +0100 CAMEL-27695: camel-telegram - TelegramConfiguration.toString() shows the authorizationToken TelegramConfiguration.toString() now masks the authorizationToken with xxxxxx. The exception messages of TelegramComponent.createEndpoint() sanitize the endpoint URI, so an authorizationToken in the query is not shown, and the toString() of SendInvoiceMessage and CreateInvoiceLinkMessage (logged by the producer at DEBUG level) masks a non-empty providerToken. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../component/telegram/TelegramComponent.java | 7 ++- .../component/telegram/TelegramConfiguration.java | 3 +- .../model/payments/CreateInvoiceLinkMessage.java | 4 +- .../model/payments/SendInvoiceMessage.java | 4 +- .../telegram/TelegramComponentParametersTest.java | 14 +++++ .../telegram/TelegramConfigurationTest.java | 15 ++++++ .../telegram/model/InvoiceMessageToStringTest.java | 62 ++++++++++++++++++++++ 7 files changed, 104 insertions(+), 5 deletions(-) diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramComponent.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramComponent.java index b02b0757ef7f..0899068fa4d5 100644 --- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramComponent.java +++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramComponent.java @@ -23,6 +23,7 @@ import org.apache.camel.Endpoint; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.HealthCheckComponent; +import org.apache.camel.util.URISupport; @Component("telegram") public class TelegramComponent extends HealthCheckComponent { @@ -56,7 +57,8 @@ public class TelegramComponent extends HealthCheckComponent { configuration.setType(remaining); if (!remaining.equals(TelegramConfiguration.ENDPOINT_TYPE_BOTS)) { - throw new IllegalArgumentException("Unsupported endpoint type for uri " + uri + ", remaining " + remaining); + throw new IllegalArgumentException( + "Unsupported endpoint type for uri " + URISupport.sanitizeUri(uri) + ", remaining " + remaining); } if (configuration.getBaseUri() == null) { configuration.setBaseUri(baseUri); @@ -68,7 +70,8 @@ public class TelegramComponent extends HealthCheckComponent { if (endpoint.getConfiguration().getAuthorizationToken() == null) { throw new IllegalArgumentException( - "AuthorizationToken must be configured on either component or endpoint for telegram: " + uri); + "AuthorizationToken must be configured on either component or endpoint for telegram: " + + URISupport.sanitizeUri(uri)); } return endpoint; diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConfiguration.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConfiguration.java index 764bb816fced..84f99456d8bf 100644 --- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConfiguration.java +++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConfiguration.java @@ -186,7 +186,8 @@ public class TelegramConfiguration { public String toString() { final StringBuilder sb = new StringBuilder("TelegramConfiguration{"); sb.append("type='").append(type).append('\''); - sb.append(", authorizationToken='").append(authorizationToken).append('\''); + // the token is a secret, so it is masked + sb.append(", authorizationToken='").append(authorizationToken != null ? "xxxxxx" : null).append('\''); sb.append(", chatId='").append(chatId).append('\''); sb.append(", timeout=").append(timeout); sb.append(", limit=").append(limit); diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/payments/CreateInvoiceLinkMessage.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/payments/CreateInvoiceLinkMessage.java index 1a97690ae080..c889dd4a2429 100644 --- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/payments/CreateInvoiceLinkMessage.java +++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/payments/CreateInvoiceLinkMessage.java @@ -284,7 +284,9 @@ public class CreateInvoiceLinkMessage implements TelegramMessage { sb.append(", title='").append(title).append('\''); sb.append(", description='").append(description).append('\''); sb.append(", payload='").append(payload).append('\''); - sb.append(", providerToken='").append(providerToken).append('\''); + // the provider token is a secret, so it is masked (it is empty for payments in Telegram Stars) + sb.append(", providerToken='").append(providerToken != null && !providerToken.isEmpty() ? "xxxxxx" : providerToken) + .append('\''); sb.append(", currency='").append(currency).append('\''); sb.append(", prices=").append(prices); sb.append(", subscriptionPeriod=").append(subscriptionPeriod); diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/payments/SendInvoiceMessage.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/payments/SendInvoiceMessage.java index 9375a87439b5..2a18466c2924 100644 --- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/payments/SendInvoiceMessage.java +++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/payments/SendInvoiceMessage.java @@ -340,7 +340,9 @@ public class SendInvoiceMessage extends OutgoingMessage { sb.append(", title='").append(title).append('\''); sb.append(", description='").append(description).append('\''); sb.append(", payload='").append(payload).append('\''); - sb.append(", providerToken='").append(providerToken).append('\''); + // the provider token is a secret, so it is masked (it is empty for payments in Telegram Stars) + sb.append(", providerToken='").append(providerToken != null && !providerToken.isEmpty() ? "xxxxxx" : providerToken) + .append('\''); sb.append(", currency='").append(currency).append('\''); sb.append(", prices=").append(prices); sb.append(", maxTipAmount=").append(maxTipAmount); diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java index 61d89bf96f79..d8a25ce295ea 100644 --- a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java +++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java @@ -21,7 +21,9 @@ import org.apache.camel.component.telegram.util.TelegramTestSupport; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Tests the usage of defaults in the component configuration @@ -74,6 +76,18 @@ public class TelegramComponentParametersTest extends TelegramTestSupport { }); } + @Test + public void testWrongTypeDoesNotShowTheAuthorizationToken() { + TelegramComponent component = (TelegramComponent) context().getComponent("telegram"); + component.setAuthorizationToken(null); + + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { + component.createEndpoint("telegram:channels?authorizationToken=secret-bot-token"); + }); + assertFalse(e.getMessage().contains("secret-bot-token"), e.getMessage()); + assertTrue(e.getMessage().contains("authorizationToken=xxxxxx"), e.getMessage()); + } + @Test public void testWrongURI3() { TelegramComponent component = (TelegramComponent) context().getComponent("telegram"); diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConfigurationTest.java b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConfigurationTest.java index ff2bf8163e96..fcf686fd61cc 100644 --- a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConfigurationTest.java +++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConfigurationTest.java @@ -22,6 +22,8 @@ import org.apache.camel.component.telegram.util.TelegramTestSupport; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Tests that the configuration is loaded properly. @@ -45,6 +47,19 @@ public class TelegramConfigurationTest extends TelegramTestSupport { assertEquals(TelegramProxyType.SOCKS5, config.getProxyType()); } + @Test + public void testToStringMasksTheAuthorizationToken() { + TelegramConfiguration config = new TelegramConfiguration(); + config.setType("bots"); + config.setAuthorizationToken("123456:secret-bot-token"); + config.setChatId("12345"); + + String text = config.toString(); + assertFalse(text.contains("secret-bot-token"), text); + assertTrue(text.contains("authorizationToken='xxxxxx'"), text); + assertTrue(text.contains("chatId='12345'"), text); + } + @Override protected RoutesBuilder createRouteBuilder() { return new RouteBuilder() { diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/model/InvoiceMessageToStringTest.java b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/model/InvoiceMessageToStringTest.java new file mode 100644 index 000000000000..f53a2f6a2c41 --- /dev/null +++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/model/InvoiceMessageToStringTest.java @@ -0,0 +1,62 @@ +/* + * 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.component.telegram.model; + +import org.apache.camel.component.telegram.model.payments.CreateInvoiceLinkMessage; +import org.apache.camel.component.telegram.model.payments.SendInvoiceMessage; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The producer logs the outgoing message at DEBUG level, so the toString() of an invoice must not show the provider + * token. + */ +class InvoiceMessageToStringTest { + + @Test + void testSendInvoiceMessageMasksTheProviderToken() { + SendInvoiceMessage message = new SendInvoiceMessage(); + message.setTitle("Coffee"); + message.setProviderToken("284685063:TEST:secret-provider-token"); + + assertThat(message.toString()) + .doesNotContain("secret-provider-token") + .contains("providerToken='xxxxxx'") + .contains("title='Coffee'"); + } + + @Test + void testCreateInvoiceLinkMessageMasksTheProviderToken() { + CreateInvoiceLinkMessage message = new CreateInvoiceLinkMessage(); + message.setTitle("Coffee"); + message.setProviderToken("284685063:TEST:secret-provider-token"); + + assertThat(message.toString()) + .doesNotContain("secret-provider-token") + .contains("providerToken='xxxxxx'") + .contains("title='Coffee'"); + } + + @Test + void testEmptyProviderTokenOfTelegramStarsIsShown() { + SendInvoiceMessage message = new SendInvoiceMessage(); + message.setProviderToken(""); + + assertThat(message.toString()).contains("providerToken=''"); + } +}
