From 8cb5332a50ffe7eaba54bdf84796a241d8a3d438 Mon Sep 17 00:00:00 2001 From: Chris Rog Date: Thu, 14 May 2020 11:25:15 -0400 Subject: [PATCH] ARTEMIS-2765 Fix parsing error in XMLUtil Fix a StringIndexOutOfBoundsException in XMLUtil while parsing system properties in XML that contains curly braces or unterminated property references --- .../activemq/artemis/utils/XMLUtil.java | 10 +++++++--- .../activemq/artemis/util/XMLUtilTest.java | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/utils/XMLUtil.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/utils/XMLUtil.java index ce75f3350b..37fa553750 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/utils/XMLUtil.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/utils/XMLUtil.java @@ -255,23 +255,27 @@ public final class XMLUtil { public static String replaceSystemPropsInString(String xml) { while (xml.contains("${")) { int start = xml.indexOf("${"); - int end = xml.indexOf("}") + 1; - if (end < 0) { + int end = xml.indexOf("}", start) + 1; + + if (start < 0 || end <= 0) { break; } + String subString = xml.substring(start, end); String prop = subString.substring(2, subString.length() - 1).trim(); String val = ""; + if (prop.contains(":")) { String[] parts = prop.split(":", 2); prop = parts[0].trim(); val = parts[1].trim(); } + String sysProp = System.getProperty(prop, val); logger.debug("replacing " + subString + " with " + sysProp); xml = xml.replace(subString, sysProp); - } + return xml; } diff --git a/artemis-core-client/src/test/java/org/apache/activemq/artemis/util/XMLUtilTest.java b/artemis-core-client/src/test/java/org/apache/activemq/artemis/util/XMLUtilTest.java index 6a19622c2e..9d8bbfcb73 100644 --- a/artemis-core-client/src/test/java/org/apache/activemq/artemis/util/XMLUtilTest.java +++ b/artemis-core-client/src/test/java/org/apache/activemq/artemis/util/XMLUtilTest.java @@ -218,6 +218,26 @@ public class XMLUtilTest extends SilentTestCase { Assert.assertEquals(after, replaced); } + @Test + public void testReplaceSystemPropertiesWithUnclosedPropertyReferenceInXML() { + String before = "\n" + " content1\n" + " content2\n" + " content3\n" + " ${sysprop2\n" + " content5\n" + " content6\n" + ""; + String after = "\n" + " content1\n" + " content2\n" + " content3\n" + " ${sysprop2\n" + " content5\n" + " content6\n" + ""; + System.setProperty("sysprop1", "test1"); + System.setProperty("sysprop2", "content4"); + String replaced = XMLUtil.replaceSystemPropsInString(before); + Assert.assertEquals(after, replaced); + } + + @Test + public void testReplaceSystemPropertiesWithMiscCurlyBracesInXML() { + String before = "\n" + " content1{ }\n" + " content2 {\n" + " content3 }\n" + " ${sysprop2}\n" + " content5{ }\n" + " content6 }\n" + ""; + String after = "\n" + " content1{ }\n" + " content2 {\n" + " content3 }\n" + " content4\n" + " content5{ }\n" + " content6 }\n" + ""; + System.setProperty("sysprop1", "test1"); + System.setProperty("sysprop2", "content4"); + String replaced = XMLUtil.replaceSystemPropsInString(before); + Assert.assertEquals(after, replaced); + } + @Test public void testStripCDATA() throws Exception { String xml = "";