diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/logs/ActiveMQUtilLogger.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/logs/ActiveMQUtilLogger.java index 540b3a5d39e..a2f3cc95538 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/logs/ActiveMQUtilLogger.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/logs/ActiveMQUtilLogger.java @@ -77,4 +77,10 @@ public interface ActiveMQUtilLogger { @LogMessage(id = 202017, value = "Algorithm two-way is deprecated and will be removed from the default codec in a future version. Use a custom codec instead. Consult the manual for details.", level = LogMessage.Level.WARN) void deprecatedDefaultCodecTwoWayAlgorithm(); + + @LogMessage(id = 202018, value = "Unable to parse URL parameter name from: {}", level = LogMessage.Level.WARN) + void unableToParseURLParameterName(String name, Exception cause); + + @LogMessage(id = 202019, value = "Unable to parse URL parameter value from: {}", level = LogMessage.Level.WARN) + void unableToParseURLParameterValue(String value, Exception cause); } diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java index 53ed8ab4ffd..889c885071b 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java @@ -28,6 +28,7 @@ import java.util.Map; import org.apache.activemq.artemis.api.core.SimpleString; +import org.apache.activemq.artemis.logs.ActiveMQUtilLogger; /** * Utility class that provides methods for parsing URI's @@ -140,12 +141,24 @@ public static boolean containsQuery(SimpleString uri) { return uri.contains('?'); } - private static void parseParameters(Map rc, String[] parameters) { + protected static void parseParameters(Map rc, String[] parameters) { for (String parameter : parameters) { int p = parameter.indexOf("="); if (p >= 0) { - String name = URLDecoder.decode(parameter.substring(0, p), StandardCharsets.UTF_8); - String value = URLDecoder.decode(parameter.substring(p + 1), StandardCharsets.UTF_8); + String name; + String value; + try { + name = URLDecoder.decode(parameter.substring(0, p), StandardCharsets.UTF_8); + } catch (IllegalArgumentException e) { + ActiveMQUtilLogger.LOGGER.unableToParseURLParameterName(parameter.substring(0, p), e); + continue; + } + try { + value = URLDecoder.decode(parameter.substring(p + 1), StandardCharsets.UTF_8); + } catch (IllegalArgumentException e) { + ActiveMQUtilLogger.LOGGER.unableToParseURLParameterValue(parameter.substring(p + 1), e); + continue; + } rc.put(name, value); } else { rc.put(parameter, null); diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/uri/URISupportTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/uri/URISupportTest.java new file mode 100644 index 00000000000..ee2871674f3 --- /dev/null +++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/uri/URISupportTest.java @@ -0,0 +1,63 @@ +/* + * 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.activemq.artemis.utils.uri; + +import java.net.URI; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class URISupportTest { + + @Test + public void testBadParameterValues() throws Exception { + Map result; + + result = URISupport.parseParameters(new URI("tcp://127.0.0.1:61616?foo=bar&KK%258K=")); + assertEquals(1, result.size()); + assertTrue(result.containsKey("foo")); + assertTrue(result.containsValue("bar")); + + result = URISupport.parseParameters(new URI("tcp://127.0.0.1:61616?bar=baz&,KK%25İK=")); + assertEquals(1, result.size()); + assertTrue(result.containsKey("bar")); + assertTrue(result.containsValue("baz")); + + result = URISupport.parseParameters(new URI("tcp://127.0.0.1:61616?KK%25-8K=&bee=boo")); + assertEquals(1, result.size()); + assertTrue(result.containsKey("bee")); + assertTrue(result.containsValue("boo")); + } + + @Test + public void testBadParameterNames() throws Exception { + Map result; + + result = URISupport.parseParameters(new URI("tcp://127.0.0.1:61616?foo=bar&K8K=K%258K=")); + assertEquals(1, result.size()); + assertTrue(result.containsKey("foo")); + assertTrue(result.containsValue("bar")); + + result = URISupport.parseParameters(new URI("tcp://127.0.0.1:61616?bar=baz&KKKÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆ=%25#¿8K=")); + assertEquals(1, result.size()); + assertTrue(result.containsKey("bar")); + assertTrue(result.containsValue("baz")); + } +}