[JCLOUDS-1433] $ and \ in tokenValues passed to Strings2.replaceTokens(String, Multimap) could result in IllegalArgumentException.

This commit is contained in:
Jesse Glick 2018-07-10 15:22:20 -04:00 committed by Ignasi Barrera
parent 1bd3b8f9df
commit c34935dcd3
2 changed files with 4 additions and 1 deletions

View File

@ -182,7 +182,7 @@ public class Strings2 {
public static String replaceTokens(String input, Multimap<String, ?> tokenValues) { public static String replaceTokens(String input, Multimap<String, ?> tokenValues) {
for (Entry<String, ?> tokenValue : tokenValues.entries()) { for (Entry<String, ?> tokenValue : tokenValues.entries()) {
Pattern pattern = TOKEN_TO_PATTERN.getUnchecked(tokenValue.getKey()); 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; return input;
} }

View File

@ -23,12 +23,15 @@ import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
@Test(groups = "unit") @Test(groups = "unit")
public class Strings2Test { public class Strings2Test {
public void testReplaceTokens() { public void testReplaceTokens() {
assertEquals(Strings2.replaceTokens("hello {where}", ImmutableMap.of("where", "world")), "hello world"); 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() { public void testUrlEncodeDecodeShouldGiveTheSameString() {