Skip to content

Commit 9bb9ee6

Browse files
authored
Also validate nested composite URIs used with BrokerView (#1849)
Add a check for VM transports that are in a nested composite URI This is a follow on to #1840 (cherry picked from commit 70caa1b)
1 parent 744fb35 commit 9bb9ee6

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

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
@@ -563,18 +563,33 @@ public long getTotalMaxUncommittedExceededCount() {
563563
return safeGetBroker().getDestinationStatistics().getMaxUncommittedExceededCount().getCount();
564564
}
565565

566-
567-
// Validate the Url does not contain VM transport
568566
private static void validateAllowedUrl(String uriString) throws URISyntaxException {
569-
URI uri = new URI(uriString);
567+
validateAllowedUri(new URI(uriString), 0);
568+
}
569+
570+
// Validate the URI does not contain VM transport
571+
private static void validateAllowedUri(URI uri, int depth) throws URISyntaxException {
572+
// Don't allow more than 5 nested URIs to prevent blowing the stack
573+
if (depth > 5) {
574+
throw new IllegalArgumentException("URI can't contain more than 5 nested composite URIs");
575+
}
576+
570577
// First check the main URI scheme
571578
validateAllowedScheme(uri.getScheme());
572579

573-
// If composite, also check all schemes for each component
580+
// If composite, iterate and check each of the composite URIs
574581
if (URISupport.isCompositeURI(uri)) {
575582
URISupport.CompositeData data = URISupport.parseComposite(uri);
583+
depth++;
576584
for (URI component : data.getComponents()) {
577-
validateAllowedScheme(component.getScheme());
585+
// Each URI could be a nested composite URI so call validateAllowedUri()
586+
// to validate it. This check if composite first so we don't add to
587+
// the recursive stack depth if there's a lot of URIs that are not composite
588+
if (URISupport.isCompositeURI(uri)) {
589+
validateAllowedUri(component, depth);
590+
} else {
591+
validateAllowedScheme(uri.getScheme());
592+
}
578593
}
579594
}
580595
}

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
@@ -67,6 +67,7 @@
6767
import org.apache.activemq.util.JMXSupport;
6868
import org.apache.activemq.util.URISupport;
6969
import org.apache.activemq.util.Wait;
70+
import org.junit.Test;
7071
import org.slf4j.Logger;
7172
import org.slf4j.LoggerFactory;
7273

@@ -2067,17 +2068,36 @@ public void testAddVmConnectorBlockedBrokerView() throws Exception {
20672068

20682069
try {
20692070
brokerView.addConnector("vm://localhost");
2070-
fail("Should have failed trying to add vm connector bridge");
2071+
fail("Should have failed trying to add vm connector");
20712072
} catch (IllegalArgumentException e) {
20722073
assertEquals("VM scheme is not allowed", e.getMessage());
20732074
}
20742075

20752076
try {
20762077
// verify any composite URI is blocked as well
20772078
brokerView.addConnector("failover:(tcp://0.0.0.0:0,vm://" + brokerName + ")");
2078-
fail("Should have failed trying to add vm connector bridge");
2079+
fail("Should have failed trying to add vm connector");
2080+
} catch (IllegalArgumentException e) {
2081+
assertEquals("VM scheme is not allowed", e.getMessage());
2082+
}
2083+
2084+
try {
2085+
// verify nested composite URI is blocked
2086+
brokerView.addConnector("failover:(failover:(failover:(vm://localhost)))");
2087+
fail("Should have failed trying to add vm connector");
20792088
} catch (IllegalArgumentException e) {
20802089
assertEquals("VM scheme is not allowed", e.getMessage());
20812090
}
2091+
2092+
try {
2093+
// verify nested composite URI with more than 5 levels is blocked
2094+
brokerView.addConnector(
2095+
"static:(failover:(failover:(failover:(failover:(failover:(tcp://localhost:0))))))");
2096+
fail("Should have failed trying to add vm connector bridge");
2097+
} catch (IllegalArgumentException e) {
2098+
assertEquals("URI can't contain more than 5 nested composite URIs", e.getMessage());
2099+
}
2100+
20822101
}
2102+
20832103
}

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)