From c34935dcd3476db7397e7433de6243ec81a09ccf Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Tue, 10 Jul 2018 15:22:20 -0400 Subject: [PATCH] [JCLOUDS-1433] $ and \ in tokenValues passed to Strings2.replaceTokens(String, Multimap) could result in IllegalArgumentException. --- core/src/main/java/org/jclouds/util/Strings2.java | 2 +- core/src/test/java/org/jclouds/util/Strings2Test.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/jclouds/util/Strings2.java b/core/src/main/java/org/jclouds/util/Strings2.java index 27fd545e4d..cc09b6ca9f 100644 --- a/core/src/main/java/org/jclouds/util/Strings2.java +++ b/core/src/main/java/org/jclouds/util/Strings2.java @@ -182,7 +182,7 @@ public class Strings2 { public static String replaceTokens(String input, Multimap tokenValues) { for (Entry tokenValue : tokenValues.entries()) { Pattern pattern = TOKEN_TO_PATTERN.getUnchecked(tokenValue.getKey()); - input = pattern.matcher(input).replaceAll(tokenValue.getValue().toString()); + input = pattern.matcher(input).replaceAll(tokenValue.getValue().toString().replace("\\", "\\\\").replace("$", "\\$")); } return input; } diff --git a/core/src/test/java/org/jclouds/util/Strings2Test.java b/core/src/test/java/org/jclouds/util/Strings2Test.java index 2acdd35fc2..cf838bcc43 100644 --- a/core/src/test/java/org/jclouds/util/Strings2Test.java +++ b/core/src/test/java/org/jclouds/util/Strings2Test.java @@ -23,12 +23,15 @@ import static org.testng.Assert.assertEquals; import org.testng.annotations.Test; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMultimap; @Test(groups = "unit") public class Strings2Test { public void testReplaceTokens() { assertEquals(Strings2.replaceTokens("hello {where}", ImmutableMap.of("where", "world")), "hello world"); + assertEquals(Strings2.replaceTokens("hello {where}", ImmutableMap.of("where", "$1,000,000 \\o/!")), "hello $1,000,000 \\o/!"); + assertEquals(Strings2.replaceTokens("hello {where}", ImmutableMultimap.of("where", "$1,000,000 \\o/!")), "hello $1,000,000 \\o/!"); } public void testUrlEncodeDecodeShouldGiveTheSameString() {