Skip to content

Commit

Permalink
ARTEMIS-4926 handled bad URL parameter names & values gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
jbertram committed Jan 15, 2025
1 parent 611b925 commit 107e8fe
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -140,12 +141,24 @@ public static boolean containsQuery(SimpleString uri) {
return uri.contains('?');
}

private static void parseParameters(Map<String, String> rc, String[] parameters) {
protected static void parseParameters(Map<String, String> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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"));
}
}

0 comments on commit 107e8fe

Please sign in to comment.