Skip to content

Commit 70caa1b

Browse files
authored
Also validate nested composite URIs used with BrokerView (#1847)
Add a check for VM transports that are in a nested composite URI This is a follow on to #1840
1 parent a4c771f commit 70caa1b

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

activemq-broker/src/main/java/org/apache/activemq/broker/jmx/BrokerView.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,33 @@ public long getTotalMaxUncommittedExceededCount() {
603603
return safeGetBroker().getDestinationStatistics().getMaxUncommittedExceededCount().getCount();
604604
}
605605

606-
607-
// Validate the Url does not contain VM transport
608606
private static void validateAllowedUrl(String uriString) throws URISyntaxException {
609-
URI uri = new URI(uriString);
607+
validateAllowedUri(new URI(uriString), 0);
608+
}
609+
610+
// Validate the URI does not contain VM transport
611+
private static void validateAllowedUri(URI uri, int depth) throws URISyntaxException {
612+
// Don't allow more than 5 nested URIs to prevent blowing the stack
613+
if (depth > 5) {
614+
throw new IllegalArgumentException("URI can't contain more than 5 nested composite URIs");
615+
}
616+
610617
// First check the main URI scheme
611618
validateAllowedScheme(uri.getScheme());
612619

613-
// If composite, also check all schemes for each component
620+
// If composite, iterate and check each of the composite URIs
614621
if (URISupport.isCompositeURI(uri)) {
615622
URISupport.CompositeData data = URISupport.parseComposite(uri);
623+
depth++;
616624
for (URI component : data.getComponents()) {
617-
validateAllowedScheme(component.getScheme());
625+
// Each URI could be a nested composite URI so call validateAllowedUri()
626+
// to validate it. This check if composite first so we don't add to
627+
// the recursive stack depth if there's a lot of URIs that are not composite
628+
if (URISupport.isCompositeURI(uri)) {
629+
validateAllowedUri(component, depth);
630+
} else {
631+
validateAllowedScheme(uri.getScheme());
632+
}
618633
}
619634
}
620635
}

activemq-unit-tests/src/test/java/org/apache/activemq/broker/jmx/MBeanTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.apache.activemq.util.JMXSupport;
6969
import org.apache.activemq.util.URISupport;
7070
import org.apache.activemq.util.Wait;
71+
import org.junit.Test;
7172
import org.junit.experimental.categories.Category;
7273
import org.slf4j.Logger;
7374
import org.slf4j.LoggerFactory;
@@ -2069,17 +2070,36 @@ public void testAddVmConnectorBlockedBrokerView() throws Exception {
20692070

20702071
try {
20712072
brokerView.addConnector("vm://localhost");
2072-
fail("Should have failed trying to add vm connector bridge");
2073+
fail("Should have failed trying to add vm connector");
20732074
} catch (IllegalArgumentException e) {
20742075
assertEquals("VM scheme is not allowed", e.getMessage());
20752076
}
20762077

20772078
try {
20782079
// verify any composite URI is blocked as well
20792080
brokerView.addConnector("failover:(tcp://0.0.0.0:0,vm://" + brokerName + ")");
2080-
fail("Should have failed trying to add vm connector bridge");
2081+
fail("Should have failed trying to add vm connector");
2082+
} catch (IllegalArgumentException e) {
2083+
assertEquals("VM scheme is not allowed", e.getMessage());
2084+
}
2085+
2086+
try {
2087+
// verify nested composite URI is blocked
2088+
brokerView.addConnector("failover:(failover:(failover:(vm://localhost)))");
2089+
fail("Should have failed trying to add vm connector");
20812090
} catch (IllegalArgumentException e) {
20822091
assertEquals("VM scheme is not allowed", e.getMessage());
20832092
}
2093+
2094+
try {
2095+
// verify nested composite URI with more than 5 levels is blocked
2096+
brokerView.addConnector(
2097+
"static:(failover:(failover:(failover:(failover:(failover:(tcp://localhost:0))))))");
2098+
fail("Should have failed trying to add vm connector bridge");
2099+
} catch (IllegalArgumentException e) {
2100+
assertEquals("URI can't contain more than 5 nested composite URIs", e.getMessage());
2101+
}
2102+
20842103
}
2104+
20852105
}

activemq-unit-tests/src/test/java/org/apache/activemq/jmx/JmxCreateNCTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,25 @@ public void testVmBridgeBlocked() throws Exception {
104104
} catch (IllegalArgumentException e) {
105105
assertEquals("VM scheme is not allowed", e.getMessage());
106106
}
107+
108+
try {
109+
// verify nested composite URI is blocked
110+
proxy.addNetworkConnector("static:(failover:(failover:(tcp://localhost:0,vm://localhost)))");
111+
fail("Should have failed trying to add vm connector bridge");
112+
} catch (IllegalArgumentException e) {
113+
assertEquals("VM scheme is not allowed", e.getMessage());
114+
}
115+
}
116+
117+
@Test
118+
public void testAddNetworkConnectorMaxComposite() throws Exception {
119+
try {
120+
// verify nested composite URI with more than 5 levels is blocked
121+
proxy.addNetworkConnector(
122+
"static:(failover:(failover:(failover:(failover:(failover:(tcp://localhost:0))))))");
123+
fail("Should have failed trying to add vm connector bridge");
124+
} catch (IllegalArgumentException e) {
125+
assertEquals("URI can't contain more than 5 nested composite URIs", e.getMessage());
126+
}
107127
}
108128
}

0 commit comments

Comments
 (0)